@@ -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,13 +57,15 @@ 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,
@@ -70,13 +75,15 @@ public func requireFileExists(
7075public func expectFileDoesNotExists (
7176 at path: AbsolutePath,
7277 _ comment: Comment? = nil ,
78+ fileSystem fs: FileSystem = localFileSystem,
7379 sourceLocation: SourceLocation = #_sourceLocation,
7480) {
7581 #expect(
76- !localFileSystem . exists ( path) ,
82+ !fs . exists ( path) ,
7783 fileErrorMessage (
7884 at: path,
7985 prefix: comment,
86+ fileSystem: fs,
8087 comment: " File: ' \( path) ' was not expected to exist, but does. " ,
8188 ) ,
8289 sourceLocation: sourceLocation,
@@ -85,13 +92,15 @@ public func expectFileDoesNotExists(
8592public func requireFileDoesNotExists (
8693 at path: AbsolutePath,
8794 _ comment: Comment? = nil ,
95+ fileSystem fs: FileSystem = localFileSystem,
8896 sourceLocation: SourceLocation = #_sourceLocation,
8997) throws {
9098 try #require(
91- !localFileSystem . exists ( path) ,
99+ !fs . exists ( path) ,
92100 fileErrorMessage (
93101 at: path,
94102 prefix: comment,
103+ fileSystem: fs,
95104 comment: " File: ' \( path) ' was not expected to exist, but does. " ,
96105 ) ,
97106 sourceLocation: sourceLocation,
@@ -101,6 +110,7 @@ public func requireFileDoesNotExists(
101110public func expectFileIsExecutable(
102111 at fixturePath: AbsolutePath,
103112 _ comment: Comment? = nil ,
113+ fileSystem fs: FileSystem = localFileSystem,
104114 sourceLocation: SourceLocation = #_sourceLocation,
105115) {
106116 let commentPrefix =
@@ -110,15 +120,16 @@ public func expectFileIsExecutable(
110120 " "
111121 }
112122 #expect(
113- localFileSystem . isExecutableFile ( fixturePath) ,
123+ fs . isExecutableFile ( fixturePath) ,
114124 " \( commentPrefix) File ' \( fixturePath) ' expected to be executable, but is not. " ,
115125 sourceLocation: sourceLocation,
116126 )
117127}
118128
119129private func directoryExistsErrorMessage (
120130 for path: AbsolutePath,
121- comment: Comment?
131+ comment: Comment? ,
132+ fileSystem fs: FileSystem = localFileSystem,
122133) - > Comment {
123134 let commentPrefix =
124135 if let comment {
@@ -128,7 +139,7 @@ private func directoryExistsErrorMessage(
128139 }
129140 let msgSuffix : String
130141 do {
131- msgSuffix = try " Directory contents: \( localFileSystem . getDirectoryContents ( path) ) "
142+ msgSuffix = try " Directory contents: \( fs . getDirectoryContents ( path) ) "
132143 } catch {
133144 msgSuffix = " "
134145 }
@@ -138,40 +149,76 @@ private func directoryExistsErrorMessage(
138149public func requireDirectoryExists(
139150 at path: AbsolutePath,
140151 _ comment: Comment? = nil ,
141- fileSystem: FileSystem = localFileSystem,
152+ fileSystem fs : FileSystem = localFileSystem,
142153 sourceLocation: SourceLocation = #_sourceLocation,
143154) throws {
144155 try #require(
145- localFileSystem . isDirectory ( path) ,
156+ fs . isDirectory ( path) ,
146157 directoryExistsErrorMessage ( for: path, comment: comment) ,
147158 sourceLocation: sourceLocation,
148159 )
149160}
150161
162+ @available ( * , deprecated, message: " use `expectDirectoryExist` instead. " )
151163public func expectDirectoryExists (
152164 at path: AbsolutePath,
153165 _ comment: Comment? = nil ,
166+ fileSystem fs: FileSystem = localFileSystem,
167+ sourceLocation: SourceLocation = #_sourceLocation,
168+ ) {
169+ expectDirectoryExists (
170+ at: path,
171+ comment,
172+ fileSystem: fs,
173+ sourceLocation: sourceLocation,
174+ )
175+ }
176+
177+ public func expectDirectoryExist (
178+ at path: AbsolutePath,
179+ _ comment: Comment? = nil ,
180+ fileSystem fs: FileSystem = localFileSystem,
154181 sourceLocation: SourceLocation = #_sourceLocation,
155182) {
156183 #expect(
157- localFileSystem. isDirectory ( path) ,
158- directoryExistsErrorMessage ( for: path, comment: comment) ,
184+ fs. isDirectory ( path) ,
185+ directoryExistsErrorMessage ( for: path, comment: comment, fileSystem: fs) ,
186+ sourceLocation: sourceLocation,
187+ )
188+ }
189+
190+ public func requireDirectoryDoesNotExist (
191+ at path: AbsolutePath,
192+ fileSystem fs: FileSystem = localFileSystem,
193+ sourceLocation: SourceLocation = #_sourceLocation,
194+ ) throws {
195+ let msgSuffix : String
196+ do {
197+ msgSuffix = try " Directory contents: \( fs. getDirectoryContents ( path) ) "
198+ } catch {
199+ msgSuffix = " "
200+ }
201+ try #require(
202+ !fs. isDirectory ( path) ,
203+ " Directory exists unexpectedly: ' \( path) '. \( msgSuffix) " ,
159204 sourceLocation: sourceLocation,
160205 )
161206}
162207
208+
163209public func expectDirectoryDoesNotExist (
164210 at path: AbsolutePath,
211+ fileSystem fs: FileSystem = localFileSystem,
165212 sourceLocation: SourceLocation = #_sourceLocation,
166213) {
167214 let msgSuffix : String
168215 do {
169- msgSuffix = try " Directory contents: \( localFileSystem . getDirectoryContents ( path) ) "
216+ msgSuffix = try " Directory contents: \( fs . getDirectoryContents ( path) ) "
170217 } catch {
171218 msgSuffix = " "
172219 }
173220 #expect(
174- !localFileSystem . isDirectory ( path) ,
221+ !fs . isDirectory ( path) ,
175222 " Directory exists unexpectedly: ' \( path) '. \( msgSuffix) " ,
176223 sourceLocation: sourceLocation,
177224 )
@@ -181,10 +228,11 @@ public func expectDirectoryDoesNotExist(
181228package func expectDirectoryContainsFile(
182229 dir: AbsolutePath,
183230 filename: String,
231+ fileSystem fs: FileSystem = localFileSystem,
184232 sourceLocation: SourceLocation = #_sourceLocation,
185233) {
186234 do {
187- for entry in try walk ( dir) {
235+ for entry in try walk ( dir, fileSystem : fs ) {
188236 if entry. basename == filename { return }
189237 }
190238 } catch {
0 commit comments