Skip to content

Commit f13f2a4

Browse files
authored
Fixup InMemoryFilesSystemTests to use #expect(throws:) vs withKnownIssue (#9481)
The `InMemoryFilesSystemTests` were using `withKnownIssue` to validate that the file system was throwing errors when expected. This was polluting the test report with tests that appeared to have known issues, but were actually working as intended. Use `#expect(throws:)` to validate these functions are throwing the appropriate error as expected.
1 parent 10af69e commit f13f2a4

File tree

1 file changed

+35
-94
lines changed

1 file changed

+35
-94
lines changed

Tests/BasicsTests/FileSystem/InMemoryFilesSystemTests.swift

Lines changed: 35 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,28 @@ import struct TSCBasic.FileSystemError
1616
import Testing
1717
import _InternalTestSupport
1818

19-
#if os(Linux)
20-
let isLinux = true
21-
#else
22-
let isLinux = false
23-
#endif
24-
2519
@Suite(
2620
.tags(
2721
.TestSize.small,
2822
.Platform.FileSystem,
2923
),
3024
)
3125
struct InMemoryFileSystemTests {
26+
private static let testFileContent = ByteString([0xAA, 0xBB, 0xCC])
27+
3228
@Test(
3329
arguments: [
3430
(
3531
path: "/",
36-
recurvise: true,
32+
recursive: true,
3733
expectedFiles: [
3834
(p: "/", shouldExist: true)
3935
],
4036
expectError: false
4137
),
4238
(
4339
path: "/tmp",
44-
recurvise: true,
40+
recursive: true,
4541
expectedFiles: [
4642
(p: "/", shouldExist: true),
4743
(p: "/tmp", shouldExist: true),
@@ -50,7 +46,7 @@ struct InMemoryFileSystemTests {
5046
),
5147
(
5248
path: "/tmp/ws",
53-
recurvise: true,
49+
recursive: true,
5450
expectedFiles: [
5551
(p: "/", shouldExist: true),
5652
(p: "/tmp", shouldExist: true),
@@ -60,7 +56,7 @@ struct InMemoryFileSystemTests {
6056
),
6157
(
6258
path: "/tmp/ws",
63-
recurvise: false,
59+
recursive: false,
6460
expectedFiles: [
6561
(p: "/", shouldExist: true),
6662
(p: "/tmp", shouldExist: true),
@@ -83,22 +79,24 @@ struct InMemoryFileSystemTests {
8379
return
8480
"Path '\(pa) \(exists ? "should exists, but doesn't" : "should not exist, but does.")"
8581
}
86-
87-
try withKnownIssue {
88-
try fs.createDirectory(pathUnderTest, recursive: recursive)
89-
90-
for (p, shouldExist) in expectedFiles {
91-
let expectedPath = AbsolutePath(p)
92-
#expect(
93-
fs.exists(expectedPath) == shouldExist,
94-
"\(errorMessage(expectedPath, shouldExist))")
82+
if expectError {
83+
#expect(throws: FileSystemError.self) {
84+
try fs.createDirectory(pathUnderTest, recursive: recursive)
85+
}
86+
} else {
87+
#expect(throws: Never.self) {
88+
try fs.createDirectory(pathUnderTest, recursive: recursive)
89+
90+
for (p, shouldExist) in expectedFiles {
91+
let expectedPath = AbsolutePath(p)
92+
#expect(
93+
fs.exists(expectedPath) == shouldExist,
94+
"\(errorMessage(expectedPath, shouldExist))")
95+
}
9596
}
96-
} when: {
97-
expectError
9897
}
9998
}
10099

101-
102100
@Test(
103101
arguments: [
104102
"/",
@@ -127,106 +125,73 @@ struct InMemoryFileSystemTests {
127125

128126
@Test
129127
func testWriteFileContentsSuccessful() async throws {
130-
// GIVEN we have a filesytstem
131128
let fs = InMemoryFileSystem()
132-
// and a path
133129
let pathUnderTest = AbsolutePath("/myFile.zip")
134-
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
135130

136-
// WHEN we write contents to the file
137-
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
131+
try fs.writeFileContents(pathUnderTest, bytes: testFileContent)
138132

139-
// THEN we expect the file to exist
140133
#expect(
141134
fs.exists(pathUnderTest),
142135
"Path \(pathUnderTest.pathString) does not exists when it should")
143136
}
144137

145138
@Test
146139
func testWritingAFileWithANonExistingParentDirectoryFails() async throws {
147-
// GIVEN we have a filesytstem
148140
let fs = InMemoryFileSystem()
149-
// and a path
150141
let pathUnderTest = AbsolutePath("/tmp/myFile.zip")
151-
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
152142

153-
// WHEN we write contents to the file
154-
// THEn we expect an error to occus
155-
withKnownIssue {
156-
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
143+
#expect(throws: FileSystemError.self) {
144+
try fs.writeFileContents(pathUnderTest, bytes: testFileContent)
157145
}
158146

159-
// AND we expect the file to not exist
160147
#expect(
161148
!fs.exists(pathUnderTest),
162149
"Path \(pathUnderTest.pathString) does exists when it should not")
163150
}
164151

165152
@Test
166153
func errorOccursWhenWritingToRootDirectory() async throws {
167-
// GIVEN we have a filesytstem
168154
let fs = InMemoryFileSystem()
169-
// and a path
170155
let pathUnderTest = AbsolutePath("/")
171-
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
172156

173-
// WHEN we write contents to the file
174-
// THEN we expect an error to occur
175-
withKnownIssue {
176-
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
157+
#expect(throws: FileSystemError.self) {
158+
try fs.writeFileContents(pathUnderTest, bytes: testFileContent)
177159
}
178-
179160
}
180161

181162
@Test
182163
func testErrorOccursIfParentIsNotADirectory() async throws {
183-
// GIVEN we have a filesytstem
184164
let fs = InMemoryFileSystem()
185-
// AND an existing file
186165
let aFile = AbsolutePath("/foo")
187166
try fs.writeFileContents(aFile, bytes: "")
188167

189-
// AND a the path under test that has an existing file as a parent
190168
let pathUnderTest = aFile.appending("myFile")
191-
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
192169

193-
// WHEN we write contents to the file
194-
// THEN we expect an error to occur
195-
withKnownIssue {
196-
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
170+
#expect(throws: FileSystemError.self) {
171+
try fs.writeFileContents(pathUnderTest, bytes: testFileContent)
197172
}
198173

199174
}
200175
}
201176

202-
203177
struct testReadFileContentsTests {
204178
@Test
205179
func readingAFileThatDoesNotExistsRaisesAnError() async throws {
206-
// GIVEN we have a filesystem
207180
let fs = InMemoryFileSystem()
208-
209-
// WHEN we read a non-existing file
210-
// THEN an error occurs
211-
withKnownIssue {
212-
let _ = try fs.readFileContents("/file/does/not/exists")
181+
#expect(throws: FileSystemError.self) {
182+
try fs.readFileContents("/file/does/not/exists")
213183
}
214184
}
215185

216186
@Test
217187
func readingExistingFileReturnsExpectedContents() async throws {
218-
// GIVEN we have a filesytstem
219188
let fs = InMemoryFileSystem()
220-
// AND a file a path
221189
let pathUnderTest = AbsolutePath("/myFile.zip")
222-
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
223-
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
190+
try fs.writeFileContents(pathUnderTest, bytes: testFileContent)
224191

225-
// WHEN we read contents if the file
226192
let actualContents = try fs.readFileContents(pathUnderTest)
227193

228-
// THEN the actual contents should match the expected to match the
229-
#expect(actualContents == expectedContents, "Actual is not as expected")
194+
#expect(actualContents == testFileContent, "Actual is not as expected")
230195
}
231196

232197
@Suite(
@@ -236,50 +201,37 @@ struct InMemoryFileSystemTests {
236201
)
237202
struct ChangeCurrentWorkingDirectoryTests {
238203
func errorOccursWhenChangingDirectoryToAFile() async throws {
239-
// GIVEN we have a file
240204
let fileUnderTest = AbsolutePath.root.appending(components: "Foo", "Bar", "baz.txt")
241205

242-
// AND filesytstem
243206
let fs = InMemoryFileSystem(
244207
emptyFiles: [
245208
fileUnderTest.pathString
246209
]
247210
)
248211

249-
// WHEN We change directory to this file
250-
// THEN An error occurs
251212
#expect(throws: FileSystemError(.notDirectory, fileUnderTest)) {
252213
try fs.changeCurrentWorkingDirectory(to: fileUnderTest)
253214
}
254215
}
255216

256217
func errorOccursWhenChangingDirectoryDoesNotExists() async throws {
257-
258-
// GIVEN we have a filesytstem
259-
let fs = InMemoryFileSystem(
260-
)
218+
let fs = InMemoryFileSystem()
261219
let nonExistingDirectory = AbsolutePath.root.appending(components: "does-not-exists")
262220

263-
// WHEN We change directory to this file
264-
// THEN An error occurs
265221
#expect(throws: FileSystemError(.noEntry, nonExistingDirectory)) {
266222
try fs.changeCurrentWorkingDirectory(to: nonExistingDirectory)
267223
}
268224
}
269225

270226
func changinDirectoryToTheParentOfAnExistingFileIsSuccessful() async throws {
271-
// GIVEN we have a file
272227
let fileUnderTest = AbsolutePath.root.appending(components: "Foo", "Bar", "baz.txt")
273228

274-
// AND filesytstem
275229
let fs = InMemoryFileSystem(
276230
emptyFiles: [
277231
fileUnderTest.pathString
278232
]
279233
)
280234

281-
// WHEN We change directory to this file
282-
// THEN do not expect any errors
283235
#expect(throws: Never.self) {
284236
try fs.changeCurrentWorkingDirectory(to: fileUnderTest.parentDirectory)
285237
}
@@ -293,39 +245,28 @@ struct InMemoryFileSystemTests {
293245
)
294246
struct GetDirectoryContentsTests {
295247
func returnsExpectedItemsWhenDirectoryHasASingleFile() async throws {
296-
// GIVEN we have a file
297248
let fileUnderTest = AbsolutePath.root.appending(components: "Foo", "Bar", "baz.txt")
298-
299-
// AND filesytstem
300249
let fs = InMemoryFileSystem(
301250
emptyFiles: [
302251
fileUnderTest.pathString
303252
]
304253
)
305254

306-
// WHEN We change directory to this file
307-
// AND we retrieve the directory contents
308255
try fs.changeCurrentWorkingDirectory(to: fileUnderTest.parentDirectory)
309256
let contents = try fs.getDirectoryContents(fileUnderTest.parentDirectory)
310257

311-
// THEN we expect the correct list of items
312258
#expect(["baz.txt"] == contents)
313259
}
314260
}
315261

316262
@Test
317263
func readingADirectoryFailsWithAnError() async throws {
318-
// GIVEN we have a filesytstem
319264
let fs = InMemoryFileSystem()
320-
// AND a file a path
321265
let pathUnderTest = AbsolutePath("/myFile.zip")
322-
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
323-
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
266+
try fs.writeFileContents(pathUnderTest, bytes: testFileContent)
324267

325-
// WHEN we read the contents of a directory
326-
// THEN we expect a failure to occur
327-
withKnownIssue {
328-
let _ = try fs.readFileContents(pathUnderTest.parentDirectory)
268+
#expect(throws: FileSystemError.self) {
269+
try fs.readFileContents(pathUnderTest.parentDirectory)
329270
}
330271
}
331272
}

0 commit comments

Comments
 (0)