Skip to content

Commit ed31e6e

Browse files
authored
Tests: Enable more WorkspaceTests on Windows (#8457)
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 (which is commented out as a crash occurs in the Linux Platform CI build with Swift Testing tests). Relates: #8433 rdar://148248105
1 parent e0afabb commit ed31e6e

File tree

5 files changed

+390
-454
lines changed

5 files changed

+390
-454
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/XCTAssertHelpers.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public func XCTAssertEqual<T:Equatable, U:Equatable> (_ lhs:(T,U), _ rhs:(T,U),
4646

4747
public func XCTSkipIfCI(file: StaticString = #filePath, line: UInt = #line) throws {
4848
// TODO: is this actually the right variable now?
49-
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil {
49+
if isInCiEnvironment {
5050
throw XCTSkip("Skipping because the test is being run on CI", file: file, line: line)
5151
}
5252
}

Sources/_InternalTestSupport/misc.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import enum TSCUtility.Git
3939
@_exported import func TSCTestSupport.systemQuietly
4040
@_exported import enum TSCTestSupport.StringPattern
4141

42+
public let isInCiEnvironment = ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil
43+
4244
/// Test helper utility for executing a block with a temporary directory.
4345
public func testWithTemporaryDirectory(
4446
function: StaticString = #function,
@@ -294,7 +296,7 @@ public func skipOnWindowsAsTestCurrentlyFails(because reason: String? = nil) thr
294296
} else {
295297
failureCause = ""
296298
}
297-
throw XCTSkip("Test fails on windows\(failureCause)")
299+
throw XCTSkip("Skipping tests on windows\(failureCause)")
298300
#endif
299301
}
300302

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

0 commit comments

Comments
 (0)