@@ -17,6 +17,7 @@ import Testing
1717fileprivate func fileErrorMessage(
1818 at path: AbsolutePath ,
1919 prefix: Comment ? ,
20+ fileSystem fs: FileSystem = localFileSystem,
2021 comment: Comment ,
2122) -> Comment {
2223 let commentPrefix =
@@ -29,22 +30,24 @@ fileprivate func fileErrorMessage(
2930 let commentSuffix : String
3031 do {
3132 let parentDir = path. parentDirectory
32- commentSuffix = try " Directory contents of \( parentDir) : \( localFileSystem . getDirectoryContents ( parentDir) ) . "
33+ commentSuffix = try " Directory contents of \( parentDir) : \( fs . getDirectoryContents ( parentDir) ) . "
3334 } catch {
3435 commentSuffix = " "
3536 }
36- return Comment ( " \( commentPrefix) \( comment) \( commentSuffix) . " )
37+ return Comment ( " \( commentPrefix) \( comment) \( commentSuffix) " )
3738}
3839public func expectFileExists(
3940 at path: AbsolutePath ,
4041 _ comment: Comment ? = nil ,
42+ fileSystem fs: FileSystem = localFileSystem,
4143 sourceLocation: SourceLocation = #_sourceLocation,
4244) {
4345 #expect(
44- localFileSystem . exists ( path) ,
46+ fs . exists ( path) ,
4547 fileErrorMessage (
4648 at: path,
4749 prefix: comment,
50+ fileSystem: fs,
4851 comment: " File ' \( path) ' does not exist. " ,
4952 ) ,
5053 sourceLocation: sourceLocation,
@@ -54,44 +57,81 @@ public func expectFileExists(
5457public func requireFileExists (
5558 at path: AbsolutePath,
5659 _ comment: Comment? = nil ,
60+ fileSystem fs: FileSystem = localFileSystem,
5761 sourceLocation: SourceLocation = #_sourceLocation,
5862) throws {
5963 try #require(
60- localFileSystem . exists ( path) ,
64+ fs . exists ( path) ,
6165 fileErrorMessage (
6266 at: path,
6367 prefix: comment,
68+ fileSystem: fs,
6469 comment: " File ' \( path) ' does not exist. " ,
6570 ) ,
6671 sourceLocation: sourceLocation,
6772 )
6873}
6974
75+ @available ( * , deprecated, message: " use `expectFileDoesNotExist` instead " )
7076public func expectFileDoesNotExists (
7177 at path: AbsolutePath,
7278 _ comment: Comment? = nil ,
79+ fileSystem fs: FileSystem = localFileSystem,
80+ sourceLocation: SourceLocation = #_sourceLocation,
81+ ) {
82+ expectFileDoesNotExist (
83+ at: path,
84+ comment,
85+ fileSystem: fs,
86+ sourceLocation: sourceLocation,
87+ )
88+ }
89+
90+ public func expectFileDoesNotExist (
91+ at path: AbsolutePath,
92+ _ comment: Comment? = nil ,
93+ fileSystem fs: FileSystem = localFileSystem,
7394 sourceLocation: SourceLocation = #_sourceLocation,
7495) {
7596 #expect(
76- !localFileSystem . exists ( path) ,
97+ !fs . exists ( path) ,
7798 fileErrorMessage (
7899 at: path,
79100 prefix: comment,
101+ fileSystem: fs,
80102 comment: " File: ' \( path) ' was not expected to exist, but does. " ,
81103 ) ,
82104 sourceLocation: sourceLocation,
83105 )
84106}
107+
108+ @available ( * , deprecated, message: " use `requireFileDoesNotExist` instead " )
85109public func requireFileDoesNotExists (
86110 at path: AbsolutePath,
87111 _ comment: Comment? = nil ,
112+ fileSystem fs: FileSystem = localFileSystem,
113+ sourceLocation: SourceLocation = #_sourceLocation,
114+ ) throws {
115+ try requireFileDoesNotExist (
116+ at: path,
117+ comment,
118+ fileSystem: fs,
119+ sourceLocation: sourceLocation,
120+ )
121+ }
122+
123+ public func requireFileDoesNotExist (
124+ at path: AbsolutePath,
125+ _ comment: Comment? = nil ,
126+ fileSystem fs: FileSystem = localFileSystem,
88127 sourceLocation: SourceLocation = #_sourceLocation,
89128) throws {
90129 try #require(
91- !localFileSystem . exists ( path) ,
130+ !fs . exists ( path) ,
92131 fileErrorMessage (
93132 at: path,
94133 prefix: comment,
134+ fileSystem: fs,
95135 comment: " File: ' \( path) ' was not expected to exist, but does. " ,
96136 ) ,
97137 sourceLocation: sourceLocation,
@@ -101,6 +141,7 @@ public func requireFileDoesNotExists(
101141public func expectFileIsExecutable(
102142 at fixturePath: AbsolutePath,
103143 _ comment: Comment? = nil ,
144+ fileSystem fs: FileSystem = localFileSystem,
104145 sourceLocation: SourceLocation = #_sourceLocation,
105146) {
106147 let commentPrefix =
@@ -110,15 +151,16 @@ public func expectFileIsExecutable(
110151 " "
111152 }
112153 #expect(
113- localFileSystem . isExecutableFile ( fixturePath) ,
154+ fs . isExecutableFile ( fixturePath) ,
114155 " \( commentPrefix) File ' \( fixturePath) ' expected to be executable, but is not. " ,
115156 sourceLocation: sourceLocation,
116157 )
117158}
118159
119160private func directoryExistsErrorMessage (
120161 for path: AbsolutePath,
121- comment: Comment?
162+ comment: Comment? ,
163+ fileSystem fs: FileSystem = localFileSystem,
122164) - > Comment {
123165 let commentPrefix =
124166 if let comment {
@@ -128,7 +170,7 @@ private func directoryExistsErrorMessage(
128170 }
129171 let msgSuffix : String
130172 do {
131- msgSuffix = try " Directory contents: \( localFileSystem . getDirectoryContents ( path) ) "
173+ msgSuffix = try " Directory contents: \( fs . getDirectoryContents ( path) ) "
132174 } catch {
133175 msgSuffix = " "
134176 }
@@ -138,11 +180,11 @@ private func directoryExistsErrorMessage(
138180public func requireDirectoryExists(
139181 at path: AbsolutePath,
140182 _ comment: Comment? = nil ,
141- fileSystem: FileSystem = localFileSystem,
183+ fileSystem fs : FileSystem = localFileSystem,
142184 sourceLocation: SourceLocation = #_sourceLocation,
143185) throws {
144186 try #require(
145- localFileSystem . isDirectory ( path) ,
187+ fs . isDirectory ( path) ,
146188 directoryExistsErrorMessage ( for: path, comment: comment) ,
147189 sourceLocation: sourceLocation,
148190 )
@@ -151,27 +193,48 @@ public func requireDirectoryExists(
151193public func expectDirectoryExists (
152194 at path: AbsolutePath,
153195 _ comment: Comment? = nil ,
196+ fileSystem fs: FileSystem = localFileSystem,
154197 sourceLocation: SourceLocation = #_sourceLocation,
155198) {
156199 #expect(
157- localFileSystem . isDirectory ( path) ,
158- directoryExistsErrorMessage ( for: path, comment: comment) ,
200+ fs . isDirectory ( path) ,
201+ directoryExistsErrorMessage ( for: path, comment: comment, fileSystem : fs ) ,
159202 sourceLocation: sourceLocation,
160203 )
161204}
162205
206+ public func requireDirectoryDoesNotExist (
207+ at path: AbsolutePath,
208+ fileSystem fs: FileSystem = localFileSystem,
209+ sourceLocation: SourceLocation = #_sourceLocation,
210+ ) throws {
211+ let msgSuffix : String
212+ do {
213+ msgSuffix = try " Directory contents: \( fs. getDirectoryContents ( path) ) "
214+ } catch {
215+ msgSuffix = " "
216+ }
217+ try #require(
218+ !fs. isDirectory ( path) ,
219+ " Directory exists unexpectedly: ' \( path) '. \( msgSuffix) " ,
220+ sourceLocation: sourceLocation,
221+ )
222+ }
223+
224+
163225public func expectDirectoryDoesNotExist (
164226 at path: AbsolutePath,
227+ fileSystem fs: FileSystem = localFileSystem,
165228 sourceLocation: SourceLocation = #_sourceLocation,
166229) {
167230 let msgSuffix : String
168231 do {
169- msgSuffix = try " Directory contents: \( localFileSystem . getDirectoryContents ( path) ) "
232+ msgSuffix = try " Directory contents: \( fs . getDirectoryContents ( path) ) "
170233 } catch {
171234 msgSuffix = " "
172235 }
173236 #expect(
174- !localFileSystem . isDirectory ( path) ,
237+ !fs . isDirectory ( path) ,
175238 " Directory exists unexpectedly: ' \( path) '. \( msgSuffix) " ,
176239 sourceLocation: sourceLocation,
177240 )
@@ -181,10 +244,11 @@ public func expectDirectoryDoesNotExist(
181244package func expectDirectoryContainsFile(
182245 dir: AbsolutePath,
183246 filename: String,
247+ fileSystem fs: FileSystem = localFileSystem,
184248 sourceLocation: SourceLocation = #_sourceLocation,
185249) {
186250 do {
187- for entry in try walk ( dir) {
251+ for entry in try walk ( dir, fileSystem : fs ) {
188252 if entry. basename == filename { return }
189253 }
190254 } catch {
0 commit comments