Skip to content

Commit

Permalink
🐛 fix: file options (include/exclude) without wildcards
Browse files Browse the repository at this point in the history
resolves #73
  • Loading branch information
MarcoEidinger committed Jul 21, 2023
1 parent 0e9a0a0 commit 89d8f13
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
19 changes: 19 additions & 0 deletions Sources/SwiftPlantUMLFramework/Configuration/FileOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,22 @@ public struct FileOptions: Codable {
/// paths to Swift source files to be excluded
public var exclude: [String]? = []
}

extension FileOptions: CustomStringConvertible {
public var description: String {
let includeArray = include ?? []
let excludeArray = exclude ?? []
if includeArray.isEmpty && excludeArray.isEmpty {
return "no values"
} else {
if includeArray.isEmpty {
return "exclude: \(excludeArray.joined(separator: ", "))"
} else if excludeArray.isEmpty {
return "include: \(includeArray.joined(separator: ", "))"
} else {
return "include: \(includeArray.joined(separator: ", ")) && exclude: \(excludeArray.joined(separator: ", "))"
}
}

}
}
13 changes: 11 additions & 2 deletions Sources/SwiftPlantUMLFramework/FileManagement/FileCollector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ public struct FileCollector {

if let include = fileOptions?.include, !include.isEmpty {
searchPaths = ["."]
Logger.shared.info("paths will be ignored in favor of configuration (files:include: \(include.joined(separator: ", ")))")
}

let allFiles = getFiles(for: searchPaths, in: directory)
var filesExcluded: [URL] = []

if let exclude = fileOptions?.exclude, !exclude.isEmpty {
excluded = expandGlobs(exclude.joined(separator: ","), in: directory)
} else {
filesNotExcluded = allFiles
}

if let fileOptions = fileOptions, fileOptions.someProvided {
Logger.shared.info("paths will be ignored in favor of configuration \(fileOptions.description)")
}

excluded.forEach { glob in
filesExcluded = allFiles.filter { glob.matches($0.path) == true }
let notExcluded = allFiles.filter { glob.matches($0.path) == false }
Expand Down Expand Up @@ -97,3 +100,9 @@ public struct FileCollector {
}
}
}

fileprivate extension FileOptions {
var someProvided: Bool {
include?.isEmpty == false || exclude?.isEmpty == false
}
}
3 changes: 1 addition & 2 deletions Sources/SwiftPlantUMLFramework/FileManagement/Glob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal enum Glob: CustomStringConvertible {
internal func matches(_ path: String) -> Bool {
switch self {
case let .path(_path):
return _path == path
return _path == path || path.contains(_path)
case let .regex(regex):
let range = NSRange(location: 0, length: path.utf16.count)
return regex.firstMatch(in: path, options: [], range: range) != nil
Expand Down Expand Up @@ -101,7 +101,6 @@ internal func expandGlobs(_ paths: String, in directory: String) -> [Glob] {
}
}

// NOTE: currently only used for testing
func matchGlobs(_ globs: [Glob], in directory: String) -> [URL] {
var urls = [URL]()
let keys: [URLResourceKey] = [.isDirectoryKey]
Expand Down
12 changes: 12 additions & 0 deletions Tests/SwiftPlantUMLFrameworkTests/FileCollectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ final class FileCollectorTests: XCTestCase {
let files = FileCollector().getFiles(for: ["."], in: testDir, honoring: FileOptions(include: ["Level 0.swift"], exclude: ["Level 0.swift"]))
XCTAssertEqual(files.count, 0)
}

func testFilesStartingFromDirectoryExcludingDirectories() {
let testDir = TestResources.path.stringByAppendingPathComponent("ProjectMock")
let files = FileCollector().getFiles(for: ["."], in: testDir, honoring: FileOptions(include: nil, exclude: ["Level 1/Level2/Level3"]))
XCTAssertEqual(files.count, 6)
}

func testFilesStartingFromDirectoryIncludngAndExcludingDirectories() {
let testDir = TestResources.path.stringByAppendingPathComponent("ProjectMock")
let files = FileCollector().getFiles(for: ["."], in: testDir, honoring: FileOptions(include: ["Level 1/Level2"], exclude: ["Level 1/Level2/Level3"]))
XCTAssertEqual(files.count, 2)
}
}
15 changes: 15 additions & 0 deletions Tests/SwiftPlantUMLFrameworkTests/FileOptionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@testable import SwiftPlantUMLFramework
import XCTest

final class FileOptionsTests: XCTestCase {
func testDescription() {
XCTAssertEqual(FileOptions().description, "no values")
XCTAssertEqual(FileOptions().description, "no values")
XCTAssertEqual(FileOptions(include: []).description, "no values")
XCTAssertEqual(FileOptions(include: ["one"]).description, "include: one")
XCTAssertEqual(FileOptions(include: ["one", "two"]).description, "include: one, two")
XCTAssertEqual(FileOptions(include: ["one", "two"], exclude: ["a"]).description, "include: one, two && exclude: a")
XCTAssertEqual(FileOptions(include: ["one", "two"], exclude: ["a", "b"]).description, "include: one, two && exclude: a, b")
}
}

8 changes: 8 additions & 0 deletions Tests/SwiftPlantUMLFrameworkTests/GlobTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ class GlobsTests: XCTestCase {
let directory = TestResources.path.stringByAppendingPathComponent("ProjectMock")
XCTAssertEqual(matchGlobs(expandGlobs(path, in: directory), in: directory).count, 1)
}

func testExpandPathWithSubdirectoryAndNoWildcard() {
let path = "Level 1/Level2"
let directory = TestResources.path.stringByAppendingPathComponent("ProjectMock")
let expand = expandGlobs(path, in: directory)
let result = matchGlobs(expand, in: directory)
XCTAssertEqual(result.count, 1)
}

// MARK: glob regex

Expand Down

0 comments on commit 89d8f13

Please sign in to comment.