|
| 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