10
10
11
11
import Basics
12
12
import struct TSCBasic. ByteString
13
+ import struct TSCBasic. FileSystemError
13
14
14
15
import Testing
16
+ import _InternalTestSupport
15
17
16
18
struct InMemoryFileSystemTests {
17
19
@Test (
@@ -43,6 +45,16 @@ struct InMemoryFileSystemTests {
43
45
] ,
44
46
expectError: false ,
45
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
+ ) ,
46
58
]
47
59
)
48
60
func creatingDirectoryCreatesInternalFiles(
@@ -95,19 +107,123 @@ struct InMemoryFileSystemTests {
95
107
}
96
108
}
97
109
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 {
103
111
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 ] )
106
119
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)
108
122
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
+ }
111
227
}
112
228
113
229
}
0 commit comments