Skip to content

Commit 0869c3c

Browse files
committed
updates
1 parent 4d7dc23 commit 0869c3c

File tree

3 files changed

+141
-16
lines changed

3 files changed

+141
-16
lines changed

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

Tests/BasicsTests/FileSystem/InMemoryFilesSystemTests.swift

+126-10
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
import Basics
1212
import struct TSCBasic.ByteString
13+
import struct TSCBasic.FileSystemError
1314

1415
import Testing
16+
import _InternalTestSupport
1517

1618
struct InMemoryFileSystemTests {
1719
@Test(
@@ -43,6 +45,16 @@ struct InMemoryFileSystemTests {
4345
],
4446
expectError: false,
4547
),
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+
),
4658
]
4759
)
4860
func creatingDirectoryCreatesInternalFiles(
@@ -95,19 +107,123 @@ struct InMemoryFileSystemTests {
95107
}
96108
}
97109

98-
@Test
99-
func testWriteFileContents() async throws {
100-
let fs = InMemoryFileSystem()
101-
let binaryPath = AbsolutePath("/tmp/ws/binary.zip")
102-
let expectedContents = ByteString([0xAA, 0xBB, 0xCC])
110+
struct writeFileContentsTests {
103111

104-
try #require(!fs.exists(binaryPath), "Path \(binaryPath.pathString) should not exist, but it does")
105-
try fs.writeFileContents(binaryPath, bytes: expectedContents)
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])
106119

107-
#expect(fs.exists(binaryPath), "Path \(binaryPath.pathString) should exist, but it does not")
120+
// WHEN we write contents to the file
121+
try fs.writeFileContents(pathUnderTest, bytes: expectedContents)
108122

109-
let actualContents = try fs.readFileContents(binaryPath)
110-
#expect(actualContents == expectedContents, "Actual file contents not as expected")
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+
}
111227
}
112228

113229
}

Tests/WorkspaceTests/WorkspaceTests.swift

+14-5
Original file line numberDiff line numberDiff line change
@@ -3025,6 +3025,11 @@ final class WorkspaceTests: XCTestCase {
30253025
workspace.manifestLoader.manifests[editedFooKey] = manifest
30263026
}
30273027

3028+
}
3029+
func testResolutionFailureWithEditedDependencyWithABadGraph() async throws {
3030+
let sandbox = AbsolutePath("/tmp/ws/")
3031+
let fs = InMemoryFileSystem()
3032+
30283033
// Try resolving a bad graph.
30293034
let deps: [MockDependency] = [
30303035
.sourceControl(path: "./Bar", requirement: .exact("1.1.0"), products: .specific(["Bar"])),
@@ -3882,14 +3887,12 @@ final class WorkspaceTests: XCTestCase {
38823887
}
38833888

38843889
func testResolvedFileSchemeToolsVersion() async throws {
3885-
3886-
let fs = InMemoryFileSystem()
3887-
38883890
for pair in [
38893891
(ToolsVersion.v5_2, ToolsVersion.v5_2),
38903892
(ToolsVersion.v5_6, ToolsVersion.v5_6),
38913893
(ToolsVersion.v5_2, ToolsVersion.v5_6),
38923894
] {
3895+
let fs = InMemoryFileSystem()
38933896
let sandbox = AbsolutePath("/tmp/ws/")
38943897
let workspace = try await MockWorkspace(
38953898
sandbox: sandbox,
@@ -3946,9 +3949,11 @@ final class WorkspaceTests: XCTestCase {
39463949

39473950
let minToolsVersion = [pair.0, pair.1].min()!
39483951
let expectedSchemeVersion = minToolsVersion >= .v5_6 ? 2 : 1
3952+
let actualSchemeVersion = try workspace.getOrCreateWorkspace().resolvedPackagesStore.load().schemeVersion()
39493953
XCTAssertEqual(
3950-
try workspace.getOrCreateWorkspace().resolvedPackagesStore.load().schemeVersion(),
3951-
expectedSchemeVersion
3954+
actualSchemeVersion,
3955+
expectedSchemeVersion,
3956+
"Actual scheme version (\(actualSchemeVersion)) is not as expected (\(expectedSchemeVersion)). Pair 0 (\(pair.0)) pair 1 (\(pair.1))"
39523957
)
39533958
}
39543959
}
@@ -8033,6 +8038,10 @@ final class WorkspaceTests: XCTestCase {
80338038
}
80348039

80358040
func testArtifactChecksum() async throws {
8041+
try skipOnWindowsAsTestCurrentlyFails(because: #"""
8042+
threw error "\tmp\ws doesn't exist in file system" because there is an issue with InMemoryFileSystem readFileContents(...) on Windows
8043+
"""#)
8044+
80368045
let fs = InMemoryFileSystem()
80378046
try fs.createMockToolchain()
80388047
let sandbox = AbsolutePath("/tmp/ws/")

0 commit comments

Comments
 (0)