1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import func XCTest.XCTFail
import TSCBasic
import TSCUtility
#if canImport(Darwin)
import class Foundation.Bundle
#endif
/// Convenience initializers for testing purposes.
extension InMemoryFileSystem {
/// Create a new file system with the given files, provided as a map from
/// file path to contents.
public convenience init(files: [String: ByteString]) {
self.init()
for (path, contents) in files {
let path = try! AbsolutePath(validating: path)
try! createDirectory(path.parentDirectory, recursive: true)
try! writeFileContents(path, bytes: contents)
}
}
/// Create a new file system with an empty file at each provided path.
public convenience init(emptyFiles files: String...) {
self.init(emptyFiles: files)
}
/// Create a new file system with an empty file at each provided path.
public convenience init(emptyFiles files: [String]) {
self.init()
self.createEmptyFiles(at: .root, files: files)
}
}
extension FileSystem {
public func createEmptyFiles(at root: AbsolutePath, files: String...) {
self.createEmptyFiles(at: root, files: files)
}
public func createEmptyFiles(at root: AbsolutePath, files: [String]) {
do {
try createDirectory(root, recursive: true)
for path in files {
let path = try AbsolutePath(validating: String(path.dropFirst()), relativeTo: root)
try createDirectory(path.parentDirectory, recursive: true)
try writeFileContents(path, bytes: "")
}
} catch {
fatalError("Failed to create empty files: \(error)")
}
}
}
extension FileSystem {
/// Print the contents of the directory. Only for debugging purposes.
public func dump(directory path: AbsolutePath) {
do {
print(try getDirectoryContents(path))
} catch {
print(String(describing: error))
}
}
}
extension AbsolutePath {
@available(*, deprecated, message: "use direct string instead")
public init(path: StaticString) {
let pathString = path.withUTF8Buffer {
String(decoding: $0, as: UTF8.self)
}
try! self.init(validating: pathString)
}
@available(*, deprecated, message: "use init(: relativeTo:) instead")
public init(path: StaticString, relativeTo basePath: AbsolutePath) {
let pathString = path.withUTF8Buffer {
String(decoding: $0, as: UTF8.self)
}
try! self.init(validating: pathString, relativeTo: basePath)
}
@available(*, deprecated, message: "use direct string instead")
public init(base: AbsolutePath, _ relStr: StaticString) {
let pathString = relStr.withUTF8Buffer {
String(decoding: $0, as: UTF8.self)
}
self.init(base, RelativePath(stringLiteral: pathString))
}
}
extension AbsolutePath: ExpressibleByStringLiteral {
public init(_ value: StringLiteralType) {
try! self.init(validating: value)
}
}
extension AbsolutePath: ExpressibleByStringInterpolation {
public init(stringLiteral value: String) {
try! self.init(validating: value)
}
}
extension AbsolutePath {
public init(_ path: StringLiteralType, relativeTo basePath: AbsolutePath) {
try! self.init(validating: path, relativeTo: basePath)
}
}
extension RelativePath {
@available(*, deprecated, message: "use direct string instead")
public init(static path: StaticString) {
let pathString = path.withUTF8Buffer {
String(decoding: $0, as: UTF8.self)
}
try! self.init(validating: pathString)
}
}
extension RelativePath: ExpressibleByStringLiteral {
public init(_ value: StringLiteralType) {
try! self.init(validating: value)
}
}
extension RelativePath: ExpressibleByStringInterpolation {
public init(stringLiteral value: String) {
try! self.init(validating: value)
}
}
|