Skip to content

Commit

Permalink
Support recursive globs (#3843)
Browse files Browse the repository at this point in the history
ref: #3789

## Overview
Support glob recursive by using [Pathos](https://github.com/dduan/Pathos)
- Introduce Pathos
- Replace glob logic
- Fix some test cases
  • Loading branch information
funzin authored Mar 11, 2022
1 parent 92e1996 commit 2ae22d0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
[JP Simard](https://github.com/jpsim)
[#3854](https://github.com/realm/SwiftLint/issues/3854)

* Support recursive globs.
[funzin](https://github.com/funzin)
[#3789](https://github.com/realm/SwiftLint/issues/3789)

#### Bug Fixes

* Extend `class_delegate_protocol` to correctly identify cases with the protocol
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"object": {
"pins": [
{
"package": "Pathos",
"repositoryURL": "https://github.com/dduan/Pathos",
"state": {
"branch": null,
"revision": "8697a340a25e9974d4bbdee80a4c361c74963c00",
"version": "0.4.2"
}
},
{
"package": "SourceKitten",
"repositoryURL": "https://github.com/jpsim/SourceKitten.git",
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ let package = Package(
.package(url: "https://github.com/jpsim/SourceKitten.git", from: "0.31.1"),
.package(url: "https://github.com/jpsim/Yams.git", from: "4.0.2"),
.package(url: "https://github.com/scottrhoyt/SwiftyTextTable.git", from: "0.9.0"),
.package(url: "https://github.com/dduan/Pathos", from: "0.4.2")
] + (addCryptoSwift ? [.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMinor(from: "1.4.3"))] : []),
targets: [
.executableTarget(
Expand All @@ -41,8 +42,9 @@ let package = Package(
name: "SwiftLintFramework",
dependencies: [
.product(name: "SourceKittenFramework", package: "SourceKitten"),
"Pathos",
"SwiftSyntax",
"Yams",
"Yams"
]
+ (addCryptoSwift ? ["CryptoSwift"] : [])
+ (staticSwiftSyntax ? ["lib_InternalSwiftSyntaxParser"] : [])
Expand Down
35 changes: 8 additions & 27 deletions Source/SwiftLintFramework/Helpers/Glob.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import Foundation

#if canImport(Darwin)
import Darwin

private let globFunction = Darwin.glob
#elseif canImport(Glibc)
import Glibc

private let globFunction = Glibc.glob
#else
#error("Unsupported platform")
#endif
import Pathos

struct Glob {
static func resolveGlob(_ pattern: String) -> [String] {
Expand All @@ -19,22 +8,14 @@ struct Glob {
return [pattern]
}

var globResult = glob_t()
defer { globfree(&globResult) }

let flags = GLOB_TILDE | GLOB_BRACE | GLOB_MARK
guard globFunction(pattern.cString(using: .utf8)!, flags, nil, &globResult) == 0 else {
do {
let paths = try Path(pattern).glob()
return try paths.compactMap { path in
try path.absolute().description
}
} catch {
queuedPrintError(error.localizedDescription)
return []
}

#if os(Linux)
let matchCount = globResult.gl_pathc
#else
let matchCount = globResult.gl_matchc
#endif

return (0..<Int(matchCount)).compactMap { index in
return globResult.gl_pathv[index].flatMap { String(validatingUTF8: $0) }
}
}
}
13 changes: 8 additions & 5 deletions Tests/SwiftLintFrameworkTests/GlobTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Foundation
@testable import SwiftLintFramework
import XCTest

Expand Down Expand Up @@ -50,7 +49,7 @@ final class GlobTests: XCTestCase {
func testMatchesMultipleFiles() {
let expectedFiles: Set = [
mockPath.stringByAppendingPathComponent("Level0.swift"),
mockPath.stringByAppendingPathComponent("Directory.swift").appending("/")
mockPath.stringByAppendingPathComponent("Directory.swift")
]

let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("*.swift"))
Expand All @@ -63,14 +62,18 @@ final class GlobTests: XCTestCase {
XCTAssertEqual(files, [mockPath.stringByAppendingPathComponent("Level1/Level1.swift")])
}

func testNoGlobstarSupport() {
func testGlobstarSupport() {
let expectedFiles: Set = [
mockPath.stringByAppendingPathComponent("Directory.swift/DirectoryLevel1.swift"),
mockPath.stringByAppendingPathComponent("Level1/Level1.swift")
mockPath.stringByAppendingPathComponent("Level1/Level1.swift"),
mockPath.stringByAppendingPathComponent("Level1/Level2/Level2.swift"),
mockPath.stringByAppendingPathComponent("Level1/Level2/Level3/Level3.swift"),
mockPath.stringByAppendingPathComponent("NestedConfig/Test/Main.swift"),
mockPath.stringByAppendingPathComponent("NestedConfig/Test/Sub/Sub.swift")
]

let files = Glob.resolveGlob(mockPath.stringByAppendingPathComponent("**/*.swift"))
XCTAssertEqual(files.count, 2)
XCTAssertEqual(files.count, 6)
XCTAssertEqual(Set(files), expectedFiles)
}
}

0 comments on commit 2ae22d0

Please sign in to comment.