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