Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Sources/Testing/ABI/EntryPoints/EntryPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if canImport(Foundation)
private import Foundation
Comment thread
bkhouri marked this conversation as resolved.
#endif
private import _TestingInternals

#if canImport(Synchronization)
Expand Down Expand Up @@ -622,9 +625,14 @@ public func configurationForEntryPoint(from args: __CommandLineArguments_v0) thr

// Attachment output.
if let attachmentsPath = args.attachmentsPath {
guard fileExists(atPath: attachmentsPath) else {
throw _EntryPointError.invalidArgument("---attachments-path", value: attachmentsPath)
}

#if canImport(Foundation)
try FileManager().createDirectory(atPath: attachmentsPath, withIntermediateDirectories: true)
#else
guard fileExists(atPath: attachmentsPath) else {
throw _EntryPointError.invalidArgument("---attachments-path", value: attachmentsPath)
}
#endif
configuration.attachmentsPath = attachmentsPath
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Testing/Testing.docc/Attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ after your tests finish running:
`.build/attachments` by default. Visual Studio Code reports the paths to
individual attachments in its Tests Results panel.
- When using Swift Package Manager's `swift test` command, you can pass the
`--attachments-path` option. The testing library saves attachments to the
specified directory.
`--attachments-path` option. The testing library creates a directory at the
specified path and saves attachments to it.

If you do not pass the `--attachments-path` option, the testing library does
not save any attachments you record.
Expand Down
54 changes: 54 additions & 0 deletions Tests/TestingTests/SwiftPMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing
private import _TestingInternals
#if canImport(Foundation)
private import Foundation
Comment thread
bkhouri marked this conversation as resolved.
#endif

private func configurationForEntryPoint(withArguments args: [String]) throws -> Configuration {
let args = try parseCommandLineArguments(from: args)
Expand Down Expand Up @@ -385,6 +388,57 @@ struct SwiftPMTests {
#expect(fileContents.contains(UInt8(ascii: ">")))
}

#if canImport(Foundation)
@Test(
Comment thread
bkhouri marked this conversation as resolved.
"--attachments-path argument (creates missing directory)",
arguments: ["--attachments-path", "--experimental-attachments-path"]
)
func attachmentsPathCreatesMissingDirectory(argumentName: String) throws {
let tempDirPath = try temporaryDirectory()
let attachmentsPath = appendPathComponent("swt_attachments_\(UInt64.random(in: 0 ..< .max))", to: tempDirPath)
defer {
_ = remove(attachmentsPath)
}
#expect(!fileExists(atPath: attachmentsPath))
let configuration = try configurationForEntryPoint(withArguments: ["PATH", argumentName, attachmentsPath])
#expect(fileExists(atPath: attachmentsPath))
let actualPath = try #require(configuration.attachmentsPath, "Attachments path is not expected to be nil")
#expect(canonicalizePath(actualPath) == canonicalizePath(attachmentsPath))
}
#endif

#if canImport(Foundation)
@Test("--attachments-path argument (bad path)")
func attachmentsPathWithBadPath() throws {
let tempDirPath = try temporaryDirectory()
let attachmentPath = appendPathComponent(UUID().uuidString, to: tempDirPath)
let fileManager = FileManager()
let success = fileManager.createFile(atPath: attachmentPath, contents: nil, )
if !success {
Issue.record("Test setup failure. Could not create file at \(attachmentPath).")
}
defer {
print("removing \(attachmentPath) ...")
_ = remove(attachmentPath)
}
#expect(throws: (any Error).self, "Attachment path is: \(attachmentPath)") {
_ = try configurationForEntryPoint(withArguments: ["PATH", "--attachments-path", attachmentPath])
}
}
#endif

@Test("--attachments-path argument (accepts existing directory)")
func attachmentsPathAcceptsExistingDirectory() throws {
let tempDirPath = try temporaryDirectory()
#expect(fileExists(atPath: tempDirPath))
let configuration = try configurationForEntryPoint(withArguments: ["PATH", "--attachments-path", tempDirPath])
let actualPath = try #require(configuration.attachmentsPath, "Attachments path is not expected to be nil")
#expect(
canonicalizePath(actualPath) == canonicalizePath(tempDirPath),
"Canonicalized actual path (\(actualPath)) is not equal to canonicalized expected path (\(tempDirPath))",
)
}

Comment thread
bkhouri marked this conversation as resolved.
@Test("--configuration-path argument", arguments: [
"--configuration-path", "--experimental-configuration-path",
])
Expand Down