Skip to content

Commit 264da56

Browse files
committed
Removed test resources
1 parent e671b72 commit 264da56

File tree

8 files changed

+115
-36
lines changed

8 files changed

+115
-36
lines changed

Package.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ var targets: [Target] = [
114114
"SwiftFormat",
115115
"_SwiftFormatTestSupport",
116116
.product(name: "Markdown", package: "swift-markdown"),
117-
] + swiftSyntaxDependencies(["SwiftOperators", "SwiftParser", "SwiftSyntax", "SwiftSyntaxBuilder"]),
118-
resources: [.copy("Resources")]
117+
] + swiftSyntaxDependencies(["SwiftOperators", "SwiftParser", "SwiftSyntax", "SwiftSyntaxBuilder"])
119118
),
120119
]
121120

Sources/SwiftFormat/Core/IgnoreFile.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class IgnoreFile {
2222
/// Name of the ignore file to look for.
2323
/// The presence of this file in a directory will cause the formatter
2424
/// to skip formatting files in that directory and its subdirectories.
25-
fileprivate static let fileName = ".swift-format-ignore"
25+
public static let standardFileName = ".swift-format-ignore"
2626

2727
/// Errors that can be thrown by the IgnoreFile initializer.
2828
public enum Error: Swift.Error {
@@ -58,7 +58,7 @@ public class IgnoreFile {
5858
///
5959
/// Note that this initializer does not search parent directories for ignore files.
6060
public convenience init?(forDirectory directory: URL) throws {
61-
let url = directory.appendingPathComponent(IgnoreFile.fileName)
61+
let url = directory.appendingPathComponent(IgnoreFile.standardFileName)
6262

6363
do {
6464
try self.init(contentsOf: url)
@@ -84,10 +84,14 @@ public class IgnoreFile {
8484
/// If you pass a directory URL, the search will not include the contents
8585
/// of that directory.
8686
public convenience init?(for url: URL) throws {
87+
guard !url.isRoot else {
88+
return nil
89+
}
90+
8791
var containingDirectory = url.absoluteURL.standardized
8892
repeat {
8993
containingDirectory.deleteLastPathComponent()
90-
let url = containingDirectory.appendingPathComponent(IgnoreFile.fileName)
94+
let url = containingDirectory.appendingPathComponent(IgnoreFile.standardFileName)
9195
if FileManager.default.isReadableFile(atPath: url.path) {
9296
try self.init(contentsOf: url)
9397
return
@@ -102,10 +106,4 @@ public class IgnoreFile {
102106
func shouldProcess(_ url: URL) -> Bool {
103107
return false
104108
}
105-
106-
/// Returns true if the name of the given URL matches
107-
/// the standard ignore file name.
108-
public static func isStandardIgnoreFile(_ url: URL) -> Bool {
109-
return url.lastPathComponent == fileName
110-
}
111109
}

Sources/SwiftFormat/Utilities/FileIterator.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ public struct FileIterator: Sequence, IteratorProtocol {
164164
: path
165165
output =
166166
URL(fileURLWithPath: relativePath, isDirectory: false, relativeTo: workingDirectory)
167-
168167
default:
169168
break
170169
}

Sources/swift-format/Frontend/Frontend.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class Frontend {
165165
/// Read and prepare the file at the given path for processing, optionally synchronizing
166166
/// diagnostic output.
167167
private func openAndPrepareFile(at url: URL) -> FileToProcess? {
168-
guard !IgnoreFile.isStandardIgnoreFile(url) else {
168+
guard url.lastPathComponent != IgnoreFile.standardFileName else {
169169
diagnosticsEngine.emitError(
170170
"Invalid ignore file \(url.relativePath): currently the only supported content for ignore files is a single asterisk `*`, which matches all files."
171171
)
Lines changed: 106 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,126 @@
1-
import SwiftFormat
1+
@_spi(Internal) import SwiftFormat
22
import XCTest
33

44
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+
}
550

651
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")))
1155
}
1256

1357
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")))
1861
}
1962

2063
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")))
2567
}
2668

2769
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(""))
3271
}
3372

3473
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")
39124
}
40125

41126
}

Tests/SwiftFormatTests/Resources/Ignore Files/empty/.swift-format-ignore

Whitespace-only changes.

Tests/SwiftFormatTests/Resources/Ignore Files/invalid/.swift-format-ignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

Tests/SwiftFormatTests/Resources/Ignore Files/valid/.swift-format-ignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)