mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
ac701c088c
The current algorithm is like "collect all included files and subtract all excluded files".
Collecting all included and all excluded files relies on the file system. This can become slow
when the patterns used to exclude files resolve to a large number of files.
The new approach only collects all lintable files and checks them against the exclude patterns.
This can be done by in-memory string-regex-match and does therefore not require file system accesses.
The new implementation also no longer traverses directories that already match an exclude pattern.
(cherry picked from commit 152355e36f)
116 lines
4.9 KiB
Swift
116 lines
4.9 KiB
Swift
import FilenameMatcher
|
|
import TestHelpers
|
|
import XCTest
|
|
|
|
@testable import SwiftLintFramework
|
|
|
|
final class GlobTests: SwiftLintTestCase {
|
|
private var mockPath: String {
|
|
TestResources.path().stringByAppendingPathComponent("ProjectMock")
|
|
}
|
|
|
|
func testNonExistingDirectory() {
|
|
XCTAssertTrue(Glob.resolveGlob("./bar/**").isEmpty)
|
|
}
|
|
|
|
func testOnlyGlobForWildcard() {
|
|
let files = Glob.resolveGlob("foo/bar.swift")
|
|
XCTAssertEqual(files, ["foo/bar.swift"])
|
|
}
|
|
|
|
func testNoMatchReturnsEmpty() {
|
|
let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("NoFile*.swift"))
|
|
XCTAssertTrue(files.isEmpty)
|
|
}
|
|
|
|
func testMatchesFiles() {
|
|
let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("Level*.swift"))
|
|
XCTAssertEqual(files, [mockPath.stringByAppendingPathComponent("Level0.swift")])
|
|
}
|
|
|
|
func testMatchesSingleCharacter() {
|
|
let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("Level?.swift"))
|
|
XCTAssertEqual(files, [mockPath.stringByAppendingPathComponent("Level0.swift")])
|
|
}
|
|
|
|
func testMatchesOneCharacterInBracket() {
|
|
let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("Level[01].swift"))
|
|
XCTAssertEqual(files, [mockPath.stringByAppendingPathComponent("Level0.swift")])
|
|
}
|
|
|
|
func testNoMatchOneCharacterInBracket() {
|
|
let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("Level[ab].swift"))
|
|
XCTAssertTrue(files.isEmpty)
|
|
}
|
|
|
|
func testMatchesCharacterInRange() {
|
|
let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("Level[0-9].swift"))
|
|
XCTAssertEqual(files, [mockPath.stringByAppendingPathComponent("Level0.swift")])
|
|
}
|
|
|
|
func testNoMatchCharactersInRange() {
|
|
let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("Level[a-z].swift"))
|
|
XCTAssertTrue(files.isEmpty)
|
|
}
|
|
|
|
func testMatchesMultipleFiles() {
|
|
let expectedFiles: Set = [
|
|
mockPath.stringByAppendingPathComponent("Level0.swift"),
|
|
mockPath.stringByAppendingPathComponent("Directory.swift"),
|
|
]
|
|
|
|
let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("*.swift"))
|
|
XCTAssertEqual(files.sorted(), expectedFiles.sorted())
|
|
}
|
|
|
|
func testMatchesNestedDirectory() {
|
|
let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("Level1/*.swift"))
|
|
XCTAssertEqual(files, [mockPath.stringByAppendingPathComponent("Level1/Level1.swift")])
|
|
}
|
|
|
|
func testGlobstarSupport() {
|
|
let expectedFiles = Set(
|
|
[
|
|
"Directory.swift",
|
|
"Directory.swift/DirectoryLevel1.swift",
|
|
"Level0.swift",
|
|
"Level1/Level1.swift",
|
|
"Level1/Level2/Level2.swift",
|
|
"Level1/Level2/Level3/Level3.swift",
|
|
"NestedConfig/Test/Main.swift",
|
|
"NestedConfig/Test/Sub/Sub.swift",
|
|
].map(mockPath.stringByAppendingPathComponent)
|
|
)
|
|
|
|
let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("**/*.swift"))
|
|
XCTAssertEqual(files.sorted(), expectedFiles.sorted())
|
|
}
|
|
|
|
func testCreateFilenameMatchers() {
|
|
func assertGlobMatch(root: String = "", pattern: String, filename: String) {
|
|
let matchers = Glob.createFilenameMatchers(root: root, pattern: pattern)
|
|
XCTAssert(matchers.anyMatch(filename: filename))
|
|
}
|
|
|
|
assertGlobMatch(root: "/a/b/", pattern: "c/*.swift", filename: "/a/b/c/d.swift")
|
|
assertGlobMatch(root: "/a", pattern: "**/*.swift", filename: "/a/b/c/d.swift")
|
|
assertGlobMatch(root: "/a", pattern: "**/*.swift", filename: "/a/b.swift")
|
|
assertGlobMatch(root: "/", pattern: "**/*.swift", filename: "/a/b.swift")
|
|
assertGlobMatch(root: "/", pattern: "a/**/b.swift", filename: "/a/b.swift")
|
|
assertGlobMatch(root: "/", pattern: "a/**/b.swift", filename: "/a/c/b.swift")
|
|
assertGlobMatch(root: "/", pattern: "**/*.swift", filename: "/a.swift")
|
|
assertGlobMatch(root: "/", pattern: "a/**/*.swift", filename: "/a/b/c.swift")
|
|
assertGlobMatch(root: "/", pattern: "a/**/*.swift", filename: "/a/b.swift")
|
|
assertGlobMatch(root: "/a/b", pattern: "/a/b/c/*.swift", filename: "/a/b/c/d.swift")
|
|
assertGlobMatch(root: "/a/", pattern: "/a/b/c/*.swift", filename: "/a/b/c/d.swift")
|
|
|
|
assertGlobMatch(pattern: "/a/b/c", filename: "/a/b/c/d.swift")
|
|
assertGlobMatch(pattern: "/a/b/c/", filename: "/a/b/c/d.swift")
|
|
assertGlobMatch(pattern: "/a/b/c/*.swift", filename: "/a/b/c/d.swift")
|
|
assertGlobMatch(pattern: "/d.swift/*.swift", filename: "/d.swift/e.swift")
|
|
assertGlobMatch(pattern: "/a/**", filename: "/a/b/c/d.swift")
|
|
assertGlobMatch(root: "/", pattern: "**/*Test*", filename: "/a/b/c/MyTest2.swift")
|
|
assertGlobMatch(root: "/", pattern: "**/*Test*", filename: "/a/b/MyTests/c.swift")
|
|
}
|
|
}
|