-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathFileAccessorTests.swift
76 lines (66 loc) · 2.65 KB
/
FileAccessorTests.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import Storage
import XCTest
class FileAccessorTests: XCTestCase {
fileprivate var testDir: String!
fileprivate var files: FileAccessor!
override func setUp() {
let docPath: NSString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
files = FileAccessor(rootPath: docPath.appendingPathComponent("filetest"))
testDir = try! files.getAndEnsureDirectory()
try! files.removeFilesInDirectory()
}
func testFileAccessor() {
// Test existence.
XCTAssertFalse(files.exists("foo"), "File doesn't exist")
createFile("foo")
XCTAssertTrue(files.exists("foo"), "File exists")
// Test moving.
do {
try files.move("foo", toRelativePath: "bar")
XCTAssertFalse(files.exists("foo"), "Old doesn't exist")
XCTAssertTrue(files.exists("bar"), "New file exists")
} catch {
XCTFail("Unable to move 'foo' to 'bar' \(error)")
}
do {
try files.move("bar", toRelativePath: "foo/bar")
XCTAssertFalse(files.exists("bar"), "Old doesn't exist")
XCTAssertTrue(files.exists("foo/bar"), "New file exists")
} catch {
XCTFail("Unable to move 'bar' to 'foo/bar' \(error)")
}
// Test removal.
do {
XCTAssertTrue(files.exists("foo"), "File exists")
try files.remove("foo")
XCTAssertFalse(files.exists("foo"), "File removed")
} catch {
XCTFail("Unable to remove 'foo' \(error)")
}
// Test directory creation and path.
do {
XCTAssertFalse(files.exists("foo"), "Directory doesn't exist")
let path = try files.getAndEnsureDirectory("foo")
var isDirectory = ObjCBool(false)
FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
XCTAssertTrue(isDirectory.boolValue, "Directory exists")
} catch {
XCTFail("Unable to find directory 'foo' \(error)")
}
}
fileprivate func createFile(_ filename: String) {
let path = (testDir as NSString).appendingPathComponent(filename)
let success: Bool
do {
try "foo".write(toFile: path, atomically: false, encoding: .utf8)
success = true
} catch _ {
success = false
}
XCTAssertTrue(success, "Wrote to \(path)")
}
}