Skip to content

Commit

Permalink
Support regix for config’s excludes
Browse files Browse the repository at this point in the history
  • Loading branch information
mono0926 committed May 8, 2017
1 parent a49c574 commit c70384f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
34 changes: 33 additions & 1 deletion Sources/LicensePlistCore/Entity/Config.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import Foundation
import LoggerAPI

struct Config {
let githubs: [GitHub]
let excludes: [String]

func excluded(name: String) -> Bool {
return excludes.contains(name)
if excludes.contains(name) {
return true
}
for text in excludes {
if let pattern = type(of: self).extractRegex(text), let regex = try? NSRegularExpression(pattern: pattern, options: []) {
let nsName = name as NSString
let matches = regex.matches(in: name, options: [], range: NSRange(location: 0, length: nsName.length))
assert(matches.count <= 1)
if !matches.isEmpty {
return true
}
}
}
return false
}

static func extractRegex(_ text: String) -> String? {
let nsText = text as NSString
let regex = try! NSRegularExpression(pattern: "^/(.+)/$", options: [])
let matches = regex.matches(in: text, options: [], range: NSRange(location: 0, length: nsText.length))
if matches.count > 1 {
Log.warning("\(text) contains multiple regex pattern(sandwitched by `/`), but those are ignored except for first one.")
}
guard let match = matches.first else {
return nil
}
let numberOfRanges = match.numberOfRanges
guard numberOfRanges == 2 else {
assert(false, "maybe invalid regular expression to: \(nsText.substring(with: match.range))")
return nil
}
return nsText.substring(with: match.rangeAt(1))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/LicensePlistCore/Entity/HasName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension Array where Element: HasName {
guard let config = config else { return true }
let result = !config.excluded(name: name)
if !result {
Log.warning("\(type(of: self))'s \(name) was excluded according to config yaml.")
Log.warning("\(type(of: Element.self))'s \(name) was excluded according to config yaml.")
}
return result
}
Expand Down
16 changes: 12 additions & 4 deletions Tests/LicensePlistTests/Entity/ConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import XCTest

class ConfigTests: XCTestCase {

func testExcluded_true() {
func testExcluded() {
let target = Config(githubs: [], excludes: ["lib1"])
XCTAssertTrue(target.excluded(name: "lib1"))
XCTAssertFalse(target.excluded(name: "lib2"))
}

func testExcluded_false() {
let target = Config(githubs: [], excludes: ["lib1"])
XCTAssertFalse(target.excluded(name: "lib2"))
func testExcluded_isRegex() {
XCTAssertEqual(Config.extractRegex("/^Core.*$/"), "^Core.*$")
XCTAssertNil(Config.extractRegex("/^Core.*$/a"))
}

func testExcluded_regex() {
let target = Config(githubs: [], excludes: ["/^lib.*$/"])
XCTAssertTrue(target.excluded(name: "lib1"))
XCTAssertTrue(target.excluded(name: "lib2"))
XCTAssertFalse(target.excluded(name: "hello"))
}
}

0 comments on commit c70384f

Please sign in to comment.