Skip to content

Commit

Permalink
Removed test resources
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Dec 11, 2024
1 parent ba343ca commit c9a14e3
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 36 deletions.
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ var targets: [Target] = [
"SwiftFormat",
"_SwiftFormatTestSupport",
.product(name: "Markdown", package: "swift-markdown"),
] + swiftSyntaxDependencies(["SwiftOperators", "SwiftParser", "SwiftSyntax", "SwiftSyntaxBuilder"]),
resources: [.copy("Resources")]
] + swiftSyntaxDependencies(["SwiftOperators", "SwiftParser", "SwiftSyntax", "SwiftSyntaxBuilder"])
),
]

Expand Down
16 changes: 7 additions & 9 deletions Sources/SwiftFormat/Core/IgnoreFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class IgnoreFile {
/// Name of the ignore file to look for.
/// The presence of this file in a directory will cause the formatter
/// to skip formatting files in that directory and its subdirectories.
fileprivate static let fileName = ".swift-format-ignore"
public static let standardFileName = ".swift-format-ignore"

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

do {
try self.init(contentsOf: url)
Expand All @@ -84,10 +84,14 @@ public class IgnoreFile {
/// If you pass a directory URL, the search will not include the contents
/// of that directory.
public convenience init?(for url: URL) throws {
guard !url.isRoot else {
return nil
}

var containingDirectory = url.absoluteURL.standardized
repeat {
containingDirectory.deleteLastPathComponent()
let url = containingDirectory.appendingPathComponent(IgnoreFile.fileName)
let url = containingDirectory.appendingPathComponent(IgnoreFile.standardFileName)
if FileManager.default.isReadableFile(atPath: url.path) {
try self.init(contentsOf: url)
return
Expand All @@ -102,10 +106,4 @@ public class IgnoreFile {
func shouldProcess(_ url: URL) -> Bool {
return false
}

/// Returns true if the name of the given URL matches
/// the standard ignore file name.
public static func isStandardIgnoreFile(_ url: URL) -> Bool {
return url.lastPathComponent == fileName
}
}
1 change: 0 additions & 1 deletion Sources/SwiftFormat/Utilities/FileIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ public struct FileIterator: Sequence, IteratorProtocol {
relativePath = path
}
output = URL(fileURLWithPath: relativePath, isDirectory: false, relativeTo: workingDirectory)

default:
break
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-format/Frontend/Frontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class Frontend {
/// Read and prepare the file at the given path for processing, optionally synchronizing
/// diagnostic output.
private func openAndPrepareFile(at url: URL) -> FileToProcess? {
guard !IgnoreFile.isStandardIgnoreFile(url) else {
guard url.lastPathComponent != IgnoreFile.standardFileName else {
diagnosticsEngine.emitError(
"Invalid ignore file \(url.relativePath): currently the only supported content for ignore files is a single asterisk `*`, which matches all files."
)
Expand Down
127 changes: 106 additions & 21 deletions Tests/SwiftFormatTests/Core/IgnoreFileTests.swift
Original file line number Diff line number Diff line change
@@ -1,41 +1,126 @@
import SwiftFormat
@_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 = Bundle.module.url(forResource: "missing", withExtension: "", subdirectory: "Ignore Files")
XCTAssertNotNil(url)
XCTAssertNil(try IgnoreFile(forDirectory: url!))
XCTAssertNil(try IgnoreFile(for: url!.appending(path:"file.swift")))
let url = URL(filePath: "/")
XCTAssertNil(try IgnoreFile(forDirectory: url))
XCTAssertNil(try IgnoreFile(for: url.appending(path: "file.swift")))
}

func testValidIgnoreFile() throws {
let url = Bundle.module.url(forResource: "valid", withExtension: "", subdirectory: "Ignore Files")
XCTAssertNotNil(url)
XCTAssertNotNil(try IgnoreFile(forDirectory: url!))
XCTAssertNotNil(try IgnoreFile(for: url!.appending(path:"file.swift")))
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 = Bundle.module.url(forResource: "invalid", withExtension: "", subdirectory: "Ignore Files")
XCTAssertNotNil(url)
XCTAssertThrowsError(try IgnoreFile(forDirectory: url!))
XCTAssertThrowsError(try IgnoreFile(for: url!.appending(path:"file.swift")))
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 {
let url = Bundle.module.url(forResource: "empty", withExtension: "", subdirectory: "Ignore Files")
XCTAssertNotNil(url)
XCTAssertThrowsError(try IgnoreFile(forDirectory: url!))
XCTAssertThrowsError(try IgnoreFile(for: url!.appending(path:"file.swift")))
XCTAssertThrowsError(try IgnoreFile(""))
}

func testNestedIgnoreFile() throws {
let url = Bundle.module.url(forResource: "nested", withExtension: "", subdirectory: "Ignore Files")
XCTAssertNotNil(url)
let subdirectory = url!.appendingPathComponent("subdirectory").appending(path: "file.swift")
XCTAssertNotNil(try IgnoreFile(for: subdirectory))
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)
print(files)
XCTAssertEqual(files.count, 1)
XCTAssertEqual(files.first?.lastPathComponent, "file.swift")
}

}
Empty file.

This file was deleted.

This file was deleted.

0 comments on commit c9a14e3

Please sign in to comment.