Skip to content

Commit

Permalink
Fix index/report exclude regression. Closes #595 (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
ileitch authored Mar 21, 2023
1 parent c4adeac commit 987cd4e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

##### Bug Fixes

- None.
- Fix index/report exclude regression.

## 2.12.2 (2023-03-19)

Expand Down
8 changes: 2 additions & 6 deletions Sources/Shared/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,9 @@ public final class Configuration {
// MARK: - Private

private func buildFilenameMatchers(with patterns: [String]) -> [FilenameMatcher] {
// TODO: respect filesystem case sensitivity.
let pwd = FilePath.current.string

return patterns.map {
let pattern = $0.hasPrefix("/") ? $0 : "\(pwd)/\($0)"
// TODO: respect filesystem case sensitivity.
return FilenameMatcher(pattern: pattern, caseSensitive: false)
}
return patterns.map { FilenameMatcher(relativePattern: $0, to: pwd, caseSensitive: false) }
}

private func configurationPath(withUserProvided path: FilePath?) throws -> FilePath? {
Expand Down
23 changes: 23 additions & 0 deletions Sources/Shared/Extensions/FilenameMatcher+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation
import FilenameMatcher

public extension FilenameMatcher {
init(relativePattern: String, to base: String, caseSensitive: Bool) {
let patternComponents = relativePattern.split(separator: "/", omittingEmptySubsequences: false)
let parentTraversalCount = patternComponents.firstIndex { $0 != ".." } ?? 0

if parentTraversalCount > base.split(separator: "/").count {
self.init(pattern: "")
return
}

let baseComponents = base.split(separator: "/", omittingEmptySubsequences: false)
let traversedPattern = patternComponents.dropFirst(parentTraversalCount).joined(separator: "/")
let traversedBaseParts = baseComponents.dropLast(parentTraversalCount)
let traversedBase = traversedBaseParts.joined(separator: "/")
let normalizedBase = traversedBase.hasSuffix("/") ? traversedBase : "\(traversedBase)/"
let shouldPrependPwd = !["/", "*"].contains { relativePattern.hasPrefix($0) }
let pattern = shouldPrependPwd ? "\(normalizedBase)\(traversedPattern)" : traversedPattern
self.init(pattern: pattern, caseSensitive: caseSensitive)
}
}
43 changes: 43 additions & 0 deletions Tests/PeripheryTests/Extensions/FilenameMatcherTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Foundation
import Shared
import XCTest
import FilenameMatcher

final class FilenameMatcherTests: XCTestCase {
func testRelativePatterns() {
assertMatch(filename: "/Sources/File.swift", relativeTo: "/", pattern: "/Sources/File.swift")
assertMatch(filename: "/Sources/File.swift", relativeTo: "/", pattern: "Sources/File.swift")
assertMatch(filename: "/Sources/File.swift", relativeTo: "/", pattern: "*/Sources/File.swift")
assertMatch(filename: "/Sources/File.swift", relativeTo: "/", pattern: "**/Sources/File.swift")

assertMatch(filename: "/a/b/Sources/File.swift", relativeTo: "/", pattern: "*/Sources/File.swift")
assertMatch(filename: "/a/b/Sources/File.swift", relativeTo: "/", pattern: "**/Sources/File.swift")
assertMatch(filename: "/a/b/Sources/File.swift", relativeTo: "/", pattern: "a/**/File.swift")

assertMatch(filename: "/a/b/Sources/File.swift", relativeTo: "/a", pattern: "*/Sources/File.swift")
assertMatch(filename: "/a/b/Sources/File.swift", relativeTo: "/a", pattern: "**/Sources/File.swift")
assertMatch(filename: "/a/b/c/Sources/File.swift", relativeTo: "/b", pattern: "/**/File.swift")

assertMatch(filename: "/a/Sources/File.swift", relativeTo: "/a/b", pattern: "../Sources/File.swift")
assertMatch(filename: "/a/Sources/File.swift", relativeTo: "/a/b/c/d", pattern: "../../../Sources/*.swift")
assertMatch(filename: "/a/b/c/d/Sources/File.swift", relativeTo: "/a/b", pattern: "../*/Sources/File.swift")
assertMatch(filename: "/a/b/c/d/Sources/File.swift", relativeTo: "/a/b", pattern: "../**/File.swift")
assertMatch(filename: "/a/b/c/d/Sources/File.swift", relativeTo: "/a/b/c", pattern: "../../**/File.swift")

assertNotMatch(filename: "/Sources/File.swift", relativeTo: "/", pattern: "/x/**/Sources/File.swift")
assertNotMatch(filename: "/Sources/File.swift", relativeTo: "/", pattern: "../../**/File.swift")
assertNotMatch(filename: "/a/Sources/File.swift", relativeTo: "/", pattern: "/Sources/File.swift")
}

// MARK: - Private

private func assertMatch(filename: String, relativeTo base: String, pattern: String, file: StaticString = #file, line: UInt = #line) {
let matcher = FilenameMatcher(relativePattern: pattern, to: base, caseSensitive: false)
XCTAssertTrue(matcher.match(filename: filename), file: file, line: line)
}

private func assertNotMatch(filename: String, relativeTo base: String, pattern: String, file: StaticString = #file, line: UInt = #line) {
let matcher = FilenameMatcher(relativePattern: pattern, to: base, caseSensitive: false)
XCTAssertFalse(matcher.match(filename: filename), file: file, line: line)
}
}

0 comments on commit 987cd4e

Please sign in to comment.