Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.0] Cover –-toolchain argument in SwiftCommandStateTests #7485

Draft
wants to merge 5 commits into
base: release/6.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/Basics/CMakeLists.txt
Expand Up @@ -29,6 +29,7 @@ add_library(Basics
Errors.swift
FileSystem/AbsolutePath.swift
FileSystem/FileSystem+Extensions.swift
FileSystem/InMemoryFileSystem.swift
FileSystem/NativePathExtensions.swift
FileSystem/RelativePath.swift
FileSystem/TemporaryFile.swift
Expand Down
47 changes: 46 additions & 1 deletion Sources/Basics/FileSystem/FileSystem+Extensions.swift
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
// Copyright (c) 2020-2024 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
Expand Down Expand Up @@ -632,3 +632,48 @@ extension FileLock {
return try Self.prepareLock(fileToLock: fileToLock.underlying, at: lockFilesDirectory?.underlying)
}
}

/// 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)")
}
}
}