-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestFileManager.swift
413 lines (337 loc) · 14.7 KB
/
TestFileManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif
class TestFileManager : XCTestCase {
static var allTests: [(String, (TestFileManager) -> () throws -> Void)] {
return [
("test_createDirectory", test_createDirectory ),
("test_createFile", test_createFile ),
("test_moveFile", test_moveFile),
("test_fileSystemRepresentation", test_fileSystemRepresentation),
("test_fileAttributes", test_fileAttributes),
("test_setFileAttributes", test_setFileAttributes),
("test_directoryEnumerator", test_directoryEnumerator),
("test_pathEnumerator",test_pathEnumerator),
("test_contentsOfDirectoryAtPath", test_contentsOfDirectoryAtPath),
("test_subpathsOfDirectoryAtPath", test_subpathsOfDirectoryAtPath),
]
}
func ignoreError(_ block: () throws -> Void) {
do { try block() } catch { }
}
func test_createDirectory() {
let fm = FileManager.default
let path = NSTemporaryDirectory() + "testdir\(NSUUID().uuidString)"
ignoreError { try fm.removeItem(atPath: path) }
do {
try fm.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
} catch _ {
XCTFail()
}
var isDir = false
let exists = fm.fileExists(atPath: path, isDirectory: &isDir)
XCTAssertTrue(exists)
XCTAssertTrue(isDir)
do {
try fm.removeItem(atPath: path)
} catch {
XCTFail("Failed to clean up file")
}
}
func test_createFile() {
let fm = FileManager.default
let path = NSTemporaryDirectory() + "testfile\(NSUUID().uuidString)"
ignoreError { try fm.removeItem(atPath: path) }
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))
var isDir = false
let exists = fm.fileExists(atPath: path, isDirectory: &isDir)
XCTAssertTrue(exists)
XCTAssertFalse(isDir)
do {
try fm.removeItem(atPath: path)
} catch {
XCTFail("Failed to clean up file")
}
}
func test_moveFile() {
let fm = FileManager.default
let path = NSTemporaryDirectory() + "testfile\(NSUUID().uuidString)"
let path2 = NSTemporaryDirectory() + "testfile2\(NSUUID().uuidString)"
func cleanup() {
ignoreError { try fm.removeItem(atPath: path) }
ignoreError { try fm.removeItem(atPath: path2) }
}
cleanup()
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))
defer { cleanup() }
do {
try fm.moveItem(atPath: path, toPath: path2)
} catch let error {
XCTFail("Failed to move file: \(error)")
}
}
func test_fileSystemRepresentation() {
let str = "☃"
let result = FileManager.default.fileSystemRepresentation(withPath: str)
XCTAssertNotNil(result)
XCTAssertEqual(UInt8(bitPattern: result[0]), 0xE2)
XCTAssertEqual(UInt8(bitPattern: result[1]), 0x98)
XCTAssertEqual(UInt8(bitPattern: result[2]), 0x83)
}
func test_fileAttributes() {
let fm = FileManager.default
let path = NSTemporaryDirectory() + "test_fileAttributes\(NSUUID().uuidString)"
ignoreError { try fm.removeItem(atPath: path) }
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))
do {
let attrs = try fm.attributesOfItem(atPath: path)
XCTAssertTrue(attrs.count > 0)
let fileSize = attrs[.size] as? NSNumber
XCTAssertEqual(fileSize!.int64Value, 0)
let fileModificationDate = attrs[.modificationDate] as? Date
XCTAssertGreaterThan(Date().timeIntervalSince1970, fileModificationDate!.timeIntervalSince1970)
let filePosixPermissions = attrs[.posixPermissions] as? NSNumber
XCTAssertNotEqual(filePosixPermissions!.int64Value, 0)
let fileReferenceCount = attrs[.referenceCount] as? NSNumber
XCTAssertEqual(fileReferenceCount!.int64Value, 1)
let fileSystemNumber = attrs[.systemNumber] as? NSNumber
XCTAssertNotEqual(fileSystemNumber!.int64Value, 0)
let fileSystemFileNumber = attrs[.systemFileNumber] as? NSNumber
XCTAssertNotEqual(fileSystemFileNumber!.int64Value, 0)
let fileType = attrs[.type] as? FileAttributeType
XCTAssertEqual(fileType!, .typeRegular)
let fileOwnerAccountID = attrs[.ownerAccountID] as? NSNumber
XCTAssertNotNil(fileOwnerAccountID)
} catch let err {
XCTFail("\(err)")
}
do {
try fm.removeItem(atPath: path)
} catch {
XCTFail("Failed to clean up files")
}
}
func test_setFileAttributes() {
let path = NSTemporaryDirectory() + "test_setFileAttributes\(NSUUID().uuidString)"
let fm = FileManager.default
ignoreError { try fm.removeItem(atPath: path) }
XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))
do {
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0600))], ofItemAtPath: path)
}
catch { XCTFail("\(error)") }
//read back the attributes
do {
let attributes = try fm.attributesOfItem(atPath: path)
XCTAssert((attributes[.posixPermissions] as? NSNumber)?.int16Value == 0o0600)
}
catch { XCTFail("\(error)") }
do {
try fm.removeItem(atPath: path)
} catch {
XCTFail("Failed to clean up files")
}
}
func test_pathEnumerator() {
let fm = FileManager.default
let testDirName = "testdir\(NSUUID().uuidString)"
let basePath = NSTemporaryDirectory() + "\(testDirName)"
let itemPath = NSTemporaryDirectory() + "\(testDirName)/item"
let basePath2 = NSTemporaryDirectory() + "\(testDirName)/path2"
let itemPath2 = NSTemporaryDirectory() + "\(testDirName)/path2/item"
ignoreError { try fm.removeItem(atPath: basePath) }
do {
try fm.createDirectory(atPath: basePath, withIntermediateDirectories: false, attributes: nil)
try fm.createDirectory(atPath: basePath2, withIntermediateDirectories: false, attributes: nil)
let _ = fm.createFile(atPath: itemPath, contents: Data(), attributes: nil)
let _ = fm.createFile(atPath: itemPath2, contents: Data(), attributes: nil)
} catch _ {
XCTFail()
}
if let e = FileManager.default.enumerator(atPath: basePath) {
var foundItems = Set<String>()
while let item = e.nextObject() as? String {
foundItems.insert(item)
}
XCTAssertEqual(foundItems, Set(["item", "path2", "path2/item"]))
} else {
XCTFail()
}
}
func test_directoryEnumerator() {
let fm = FileManager.default
let testDirName = "testdir\(NSUUID().uuidString)"
let path = NSTemporaryDirectory() + "\(testDirName)"
let itemPath = NSTemporaryDirectory() + "\(testDirName)/item"
ignoreError { try fm.removeItem(atPath: path) }
do {
try fm.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
let _ = fm.createFile(atPath: itemPath, contents: Data(), attributes: nil)
} catch _ {
XCTFail()
}
if let e = FileManager.default.enumerator(at: URL(fileURLWithPath: path), includingPropertiesForKeys: nil, options: [], errorHandler: nil) {
var foundItems = [String:Int]()
while let item = e.nextObject() as? URL {
foundItems[item.path] = e.level
}
XCTAssertEqual(foundItems[itemPath], 1)
} else {
XCTFail()
}
let subDirPath = NSTemporaryDirectory() + "\(testDirName)/testdir2"
let subDirItemPath = NSTemporaryDirectory() + "\(testDirName)/testdir2/item"
do {
try fm.createDirectory(atPath: subDirPath, withIntermediateDirectories: false, attributes: nil)
let _ = fm.createFile(atPath: subDirItemPath, contents: Data(), attributes: nil)
} catch _ {
XCTFail()
}
if let e = FileManager.default.enumerator(at: URL(fileURLWithPath: path), includingPropertiesForKeys: nil, options: [], errorHandler: nil) {
var foundItems = [String:Int]()
while let item = e.nextObject() as? URL {
foundItems[item.path] = e.level
}
XCTAssertEqual(foundItems[itemPath], 1)
XCTAssertEqual(foundItems[subDirPath], 1)
XCTAssertEqual(foundItems[subDirItemPath], 2)
} else {
XCTFail()
}
if let e = FileManager.default.enumerator(at: URL(fileURLWithPath: path), includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants], errorHandler: nil) {
var foundItems = [String:Int]()
while let item = e.nextObject() as? URL {
foundItems[item.path] = e.level
}
XCTAssertEqual(foundItems[itemPath], 1)
XCTAssertEqual(foundItems[subDirPath], 1)
} else {
XCTFail()
}
if let e = FileManager.default.enumerator(at: URL(fileURLWithPath: path), includingPropertiesForKeys: nil, options: [], errorHandler: nil) {
var foundItems = [String:Int]()
while let item = e.nextObject() as? URL {
foundItems[item.path] = e.level
}
XCTAssertEqual(foundItems[itemPath], 1)
XCTAssertEqual(foundItems[subDirPath], 1)
} else {
XCTFail()
}
var didGetError = false
let handler : (URL, Error) -> Bool = { (URL, Error) in
didGetError = true
return true
}
if let e = FileManager.default.enumerator(at: URL(fileURLWithPath: "/nonexistant-path"), includingPropertiesForKeys: nil, options: [], errorHandler: handler) {
XCTAssertNil(e.nextObject())
} else {
XCTFail()
}
XCTAssertTrue(didGetError)
do {
let contents = try FileManager.default.contentsOfDirectory(at: URL(fileURLWithPath: path), includingPropertiesForKeys: nil, options: []).map {
return $0.path
}
XCTAssertEqual(contents.count, 2)
XCTAssertTrue(contents.contains(itemPath))
XCTAssertTrue(contents.contains(subDirPath))
} catch {
XCTFail()
}
do {
try fm.removeItem(atPath: path)
} catch {
XCTFail("Failed to clean up files")
}
}
func test_contentsOfDirectoryAtPath() {
let fm = FileManager.default
let testDirName = "testdir\(NSUUID().uuidString)"
let path = NSTemporaryDirectory() + "\(testDirName)"
let itemPath1 = NSTemporaryDirectory() + "\(testDirName)/item"
let itemPath2 = NSTemporaryDirectory() + "\(testDirName)/item2"
ignoreError { try fm.removeItem(atPath: path) }
do {
try fm.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
let _ = fm.createFile(atPath: itemPath1, contents: Data(), attributes: nil)
let _ = fm.createFile(atPath: itemPath2, contents: Data(), attributes: nil)
} catch _ {
XCTFail()
}
do {
let entries = try fm.contentsOfDirectory(atPath: path)
XCTAssertEqual(2, entries.count)
XCTAssertTrue(entries.contains("item"))
XCTAssertTrue(entries.contains("item2"))
}
catch _ {
XCTFail()
}
do {
let _ = try fm.contentsOfDirectory(atPath: "")
XCTFail()
}
catch _ {
// Invalid directories should fail.
}
do {
try fm.removeItem(atPath: path)
} catch {
XCTFail("Failed to clean up files")
}
}
func test_subpathsOfDirectoryAtPath() {
let fm = FileManager.default
let path = NSTemporaryDirectory() + "testdir"
let path2 = NSTemporaryDirectory() + "testdir/sub"
let itemPath1 = NSTemporaryDirectory() + "testdir/item"
let itemPath2 = NSTemporaryDirectory() + "testdir/item2"
let itemPath3 = NSTemporaryDirectory() + "testdir/sub/item3"
ignoreError { try fm.removeItem(atPath: path) }
do {
try fm.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
let _ = fm.createFile(atPath: itemPath1, contents: Data(), attributes: nil)
let _ = fm.createFile(atPath: itemPath2, contents: Data(), attributes: nil)
try fm.createDirectory(atPath: path2, withIntermediateDirectories: false, attributes: nil)
let _ = fm.createFile(atPath: itemPath3, contents: Data(), attributes: nil)
} catch _ {
XCTFail()
}
do {
let entries = try fm.subpathsOfDirectory(atPath: path)
XCTAssertEqual(4, entries.count)
XCTAssertTrue(entries.contains("item"))
XCTAssertTrue(entries.contains("item2"))
XCTAssertTrue(entries.contains("sub"))
XCTAssertTrue(entries.contains("sub/item3"))
}
catch _ {
XCTFail()
}
do {
let _ = try fm.subpathsOfDirectory(atPath: "")
XCTFail()
}
catch _ {
// Invalid directories should fail.
}
do {
try fm.removeItem(atPath: path)
} catch {
XCTFail("Failed to clean up files")
}
}
}