-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathIgnoreFileTests.swift
126 lines (110 loc) · 4.24 KB
/
IgnoreFileTests.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
@_spi(Internal) import SwiftFormat
import XCTest
final class IgnoreFileTests: XCTestCase {
var testTreeURL: URL?
/// Description of a file or directory tree to create for testing.
enum TestTree {
case file(String, String)
case directory(String, [TestTree])
}
override func tearDown() {
// Clean up any test tree after each test.
if let testTreeURL {
// try? FileManager.default.removeItem(at: testTreeURL)
}
}
/// Make a temporary directory tree for testing.
/// Returns the URL of the root directory.
/// The tree will be cleaned up after the test.
/// If a tree is already set up, it will be cleaned up first.
func makeTempTree(_ tree: TestTree) throws -> URL {
if let testTreeURL {
try? FileManager.default.removeItem(at: testTreeURL)
}
let tempDir = FileManager.default.temporaryDirectory
let tempURL = tempDir.appendingPathComponent(UUID().uuidString)
try FileManager.default.createDirectory(at: tempURL, withIntermediateDirectories: true)
try writeTree(tree, to: tempURL)
testTreeURL = tempURL
return tempURL
}
/// Write a file or directory tree to the given root URL.
func writeTree(_ tree: TestTree, to root: URL) throws {
switch tree {
case let .file(name, contents):
print("Writing file \(name) to \(root)")
try contents.write(to: root.appendingPathComponent(name), atomically: true, encoding: .utf8)
case let .directory(name, children):
let directory = root.appendingPathComponent(name)
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
for child in children {
try writeTree(child, to: directory)
}
}
}
func testMissingIgnoreFile() throws {
let url = URL(filePath: "/")
XCTAssertNil(try IgnoreFile(forDirectory: url))
XCTAssertNil(try IgnoreFile(for: url.appending(path: "file.swift")))
}
func testValidIgnoreFile() throws {
let url = try makeTempTree(.file(IgnoreFile.standardFileName, "*"))
XCTAssertNotNil(try IgnoreFile(forDirectory: url))
XCTAssertNotNil(try IgnoreFile(for: url.appending(path: "file.swift")))
}
func testInvalidIgnoreFile() throws {
let url = try makeTempTree(.file(IgnoreFile.standardFileName, "this is an invalid pattern"))
XCTAssertThrowsError(try IgnoreFile(forDirectory: url))
XCTAssertThrowsError(try IgnoreFile(for: url.appending(path: "file.swift")))
}
func testEmptyIgnoreFile() throws {
XCTAssertThrowsError(try IgnoreFile(""))
}
func testNestedIgnoreFile() throws {
let url = try makeTempTree(.file(IgnoreFile.standardFileName, "*"))
let fileInSubdirectory = url.appendingPathComponent("subdirectory").appending(path: "file.swift")
XCTAssertNotNil(try IgnoreFile(for: fileInSubdirectory))
}
func testIterateWithIgnoreFile() throws {
let url = try makeTempTree(.file(IgnoreFile.standardFileName, "*"))
let iterator = FileIterator(urls: [url], followSymlinks: false)
let files = Array(iterator)
XCTAssertEqual(files.count, 0)
}
func testIterateWithInvalidIgnoreFile() throws {
let url = try makeTempTree(.file(IgnoreFile.standardFileName, "this file is invalid"))
let iterator = FileIterator(urls: [url], followSymlinks: false)
let files = Array(iterator)
XCTAssertEqual(files.count, 1)
XCTAssertTrue(files.first?.lastPathComponent == IgnoreFile.standardFileName)
}
func testIterateWithNestedIgnoreFile() throws {
let url = try makeTempTree(
.directory(
"Source",
[
.directory(
"Ignored",
[
.file(IgnoreFile.standardFileName, "*"),
.file("file.swift", "contents"),
]
),
.directory(
"Not Ignored",
[
.file("file.swift", "contents")
]
),
]
)
)
XCTAssertNil(try IgnoreFile(forDirectory: url))
XCTAssertNil(try IgnoreFile(for: url.appending(path: "Source/file.swift")))
XCTAssertNotNil(try IgnoreFile(for: url.appending(path: "Source/Ignored/file.swift")))
let iterator = FileIterator(urls: [url], followSymlinks: false)
let files = Array(iterator)
XCTAssertEqual(files.count, 1)
XCTAssertEqual(files.first?.lastPathComponent, "file.swift")
}
}