10
10
11
11
import Basics
12
12
import Testing
13
+ import TSCTestSupport
13
14
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.
14
22
public func expectFileExists(
15
23
at path: AbsolutePath ,
16
24
sourceLocation: SourceLocation = #_sourceLocation,
17
25
) {
18
-
19
26
#expect(
20
27
localFileSystem. exists ( path) ,
21
28
" Files ' \( path) ' does not exist. " ,
22
29
sourceLocation: sourceLocation,
23
30
)
24
31
}
25
32
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
26
66
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.
27
74
public func expectThrowsCommandExecutionError< T> (
28
75
_ expression: @autoclosure ( ) async throws -> T,
29
76
_ message: @autoclosure ( ) - > Comment = " " ,
@@ -42,6 +89,12 @@ public func expectThrowsCommandExecutionError<T>(
42
89
}
43
90
44
91
/// 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.
45
98
public func expectAsyncThrowsError< T> (
46
99
_ expression: @autoclosure ( ) async throws -> T,
47
100
_ message: @autoclosure ( ) - > Comment? = nil ,
@@ -55,3 +108,26 @@ public func expectAsyncThrowsError<T>(
55
108
errorHandler ( error)
56
109
}
57
110
}
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
+
0 commit comments