Skip to content

Commit 167144e

Browse files
committed
Tests: Enable more WorkspaceTests on Windows
Many WorkspaceTests instantiate a MockWorkspace, which takes a path as an input. The tests were defining a POSIX-like path as the sandbox directory, however, some assertions were incorrect as, on these *nix like filesystem, the canonical path name and the path were identical. Update the WorkspaceTests expectation accordingly, and add some automated tests to cover the InMemoryFileSystem.
1 parent 49e69c7 commit 167144e

File tree

4 files changed

+380
-453
lines changed

4 files changed

+380
-453
lines changed

Sources/_InternalTestSupport/MockWorkspace.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,14 @@ public final class MockWorkspace {
185185

186186
private func create() async throws {
187187
// Remove the sandbox if present.
188-
try self.fileSystem.removeFileTree(self.sandbox)
188+
if self.fileSystem.exists(self.sandbox) {
189+
try self.fileSystem.removeFileTree(self.sandbox)
190+
}
189191

190192
// Create directories.
191193
try self.fileSystem.createDirectory(self.sandbox, recursive: true)
192-
try self.fileSystem.createDirectory(self.rootsDir)
193-
try self.fileSystem.createDirectory(self.packagesDir)
194+
try self.fileSystem.createDirectory(self.rootsDir, recursive: true)
195+
try self.fileSystem.createDirectory(self.packagesDir, recursive: true)
194196

195197
var manifests: [MockManifestLoader.Key: Manifest] = [:]
196198

Sources/_InternalTestSupport/misc.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public func skipOnWindowsAsTestCurrentlyFails(because reason: String? = nil) thr
285285
} else {
286286
failureCause = ""
287287
}
288-
throw XCTSkip("Test fails on windows\(failureCause)")
288+
throw XCTSkip("Skipping tests on windows\(failureCause)")
289289
#endif
290290
}
291291

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2025 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import Basics
12+
import struct TSCBasic.ByteString
13+
import struct TSCBasic.FileSystemError
14+
15+
import Testing
16+
import _InternalTestSupport
17+
18+
struct InMemoryFileSystemTests {
19+
@Test(
20+
arguments: [
21+
(
22+
path: "/",
23+
recurvise: true,
24+
expectedFiles: [
25+
(p: "/", shouldExist: true),
26+
],
27+
expectError: false,
28+
),
29+
(
30+
path: "/tmp",
31+
recurvise: true,
32+
expectedFiles: [
33+
(p: "/", shouldExist: true),
34+
(p: "/tmp", shouldExist: true),
35+
],
36+
expectError: false,
37+
),
38+
(
39+
path: "/tmp/ws",
40+
recurvise: true,
41+
expectedFiles: [
42+
(p: "/", shouldExist: true),
43+
(p: "/tmp", shouldExist: true),
44+
(p: "/tmp/ws", shouldExist: true),
45+
],
46+
expectError: false,
47+
),
48+
(
49+
path: "/tmp/ws",
50+
recurvise: false,
51+
expectedFiles: [
52+
(p: "/", shouldExist: true),
53+
(p: "/tmp", shouldExist: true),
54+
(p: "/tmp/ws", shouldExist: true),
55+
],
56+
expectError: true,
57+
),
58+
]
59+
)
60+
func creatingDirectoryCreatesInternalFiles(
61+
path: String,
62+
recursive: Bool,
63+
expectedFiles: [(String, Bool) ],
64+
expectError: Bool
65+
) async throws {
66+
let fs = InMemoryFileSystem()
67+
let pathUnderTest = AbsolutePath(path)
68+
69+
func errorMessage(_ pa: AbsolutePath, _ exists: Bool) -> String {
70+
return "Path '\(pa) \(exists ? "should exists, but doesn't" : "should not exist, but does.")"
71+
}
72+
73+
try withKnownIssue {
74+
try fs.createDirectory(pathUnderTest, recursive: recursive)
75+
76+
for (p, shouldExist) in expectedFiles {
77+
let expectedPath = AbsolutePath(p)
78+
#expect(fs.exists(expectedPath) == shouldExist, "\(errorMessage(expectedPath, shouldExist))")
79+
}
80+
} when: {
81+
expectError
82+
}
83+
}
84+
85+
86+
@Test(
87+
arguments: [
88+
"/",
89+
"/tmp",
90+
"/tmp/",
91+
"/something/ws",
92+
"/something/ws/",
93+
"/what/is/this",
94+
"/what/is/this/",
95+
]
96+
)
97+
func callingCreateDirectoryOnAnExistingDirectoryIsSuccessful(path: String) async throws {
98+
let root = AbsolutePath(path)
99+
let fs = InMemoryFileSystem()
100+
101+
#expect(throws: Never.self) {
102+
try fs.createDirectory(root, recursive: true)
103+
}
104+
105+
#expect(throws: Never.self) {
106+
try fs.createDirectory(root.appending("more"), recursive: true)
107+
}
108+
}
109+
110+
struct writeFileContentsTests {
111+
112+
@Test
113+
func testWriteFileContentsSuccessful() async throws {
114+
// GIVEN we have a filesytstem
115+
let fs = InMemoryFileSystem()
116+
// and a path
117+
let pathUnderTest = AbsolutePath("/myFile.zip")
118+
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
119+
120+
// WHEN we write contents to the file
121+
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
122+
123+
// THEN we expect the file to exist
124+
#expect(fs.exists(pathUnderTest), "Path \(pathUnderTest.pathString) does not exists when it should")
125+
}
126+
127+
@Test
128+
func testWritingAFileWithANonExistingParentDirectoryFails() async throws {
129+
// GIVEN we have a filesytstem
130+
let fs = InMemoryFileSystem()
131+
// and a path
132+
let pathUnderTest = AbsolutePath("/tmp/myFile.zip")
133+
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
134+
135+
// WHEN we write contents to the file
136+
// THEn we expect an error to occus
137+
try withKnownIssue {
138+
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
139+
}
140+
141+
// AND we expect the file to not exist
142+
#expect(!fs.exists(pathUnderTest), "Path \(pathUnderTest.pathString) does exists when it should not")
143+
}
144+
145+
@Test
146+
func errorOccursWhenWritingToRootDirectory() async throws {
147+
// GIVEN we have a filesytstem
148+
let fs = InMemoryFileSystem()
149+
// and a path
150+
let pathUnderTest = AbsolutePath("/")
151+
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
152+
153+
// WHEN we write contents to the file
154+
// THEN we expect an error to occur
155+
try withKnownIssue {
156+
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
157+
}
158+
159+
}
160+
161+
@Test
162+
func testErrorOccursIfParentIsNotADirectory() async throws {
163+
// GIVEN we have a filesytstem
164+
let fs = InMemoryFileSystem()
165+
// AND an existing file
166+
let aFile = AbsolutePath("/foo")
167+
try fs.writeFileContents(aFile, bytes: "")
168+
169+
// AND a the path under test that has an existing file as a parent
170+
let pathUnderTest = aFile.appending("myFile")
171+
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
172+
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)
177+
}
178+
179+
}
180+
}
181+
182+
183+
struct testReadFileContentsTests {
184+
@Test
185+
func readingAFileThatDoesNotExistsRaisesAnError()async throws {
186+
// GIVEN we have a filesystem
187+
let fs = InMemoryFileSystem()
188+
189+
// WHEN we read a non-existing file
190+
// THEN an error occurs
191+
try withKnownIssue {
192+
let _ = try fs.readFileContents("/file/does/not/exists")
193+
}
194+
}
195+
196+
@Test
197+
func readingExistingFileReturnsExpectedContents() async throws {
198+
// GIVEN we have a filesytstem
199+
let fs = InMemoryFileSystem()
200+
// AND a file a path
201+
let pathUnderTest = AbsolutePath("/myFile.zip")
202+
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
203+
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
204+
205+
// WHEN we read contents if the file
206+
let actualContents = try fs.readFileContents(pathUnderTest)
207+
208+
// THEN the actual contents should match the expected to match the
209+
#expect(actualContents == expectedContents, "Actual is not as expected")
210+
}
211+
212+
@Test
213+
func readingADirectoryFailsWithAnError() async throws {
214+
// GIVEN we have a filesytstem
215+
let fs = InMemoryFileSystem()
216+
// AND a file a path
217+
let pathUnderTest = AbsolutePath("/myFile.zip")
218+
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
219+
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
220+
221+
// WHEN we read the contents of a directory
222+
// THEN we expect a failure to occur
223+
withKnownIssue {
224+
let _ = try fs.readFileContents(pathUnderTest.parentDirectory)
225+
}
226+
}
227+
}
228+
229+
}

0 commit comments

Comments
 (0)