Skip to content
Open
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
122 changes: 75 additions & 47 deletions Sources/_InternalTestSupport/SwiftTesting+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,106 +11,134 @@
import Basics
import Testing

// MARK: File System Helpers

/// Verifies that a file exists at the specified path.
///
/// - Parameters:
/// - path: The absolute path to check for file existence.
/// - sourceLocation: The source location where the expectation is made.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: thank you for adding documentation to these files! this makes it a lot more readable.

public func expectFileExists(
at path: AbsolutePath,
_ comment: Comment? = nil,
sourceLocation: SourceLocation = #_sourceLocation,
) {
let commentPrefix =
if let comment {
"\(comment): "
} else {
""
}
let msgSuffix: String
do {
msgSuffix = try "Directory contents: \(localFileSystem.getDirectoryContents(path.parentDirectory))"
} catch {
msgSuffix = ""
}
#expect(
localFileSystem.exists(path),
"\(commentPrefix)File '\(path)' does not exist. \(msgSuffix)",
wrapMessage(
"Files '\(path)' does not exist.",
comment: comment,
directoryPath: path.parentDirectory
),
sourceLocation: sourceLocation,
)
}

/// Verifies that a file does not exist at the specified path.
///
/// - Parameters:
/// - path: The absolute path to check for file non-existence.
/// - comment: An optional comment to include in the failure message.
/// - sourceLocation: The source location where the expectation is made.
public func expectFileDoesNotExists(
at path: AbsolutePath,
_ comment: Comment? = nil,
sourceLocation: SourceLocation = #_sourceLocation,
) {
let commentPrefix =
if let comment {
"\(comment): "
} else {
""
}
let msgSuffix: String
do {
msgSuffix = try "Directory contents: \(localFileSystem.getDirectoryContents(path.parentDirectory))"
} catch {
msgSuffix = ""
}
#expect(
!localFileSystem.exists(path),
"\(commentPrefix)File: '\(path)' was not expected to exist, but does.\(msgSuffix))",
wrapMessage(
"File: '\(path)' was not expected to exist, but does.",
comment: comment,
directoryPath: path.parentDirectory
),
sourceLocation: sourceLocation,
)
}

/// Verifies that a file exists and is executable at the specified path.
///
/// - Parameters:
/// - fixturePath: The absolute path to check for executable file existence.
/// - comment: An optional comment to include in the failure message.
/// - sourceLocation: The source location where the expectation is made.
public func expectFileIsExecutable(
at fixturePath: AbsolutePath,
_ comment: Comment? = nil,
sourceLocation: SourceLocation = #_sourceLocation,
) {
let commentPrefix =
if let comment {
"\(comment): "
} else {
""
}
#expect(
localFileSystem.isExecutableFile(fixturePath),
"\(commentPrefix)File '\(fixturePath)' expected to be executable, but is not.",
wrapMessage("File '\(fixturePath)' expected to be executable, but is not.", comment: comment),
sourceLocation: sourceLocation,
)
}

/// Verifies that a directory exists at the specified path.
///
/// - Parameters:
/// - path: The absolute path to check for directory existence.
/// - sourceLocation: The source location where the expectation is made.
public func expectDirectoryExists(
at path: AbsolutePath,
_ comment: Comment? = nil,
sourceLocation: SourceLocation = #_sourceLocation,
) {
let msgSuffix: String
do {
msgSuffix = try "Directory contents: \(localFileSystem.getDirectoryContents(path))"
} catch {
msgSuffix = ""
}
#expect(
localFileSystem.isDirectory(path),
"Expected directory doesn't exist: '\(path)'. \(msgSuffix)",
wrapMessage("Expected directory doesn't exist: '\(path)'", comment: comment, directoryPath: path),
sourceLocation: sourceLocation,
)
}

/// Verifies that a directory does not exist at the specified path.
///
/// - Parameters:
/// - path: The absolute path to check for directory non-existence.
/// - sourceLocation: The source location where the expectation is made.
public func expectDirectoryDoesNotExist(
at path: AbsolutePath,
_ comment: Comment? = nil,
sourceLocation: SourceLocation = #_sourceLocation,
) {
let msgSuffix: String
do {
msgSuffix = try "Directory contents: \(localFileSystem.getDirectoryContents(path))"
} catch {
msgSuffix = ""
}
#expect(
!localFileSystem.isDirectory(path),
"Directory exists unexpectedly: '\(path)'.\(msgSuffix)",
wrapMessage("Directory exists unexpectedly: '\(path)'", comment: comment, directoryPath: path),
sourceLocation: sourceLocation,
)
}

/// Wraps a message with an optional comment prefix and directory contents suffix.
///
/// - Parameters:
/// - message: The base message to wrap.
/// - comment: An optional comment to prefix the message with.
/// - directoryPath: An optional path to a folder whose contents will be appended to the message.
/// - Returns: The formatted message with prefix and suffix.
private func wrapMessage(
_ message: Comment,
comment: Comment? = nil,
directoryPath: AbsolutePath? = nil
) -> Comment {
let commentPrefix =
if let comment {
"\(comment): "
} else {
""
}

var msgSuffix = ""
if let directoryPath {
do {
msgSuffix = try " Directory contents: \(localFileSystem.getDirectoryContents(directoryPath))"
} catch {
// Silently ignore errors when getting directory contents
}
}

return "\(commentPrefix)\(message)\(msgSuffix)"
}

/// Expects that the expression throws a CommandExecutionError and passes it to the provided throwing error handler.
/// - Parameters:
/// - expression: The expression expected to throw
Expand Down
Loading