Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix globstar expansion logic to prevent infinite loop when file path contains ** #6174

Merged
merged 19 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions Sources/TuistSupport/Utils/Glob.swift
kyounh12 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,17 @@
import Foundation

// swiftlint:disable:next identifier_name
public let GlobBehaviorBashV3 = Glob.Behavior(
supportsGlobstar: false,
includesFilesFromRootOfGlobstar: false,
includesDirectoriesInResults: true,
includesFilesInResultsIfTrailingSlash: false
)

// swiftlint:disable:next identifier_name
public let GlobBehaviorBashV4 = Glob.Behavior(
let GlobBehaviorBashV4 = Glob.Behavior(
supportsGlobstar: true, // Matches Bash v4 with "shopt -s globstar" option
includesFilesFromRootOfGlobstar: true,
includesDirectoriesInResults: true,
includesFilesInResultsIfTrailingSlash: false
)
// swiftlint:disable:next identifier_name
public let GlobBehaviorGradle = Glob.Behavior(
supportsGlobstar: true,
includesFilesFromRootOfGlobstar: true,
includesDirectoriesInResults: false,
includesFilesInResultsIfTrailingSlash: true
)

/**
Finds files on the file system using pattern matching.
*/
public class Glob: Collection {
final class Glob: Collection {
/**
* Different glob implementations have different behaviors, so the behavior of this
* implementation is customizable.
Expand Down Expand Up @@ -66,16 +51,12 @@ public class Glob: Collection {
}
}

public static let defaultBehavior = GlobBehaviorBashV4

public let behavior: Behavior
public let behavior: Behavior = GlobBehaviorBashV4
var paths = [String]()
public var startIndex: Int { paths.startIndex }
public var endIndex: Int { paths.endIndex }

public init(pattern: String, behavior: Behavior = Glob.defaultBehavior) {
self.behavior = behavior

public init(pattern: String) {
var adjustedPattern = pattern
let hasTrailingGlobstarSlash = pattern.hasSuffix("**/")
var includeFiles = !hasTrailingGlobstarSlash
Expand Down Expand Up @@ -113,15 +94,37 @@ public class Glob: Collection {
}

private func expandGlobstar(pattern: String) -> [String] {
guard pattern.contains("**") else {
// Split pattern string by slash to find globstar.
let patternComponents = pattern.components(separatedBy: "/")

// We are only interested in the first globstar since that is where we want to separate the pattern string.
guard let pivot = patternComponents.firstIndex(of: "**") else {
fortmarek marked this conversation as resolved.
Show resolved Hide resolved
return [pattern]
}

var results = [String]()
var parts = pattern.components(separatedBy: "**")
let firstPart = parts.removeFirst()
var lastPart = parts.joined(separator: "**")

// Part before the first globstar
let firstPartLowerBound = 0
let firstPartUpperBound = pivot
let firstPartComponents: ArraySlice<String> = if firstPartLowerBound < firstPartUpperBound {
patternComponents[firstPartLowerBound ..< firstPartUpperBound]
} else {
[]
}
let firstPart = firstPartComponents.joined(separator: "/")

// Part after the first globstar
let lastPartLowerBound = pivot + 1
let lastPartUpperBound = patternComponents.count
let lastPartComponents: ArraySlice<String> = if lastPartLowerBound < lastPartUpperBound {
patternComponents[lastPartLowerBound ..< lastPartUpperBound]
} else {
[]
}
fortmarek marked this conversation as resolved.
Show resolved Hide resolved
var lastPart = lastPartComponents.joined(separator: "/")

// Find subdirectories
let fileManager = FileManager.default

var directories = fileManager.subdirectoriesResolvingSymbolicLinks(atPath: firstPart)
Expand Down
119 changes: 119 additions & 0 deletions Tests/TuistSupportTests/Utils/GlobTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import Foundation
import Path
import XCTest
@testable import TuistSupport
@testable import TuistSupportTesting

// Inspired by: https://gist.github.com/efirestone/ce01ae109e08772647eb061b3bb387c3

final class GlobTests: TuistTestCase {
let temporaryFiles = ["foo", "bar", "baz", "dir1/file1.ext", "dir1/dir2/dir3/file2.ext", "dir1/**(_:_:)/file3.ext"]
private var temporaryDirectory: AbsolutePath!

override func setUpWithError() throws {
super.setUp()

temporaryDirectory = try temporaryPath()
try createFiles(temporaryFiles, content: "")
}

func testNothingMatches() {
let pattern = "nothing"
XCTAssertEmpty(temporaryDirectory.glob(pattern))
}

func testBraces() {
let pattern = "ba{r,y,z}"
XCTAssertEqual(
temporaryDirectory.glob(pattern),
[temporaryDirectory.appending(component: "bar"), temporaryDirectory.appending(component: "baz")]
)
}

// MARK: - Globstar - Bash v4

func testGlobstarNoSlash() {
// Should be the equivalent of "ls -d -1 /(temporaryDirectory)/**"
let expected: [String] = [
".",
"bar",
"baz",
"dir1",
"dir1/**(_:_:)",
"dir1/**(_:_:)/file3.ext",
"dir1/dir2",
"dir1/dir2/dir3",
"dir1/dir2/dir3/file2.ext",
"dir1/file1.ext",
"foo",
]

let result = temporaryDirectory.glob("**").map { $0.relative(to: temporaryDirectory).pathString }
XCTAssertEqual(result, expected)
}

func testGlobstarWithSlash() {
// `**/` is treated as same as `**` with Tuist since it converts pattern string to RelativePath.
// This is not an expected behavior for bash glob but this should be kept to avoid unexpected source file drops.
let expected: [String] = [
".",
"bar",
"baz",
"dir1",
"dir1/**(_:_:)",
"dir1/**(_:_:)/file3.ext",
"dir1/dir2",
"dir1/dir2/dir3",
"dir1/dir2/dir3/file2.ext",
"dir1/file1.ext",
"foo",
]

let result = temporaryDirectory.glob("**/").map { $0.relative(to: temporaryDirectory).pathString }
XCTAssertEqual(result, expected)
}

func testGlobstarWithSlashAndWildcard() {
// Should be the equivalent of "ls -d -1 /(temporaryDirectory)/**/*"
let expected: [String] = [
"bar",
"baz",
"dir1",
"dir1/**(_:_:)",
"dir1/**(_:_:)/file3.ext",
"dir1/dir2",
"dir1/dir2/dir3",
"dir1/dir2/dir3/file2.ext",
"dir1/file1.ext",
"foo",
]

let result = temporaryDirectory.glob("**/*").map { $0.relative(to: temporaryDirectory).pathString }
XCTAssertEqual(result, expected)
}

func testPatternEndsWithGlobstar() {
let expected: [String] = [
"dir1",
"dir1/**(_:_:)",
"dir1/**(_:_:)/file3.ext",
"dir1/dir2",
"dir1/dir2/dir3",
"dir1/dir2/dir3/file2.ext",
"dir1/file1.ext",
]

let result = temporaryDirectory.glob("dir1/**").map { $0.relative(to: temporaryDirectory).pathString }
XCTAssertEqual(result, expected)
}

func testDoubleGlobstar() {
let expected: [String] = [
"dir1/dir2/dir3",
"dir1/dir2/dir3/file2.ext",
]

let result = temporaryDirectory.glob("**/dir2/**/*").map { $0.relative(to: temporaryDirectory).pathString }
XCTAssertEqual(result, expected)
}
}