From a6a74a23554a6cf873867fa07e5d7cde65e5b315 Mon Sep 17 00:00:00 2001 From: Geoffrey Foster Date: Mon, 2 Dec 2019 18:58:39 -0500 Subject: [PATCH] cleaning up --- Sources/Pathspec/GitIgnoreSpec.swift | 2 +- Sources/Pathspec/Pathspec.swift | 29 ++++++------------------- Sources/Pathspec/RegexSpec.swift | 16 -------------- Tests/PathspecTests/PathspecTests.swift | 9 +++++--- 4 files changed, 14 insertions(+), 42 deletions(-) delete mode 100644 Sources/Pathspec/RegexSpec.swift diff --git a/Sources/Pathspec/GitIgnoreSpec.swift b/Sources/Pathspec/GitIgnoreSpec.swift index 5154810..38bd512 100644 --- a/Sources/Pathspec/GitIgnoreSpec.swift +++ b/Sources/Pathspec/GitIgnoreSpec.swift @@ -7,7 +7,7 @@ import Foundation -class GitIgnoreSpec: Spec { +struct GitIgnoreSpec: Spec { private(set) var inclusive: Bool = true let regex: NSRegularExpression diff --git a/Sources/Pathspec/Pathspec.swift b/Sources/Pathspec/Pathspec.swift index dfb687a..f0b6f9b 100644 --- a/Sources/Pathspec/Pathspec.swift +++ b/Sources/Pathspec/Pathspec.swift @@ -6,33 +6,18 @@ // public final class Pathspec { - public enum Kind { - case git - case regex - } - private var specs: [Spec] = [] + private let specs: [Spec] - public init(kind: Kind, patterns: String...) { - for pattern in patterns { - add(pattern: pattern, kind: kind) + public init(patterns: String...) { + specs = patterns.compactMap { + GitIgnoreSpec(pattern: $0) } } public func match(path: String) -> Bool { - return matchingSpecs(path: path).allSatisfy { $0.inclusive } - } - - func add(pattern: String, kind: Kind) { - let spec: Spec? - switch kind { - case .git: - spec = GitIgnoreSpec(pattern: pattern) - case .regex: - spec = RegexSpec() - } - if let spec = spec { - specs.append(spec) - } + let matchingSpecs = self.matchingSpecs(path: path) + guard !matchingSpecs.isEmpty else { return false } + return matchingSpecs.allSatisfy { $0.inclusive } } private func matchingSpecs(path: String) -> [Spec] { diff --git a/Sources/Pathspec/RegexSpec.swift b/Sources/Pathspec/RegexSpec.swift deleted file mode 100644 index 157d93c..0000000 --- a/Sources/Pathspec/RegexSpec.swift +++ /dev/null @@ -1,16 +0,0 @@ -// -// RegexSpec.swift -// Pathspec -// -// Created by Geoffrey Foster on 2019-06-29. -// - -import Foundation - -class RegexSpec: Spec { - var inclusive: Bool = false - - func match(file: String) -> Bool { - return false - } -} diff --git a/Tests/PathspecTests/PathspecTests.swift b/Tests/PathspecTests/PathspecTests.swift index d494146..48deb9c 100644 --- a/Tests/PathspecTests/PathspecTests.swift +++ b/Tests/PathspecTests/PathspecTests.swift @@ -1,6 +1,6 @@ // -// File.swift -// +// PathspecTests.swift +// Pathspec // // Created by Geoffrey Foster on 2019-06-29. // @@ -255,5 +255,8 @@ final class PathspecTests: XCTestCase { ) } - // MARK: - + func testFailingInitializers() { + XCTAssertNil(GitIgnoreSpec(pattern: "")) + XCTAssertNil(GitIgnoreSpec(pattern: "***")) + } }