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