-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
|
||
##### Bug Fixes | ||
|
||
- None. | ||
- Fix index/report exclude regression. | ||
|
||
## 2.12.2 (2023-03-19) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
Tests/PeripheryTests/Extensions/FilenameMatcherTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |