@@ -20,41 +20,38 @@ import Testing
20
20
/// - sourceLocation: The source location where the expectation is made.
21
21
public func expectFileExists(
22
22
at path: AbsolutePath ,
23
+ _ comment: Comment ? = nil ,
23
24
sourceLocation: SourceLocation = #_sourceLocation,
24
25
) {
25
26
#expect(
26
27
localFileSystem. exists ( path) ,
27
- " Files ' \( path) ' does not exist. " ,
28
+ wrapMessage (
29
+ " Files ' \( path) ' does not exist. " ,
30
+ comment: comment,
31
+ directoryPath: path. parentDirectory
32
+ ) ,
28
33
sourceLocation: sourceLocation,
29
34
)
30
35
}
31
36
32
37
/// Verifies that a file does not exist at the specified path.
33
38
///
34
39
/// - Parameters:
35
- /// - fixturePath : The absolute path to check for file non-existence.
40
+ /// - path : The absolute path to check for file non-existence.
36
41
/// - comment: An optional comment to include in the failure message.
37
42
/// - sourceLocation: The source location where the expectation is made.
38
43
public func expectFileDoesNotExists (
39
44
at path: AbsolutePath,
40
45
_ comment: Comment? = nil ,
41
46
sourceLocation: SourceLocation = #_sourceLocation,
42
47
) {
43
- let commentPrefix =
44
- if let comment {
45
- " \( comment) : "
46
- } else {
47
- " "
48
- }
49
- let msgSuffix : String
50
- do {
51
- msgSuffix = try " Directory contents: \( localFileSystem. getDirectoryContents ( path. parentDirectory) ) "
52
- } catch {
53
- msgSuffix = " "
54
- }
55
48
#expect(
56
49
!localFileSystem. exists ( path) ,
57
- " \( commentPrefix) File: ' \( path) ' was not expected to exist, but does. \( msgSuffix) ) " ,
50
+ wrapMessage (
51
+ " File: ' \( path) ' was not expected to exist, but does. " ,
52
+ comment: comment,
53
+ directoryPath: path. parentDirectory
54
+ ) ,
58
55
sourceLocation: sourceLocation,
59
56
)
60
57
}
@@ -70,15 +67,9 @@ public func expectFileIsExecutable(
70
67
_ comment: Comment? = nil ,
71
68
sourceLocation: SourceLocation = #_sourceLocation,
72
69
) {
73
- let commentPrefix =
74
- if let comment {
75
- " \( comment) : "
76
- } else {
77
- " "
78
- }
79
70
#expect(
80
71
localFileSystem. isExecutableFile ( fixturePath) ,
81
- " \( commentPrefix ) File ' \( fixturePath) ' expected to be executable, but is not. " ,
72
+ wrapMessage ( " File ' \( fixturePath) ' expected to be executable, but is not. " , comment : comment ) ,
82
73
sourceLocation: sourceLocation,
83
74
)
84
75
}
@@ -90,17 +81,12 @@ public func expectFileIsExecutable(
90
81
/// - sourceLocation: The source location where the expectation is made.
91
82
public func expectDirectoryExists (
92
83
at path: AbsolutePath,
84
+ _ comment: Comment? = nil ,
93
85
sourceLocation: SourceLocation = #_sourceLocation,
94
86
) {
95
- let msgSuffix : String
96
- do {
97
- msgSuffix = try " Directory contents: \( localFileSystem. getDirectoryContents ( path) ) "
98
- } catch {
99
- msgSuffix = " "
100
- }
101
87
#expect(
102
88
localFileSystem. isDirectory ( path) ,
103
- " Expected directory doesn't exist: ' \( path) '. \( msgSuffix ) " ,
89
+ wrapMessage ( " Expected directory doesn't exist: ' \( path) ' " , comment : comment , directoryPath : path ) ,
104
90
sourceLocation: sourceLocation,
105
91
)
106
92
}
@@ -112,21 +98,47 @@ let msgSuffix: String
112
98
/// - sourceLocation: The source location where the expectation is made.
113
99
public func expectDirectoryDoesNotExist (
114
100
at path: AbsolutePath,
101
+ _ comment: Comment? = nil ,
115
102
sourceLocation: SourceLocation = #_sourceLocation,
116
103
) {
117
- let msgSuffix : String
118
- do {
119
- msgSuffix = try " Directory contents: \( localFileSystem. getDirectoryContents ( path) ) "
120
- } catch {
121
- msgSuffix = " "
122
- }
123
104
#expect(
124
105
!localFileSystem. isDirectory ( path) ,
125
- " Directory exists unexpectedly: ' \( path) '. \( msgSuffix ) " ,
106
+ wrapMessage ( " Directory exists unexpectedly: ' \( path) ' " , comment : comment , directoryPath : path ) ,
126
107
sourceLocation: sourceLocation,
127
108
)
128
109
}
129
110
111
+ /// Wraps a message with an optional comment prefix and directory contents suffix.
112
+ ///
113
+ /// - Parameters:
114
+ /// - message: The base message to wrap.
115
+ /// - comment: An optional comment to prefix the message with.
116
+ /// - directoryPath: An optional path to a folder whose contents will be appended to the message.
117
+ /// - Returns: The formatted message with prefix and suffix.
118
+ private func wrapMessage(
119
+ _ message: Comment,
120
+ comment: Comment? = nil ,
121
+ directoryPath: AbsolutePath? = nil
122
+ ) - > Comment {
123
+ let commentPrefix =
124
+ if let comment {
125
+ " \( comment) : "
126
+ } else {
127
+ " "
128
+ }
129
+
130
+ var msgSuffix = " "
131
+ if let directoryPath {
132
+ do {
133
+ msgSuffix = try " Directory contents: \( localFileSystem. getDirectoryContents ( directoryPath) ) "
134
+ } catch {
135
+ // Silently ignore errors when getting directory contents
136
+ }
137
+ }
138
+
139
+ return " \( commentPrefix) \( message) \( msgSuffix) "
140
+ }
141
+
130
142
/// Expects that the expression throws a CommandExecutionError and passes it to the provided throwing error handler.
131
143
/// - Parameters:
132
144
/// - expression: The expression expected to throw
0 commit comments