Skip to content

Commit d4968ab

Browse files
committed
Move test utilities out to helper file
1 parent 985ba15 commit d4968ab

File tree

2 files changed

+77
-45
lines changed

2 files changed

+77
-45
lines changed

Sources/_InternalTestSupport/SwiftTesting+Helpers.swift

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,67 @@
1010

1111
import Basics
1212
import Testing
13+
import TSCTestSupport
1314

15+
// MARK: File Helpers
16+
17+
/// Verifies that a file exists at the specified path.
18+
///
19+
/// - Parameters:
20+
/// - path: The absolute path to check for file existence.
21+
/// - sourceLocation: The source location where the expectation is made.
1422
public func expectFileExists(
1523
at path: AbsolutePath,
1624
sourceLocation: SourceLocation = #_sourceLocation,
1725
) {
18-
1926
#expect(
2027
localFileSystem.exists(path),
2128
"Files '\(path)' does not exist.",
2229
sourceLocation: sourceLocation,
2330
)
2431
}
2532

33+
/// Verifies that no file or directory exists at the specified path.
34+
///
35+
/// - Parameters:
36+
/// - path: The absolute path to check for non-existence.
37+
/// - sourceLocation: The source location where the expectation is made.
38+
public func expectNoSuchPath(
39+
_ path: AbsolutePath,
40+
sourceLocation: SourceLocation = #_sourceLocation
41+
) {
42+
#expect(
43+
!localFileSystem.exists(path),
44+
"Expected no such path '\(path)'",
45+
sourceLocation: sourceLocation
46+
)
47+
}
48+
49+
/// Verifies that a directory exists at the specified path.
50+
///
51+
/// - Parameters:
52+
/// - path: The absolute path to check for directory existence.
53+
/// - sourceLocation: The source location where the expectation is made.
54+
public func expectDirectoryExists(
55+
_ path: AbsolutePath,
56+
sourceLocation: SourceLocation = #_sourceLocation
57+
) {
58+
#expect(
59+
localFileSystem.isDirectory(path),
60+
"Expected directory at '\(path)'",
61+
sourceLocation: sourceLocation
62+
)
63+
}
64+
65+
// MARK: Error Helpers
2666

67+
/// Verifies that an expression throws a `CommandExecutionError`.
68+
///
69+
/// - Parameters:
70+
/// - expression: The expression to evaluate.
71+
/// - message: An optional description of the failure.
72+
/// - sourceLocation: The source location where the expectation is made.
73+
/// - errorHandler: A closure that's called with the error if the expression throws.
2774
public func expectThrowsCommandExecutionError<T>(
2875
_ expression: @autoclosure () async throws -> T,
2976
_ message: @autoclosure () -> Comment = "",
@@ -42,6 +89,12 @@ public func expectThrowsCommandExecutionError<T>(
4289
}
4390

4491
/// An `async`-friendly replacement for `XCTAssertThrowsError`.
92+
///
93+
/// - Parameters:
94+
/// - expression: The expression to evaluate.
95+
/// - message: An optional description of the failure.
96+
/// - sourceLocation: The source location where the expectation is made.
97+
/// - errorHandler: A closure that's called with the error if the expression throws.
4598
public func expectAsyncThrowsError<T>(
4699
_ expression: @autoclosure () async throws -> T,
47100
_ message: @autoclosure () -> Comment? = nil,
@@ -55,3 +108,26 @@ public func expectAsyncThrowsError<T>(
55108
errorHandler(error)
56109
}
57110
}
111+
112+
// MARK: String Pattern Matching Helpers
113+
114+
/// Verifies that a string matches a pattern.
115+
///
116+
/// - Parameters:
117+
/// - value: The string to test.
118+
/// - pattern: The pattern to match against.
119+
/// - sourceLocation: The source location where the expectation is made.
120+
public func expectMatch(_ value: String, _ pattern: StringPattern, sourceLocation: SourceLocation = #_sourceLocation) {
121+
#expect(pattern ~= value, "Expected match for '\(value)' with pattern '\(pattern)'", sourceLocation: sourceLocation)
122+
}
123+
124+
/// Verifies that a string does not match a pattern.
125+
///
126+
/// - Parameters:
127+
/// - value: The string to test.
128+
/// - pattern: The pattern to match against.
129+
/// - sourceLocation: The source location where the expectation is made.
130+
public func expectNoMatch(_ value: String, _ pattern: StringPattern, sourceLocation: SourceLocation = #_sourceLocation) {
131+
#expect(!(pattern ~= value), "Expected no match for '\(value)' with pattern '\(pattern)'", sourceLocation: sourceLocation)
132+
}
133+

Tests/WorkspaceTests/InitTests.swift

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,8 @@ import _InternalTestSupport
1515
import PackageModel
1616
import Workspace
1717
import Testing
18-
19-
import TSCTestSupport
2018
import SPMBuildCore
2119

22-
public func expectMatch(_ value: String, _ pattern: StringPattern, sourceLocation: SourceLocation = #_sourceLocation) {
23-
#expect(pattern ~= value, "Expected match for '\(value)' with pattern '\(pattern)'", sourceLocation: sourceLocation)
24-
}
25-
public func expectNoMatch(_ value: String, _ pattern: StringPattern, sourceLocation: SourceLocation = #_sourceLocation) {
26-
#expect(!(pattern ~= value), "Expected no match for '\(value)' with pattern '\(pattern)'", sourceLocation: sourceLocation)
27-
}
28-
29-
// Should be replaced by https://github.com/swiftlang/swift-package-manager/pull/8993/files#diff-150cbfd25c6baadfd6b02914bfa68513168ae042a0b01c89bf326b2429ba242a
30-
// when it is merged.
31-
public func expectFileExists(
32-
at path: AbsolutePath,
33-
sourceLocation: SourceLocation = #_sourceLocation,
34-
) {
35-
#expect(
36-
localFileSystem.exists(path),
37-
"Files '\(path)' does not exist.",
38-
sourceLocation: sourceLocation,
39-
)
40-
}
41-
42-
public func expectNoSuchPath(
43-
_ path: AbsolutePath,
44-
sourceLocation: SourceLocation = #_sourceLocation
45-
) {
46-
#expect(
47-
!localFileSystem.exists(path),
48-
"Expected no such path '\(path)'",
49-
sourceLocation: sourceLocation
50-
)
51-
}
52-
53-
public func expectDirectoryExists(
54-
_ path: AbsolutePath,
55-
sourceLocation: SourceLocation = #_sourceLocation
56-
) {
57-
#expect(
58-
localFileSystem.isDirectory(path),
59-
"Expected directory at '\(path)'",
60-
sourceLocation: sourceLocation
61-
)
62-
}
63-
6420
public func expectBuilds(
6521
_ path: AbsolutePath,
6622
buildSystem: BuildSystemProvider.Kind,

0 commit comments

Comments
 (0)