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
47 changes: 45 additions & 2 deletions Sources/_InternalTestSupport/SwiftTesting+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@
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,
sourceLocation: SourceLocation = #_sourceLocation,
) {

#expect(
localFileSystem.exists(path),
"Files '\(path)' does not exist.",
sourceLocation: sourceLocation,
)
}

/// Verifies that a file does not exist at the specified path.
///
/// - Parameters:
/// - fixturePath: 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 fixturePath: AbsolutePath,
_ comment: Comment? = nil,
Expand All @@ -41,6 +53,12 @@ public func expectFileDoesNotExists(
)
}

/// 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,
Expand All @@ -59,6 +77,11 @@ public func expectFileIsExecutable(
)
}

/// 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,
sourceLocation: SourceLocation = #_sourceLocation,
Expand All @@ -70,6 +93,11 @@ public func expectDirectoryExists(
)
}

/// 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,
sourceLocation: SourceLocation = #_sourceLocation,
Expand All @@ -81,6 +109,15 @@ public func expectDirectoryDoesNotExist(
)
}

// MARK: Error Helpers

/// Verifies that an expression throws a `CommandExecutionError`.
///
/// - Parameters:
/// - expression: The expression to evaluate.
/// - message: An optional description of the failure.
/// - sourceLocation: The source location where the expectation is made.
/// - errorHandler: A closure that's called with the error if the expression throws.
public func expectThrowsCommandExecutionError<T>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> Comment = "",
Expand All @@ -99,7 +136,13 @@ public func expectThrowsCommandExecutionError<T>(
}
}

/// An `async`-friendly replacement for `XCTAssertThrowsError`.
/// An async-friendly replacement for `XCTAssertThrowsError` that verifies an expression throws an error.
///
/// - Parameters:
/// - expression: The expression to evaluate that should throw an error.
/// - message: An optional failure message to display if the expression doesn't throw.
/// - sourceLocation: The source location where the expectation is made.
/// - errorHandler: A closure that's called with the error if the expression throws.
public func expectAsyncThrowsError<T>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> Comment? = nil,
Expand Down
Loading