Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
import Storage
7
import XCTest
8
9
class FileAccessorTests: XCTestCase {
10
fileprivate var testDir: String!
11
fileprivate var files: FileAccessor!
12
13
override func setUp() {
14
let docPath: NSString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
15
files = FileAccessor(rootPath: docPath.appendingPathComponent("filetest"))
16
17
testDir = try! files.getAndEnsureDirectory()
18
try! files.removeFilesInDirectory()
19
}
20
21
func testFileAccessor() {
22
// Test existence.
23
XCTAssertFalse(files.exists("foo"), "File doesn't exist")
24
createFile("foo")
25
XCTAssertTrue(files.exists("foo"), "File exists")
26
27
// Test moving.
28
do {
29
try files.move("foo", toRelativePath: "bar")
30
XCTAssertFalse(files.exists("foo"), "Old doesn't exist")
31
XCTAssertTrue(files.exists("bar"), "New file exists")
32
} catch {
33
XCTFail("Unable to move 'foo' to 'bar' \(error)")
34
}
35
36
do {
37
try files.move("bar", toRelativePath: "foo/bar")
38
XCTAssertFalse(files.exists("bar"), "Old doesn't exist")
39
XCTAssertTrue(files.exists("foo/bar"), "New file exists")
40
} catch {
41
XCTFail("Unable to move 'bar' to 'foo/bar' \(error)")
42
}
43
44
// Test removal.
45
do {
46
XCTAssertTrue(files.exists("foo"), "File exists")
47
try files.remove("foo")
48
XCTAssertFalse(files.exists("foo"), "File removed")
49
} catch {
50
XCTFail("Unable to remove 'foo' \(error)")
51
}
52
53
// Test directory creation and path.
54
do {
55
XCTAssertFalse(files.exists("foo"), "Directory doesn't exist")
56
let path = try files.getAndEnsureDirectory("foo")
57
var isDirectory = ObjCBool(false)
58
FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
59
XCTAssertTrue(isDirectory.boolValue, "Directory exists")
60
} catch {
61
XCTFail("Unable to find directory 'foo' \(error)")
62
}
63
}
64
65
fileprivate func createFile(_ filename: String) {
66
let path = (testDir as NSString).appendingPathComponent(filename)
67
let success: Bool
68
do {
69
try "foo".write(toFile: path, atomically: false, encoding: .utf8)
70
success = true
71
} catch _ {
72
success = false
73
}
74
XCTAssertTrue(success, "Wrote to \(path)")
75
}
76
}