|
2 | 2 | //
|
3 | 3 | // This source file is part of the Swift open source project
|
4 | 4 | //
|
5 |
| -// Copyright (c) 2020-2021 Apple Inc. and the Swift project authors |
| 5 | +// Copyright (c) 2020-2024 Apple Inc. and the Swift project authors |
6 | 6 | // Licensed under Apache License v2.0 with Runtime Library Exception
|
7 | 7 | //
|
8 | 8 | // See http://swift.org/LICENSE.txt for license information
|
@@ -632,3 +632,48 @@ extension FileLock {
|
632 | 632 | return try Self.prepareLock(fileToLock: fileToLock.underlying, at: lockFilesDirectory?.underlying)
|
633 | 633 | }
|
634 | 634 | }
|
| 635 | + |
| 636 | +/// Convenience initializers for testing purposes. |
| 637 | +extension InMemoryFileSystem { |
| 638 | + /// Create a new file system with the given files, provided as a map from |
| 639 | + /// file path to contents. |
| 640 | + public convenience init(files: [String: ByteString]) { |
| 641 | + self.init() |
| 642 | + |
| 643 | + for (path, contents) in files { |
| 644 | + let path = try! AbsolutePath(validating: path) |
| 645 | + try! createDirectory(path.parentDirectory, recursive: true) |
| 646 | + try! writeFileContents(path, bytes: contents) |
| 647 | + } |
| 648 | + } |
| 649 | + |
| 650 | + /// Create a new file system with an empty file at each provided path. |
| 651 | + public convenience init(emptyFiles files: String...) { |
| 652 | + self.init(emptyFiles: files) |
| 653 | + } |
| 654 | + |
| 655 | + /// Create a new file system with an empty file at each provided path. |
| 656 | + public convenience init(emptyFiles files: [String]) { |
| 657 | + self.init() |
| 658 | + self.createEmptyFiles(at: .root, files: files) |
| 659 | + } |
| 660 | +} |
| 661 | + |
| 662 | +extension FileSystem { |
| 663 | + public func createEmptyFiles(at root: AbsolutePath, files: String...) { |
| 664 | + self.createEmptyFiles(at: root, files: files) |
| 665 | + } |
| 666 | + |
| 667 | + public func createEmptyFiles(at root: AbsolutePath, files: [String]) { |
| 668 | + do { |
| 669 | + try createDirectory(root, recursive: true) |
| 670 | + for path in files { |
| 671 | + let path = try AbsolutePath(validating: String(path.dropFirst()), relativeTo: root) |
| 672 | + try createDirectory(path.parentDirectory, recursive: true) |
| 673 | + try writeFileContents(path, bytes: "") |
| 674 | + } |
| 675 | + } catch { |
| 676 | + fatalError("Failed to create empty files: \(error)") |
| 677 | + } |
| 678 | + } |
| 679 | +} |
0 commit comments