|
1 |
| -import SwiftFormat |
| 1 | +@_spi(Internal) import SwiftFormat |
2 | 2 | import XCTest
|
3 | 3 |
|
4 | 4 | final class IgnoreFileTests: XCTestCase {
|
| 5 | + var testTreeURL: URL? |
| 6 | + |
| 7 | + /// Description of a file or directory tree to create for testing. |
| 8 | + enum TestTree { |
| 9 | + case file(String, String) |
| 10 | + case directory(String, [TestTree]) |
| 11 | + } |
| 12 | + |
| 13 | + override func tearDown() { |
| 14 | + // Clean up any test tree after each test. |
| 15 | + if let testTreeURL { |
| 16 | + // try? FileManager.default.removeItem(at: testTreeURL) |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + /// Make a temporary directory tree for testing. |
| 21 | + /// Returns the URL of the root directory. |
| 22 | + /// The tree will be cleaned up after the test. |
| 23 | + /// If a tree is already set up, it will be cleaned up first. |
| 24 | + func makeTempTree(_ tree: TestTree) throws -> URL { |
| 25 | + if let testTreeURL { |
| 26 | + try? FileManager.default.removeItem(at: testTreeURL) |
| 27 | + } |
| 28 | + let tempDir = FileManager.default.temporaryDirectory |
| 29 | + let tempURL = tempDir.appendingPathComponent(UUID().uuidString) |
| 30 | + try FileManager.default.createDirectory(at: tempURL, withIntermediateDirectories: true) |
| 31 | + try writeTree(tree, to: tempURL) |
| 32 | + testTreeURL = tempURL |
| 33 | + return tempURL |
| 34 | + } |
| 35 | + |
| 36 | + /// Write a file or directory tree to the given root URL. |
| 37 | + func writeTree(_ tree: TestTree, to root: URL) throws { |
| 38 | + switch tree { |
| 39 | + case let .file(name, contents): |
| 40 | + print("Writing file \(name) to \(root)") |
| 41 | + try contents.write(to: root.appendingPathComponent(name), atomically: true, encoding: .utf8) |
| 42 | + case let .directory(name, children): |
| 43 | + let directory = root.appendingPathComponent(name) |
| 44 | + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) |
| 45 | + for child in children { |
| 46 | + try writeTree(child, to: directory) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
5 | 50 |
|
6 | 51 | func testMissingIgnoreFile() throws {
|
7 |
| - let url = Bundle.module.url(forResource: "missing", withExtension: "", subdirectory: "Ignore Files") |
8 |
| - XCTAssertNotNil(url) |
9 |
| - XCTAssertNil(try IgnoreFile(forDirectory: url!)) |
10 |
| - XCTAssertNil(try IgnoreFile(for: url!.appending(path:"file.swift"))) |
| 52 | + let url = URL(filePath: "/") |
| 53 | + XCTAssertNil(try IgnoreFile(forDirectory: url)) |
| 54 | + XCTAssertNil(try IgnoreFile(for: url.appending(path: "file.swift"))) |
11 | 55 | }
|
12 | 56 |
|
13 | 57 | func testValidIgnoreFile() throws {
|
14 |
| - let url = Bundle.module.url(forResource: "valid", withExtension: "", subdirectory: "Ignore Files") |
15 |
| - XCTAssertNotNil(url) |
16 |
| - XCTAssertNotNil(try IgnoreFile(forDirectory: url!)) |
17 |
| - XCTAssertNotNil(try IgnoreFile(for: url!.appending(path:"file.swift"))) |
| 58 | + let url = try makeTempTree(.file(IgnoreFile.standardFileName, "*")) |
| 59 | + XCTAssertNotNil(try IgnoreFile(forDirectory: url)) |
| 60 | + XCTAssertNotNil(try IgnoreFile(for: url.appending(path: "file.swift"))) |
18 | 61 | }
|
19 | 62 |
|
20 | 63 | func testInvalidIgnoreFile() throws {
|
21 |
| - let url = Bundle.module.url(forResource: "invalid", withExtension: "", subdirectory: "Ignore Files") |
22 |
| - XCTAssertNotNil(url) |
23 |
| - XCTAssertThrowsError(try IgnoreFile(forDirectory: url!)) |
24 |
| - XCTAssertThrowsError(try IgnoreFile(for: url!.appending(path:"file.swift"))) |
| 64 | + let url = try makeTempTree(.file(IgnoreFile.standardFileName, "this is an invalid pattern")) |
| 65 | + XCTAssertThrowsError(try IgnoreFile(forDirectory: url)) |
| 66 | + XCTAssertThrowsError(try IgnoreFile(for: url.appending(path: "file.swift"))) |
25 | 67 | }
|
26 | 68 |
|
27 | 69 | func testEmptyIgnoreFile() throws {
|
28 |
| - let url = Bundle.module.url(forResource: "empty", withExtension: "", subdirectory: "Ignore Files") |
29 |
| - XCTAssertNotNil(url) |
30 |
| - XCTAssertThrowsError(try IgnoreFile(forDirectory: url!)) |
31 |
| - XCTAssertThrowsError(try IgnoreFile(for: url!.appending(path:"file.swift"))) |
| 70 | + XCTAssertThrowsError(try IgnoreFile("")) |
32 | 71 | }
|
33 | 72 |
|
34 | 73 | func testNestedIgnoreFile() throws {
|
35 |
| - let url = Bundle.module.url(forResource: "nested", withExtension: "", subdirectory: "Ignore Files") |
36 |
| - XCTAssertNotNil(url) |
37 |
| - let subdirectory = url!.appendingPathComponent("subdirectory").appending(path: "file.swift") |
38 |
| - XCTAssertNotNil(try IgnoreFile(for: subdirectory)) |
| 74 | + let url = try makeTempTree(.file(IgnoreFile.standardFileName, "*")) |
| 75 | + let fileInSubdirectory = url.appendingPathComponent("subdirectory").appending(path: "file.swift") |
| 76 | + XCTAssertNotNil(try IgnoreFile(for: fileInSubdirectory)) |
| 77 | + } |
| 78 | + |
| 79 | + func testIterateWithIgnoreFile() throws { |
| 80 | + let url = try makeTempTree(.file(IgnoreFile.standardFileName, "*")) |
| 81 | + let iterator = FileIterator(urls: [url], followSymlinks: false) |
| 82 | + let files = Array(iterator) |
| 83 | + XCTAssertEqual(files.count, 0) |
| 84 | + } |
| 85 | + |
| 86 | + func testIterateWithInvalidIgnoreFile() throws { |
| 87 | + let url = try makeTempTree(.file(IgnoreFile.standardFileName, "this file is invalid")) |
| 88 | + let iterator = FileIterator(urls: [url], followSymlinks: false) |
| 89 | + let files = Array(iterator) |
| 90 | + XCTAssertEqual(files.count, 1) |
| 91 | + XCTAssertTrue(files.first?.lastPathComponent == IgnoreFile.standardFileName) |
| 92 | + } |
| 93 | + |
| 94 | + func testIterateWithNestedIgnoreFile() throws { |
| 95 | + let url = try makeTempTree( |
| 96 | + .directory( |
| 97 | + "Source", |
| 98 | + [ |
| 99 | + .directory( |
| 100 | + "Ignored", |
| 101 | + [ |
| 102 | + .file(IgnoreFile.standardFileName, "*"), |
| 103 | + .file("file.swift", "contents"), |
| 104 | + ] |
| 105 | + ), |
| 106 | + .directory( |
| 107 | + "Not Ignored", |
| 108 | + [ |
| 109 | + .file("file.swift", "contents") |
| 110 | + ] |
| 111 | + ), |
| 112 | + ] |
| 113 | + ) |
| 114 | + ) |
| 115 | + |
| 116 | + XCTAssertNil(try IgnoreFile(forDirectory: url)) |
| 117 | + XCTAssertNil(try IgnoreFile(for: url.appending(path: "Source/file.swift"))) |
| 118 | + XCTAssertNotNil(try IgnoreFile(for: url.appending(path: "Source/Ignored/file.swift"))) |
| 119 | + let iterator = FileIterator(urls: [url], followSymlinks: false) |
| 120 | + let files = Array(iterator) |
| 121 | + print(files) |
| 122 | + XCTAssertEqual(files.count, 1) |
| 123 | + XCTAssertEqual(files.first?.lastPathComponent, "file.swift") |
39 | 124 | }
|
40 | 125 |
|
41 | 126 | }
|
0 commit comments