restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
@testable import HelderServices
|
||||
import Testing
|
||||
|
||||
@Suite struct ProjectScannerTests {
|
||||
private let defaults = ScanFilter(base: IgnoreRules.ripgrepBaseIgnore, files: HelderConfig.Files())
|
||||
|
||||
private func walk(_ dir: TempDirectory) -> WalkScanner {
|
||||
WalkScanner(root: dir.root, runner: ProcessRunner())
|
||||
}
|
||||
|
||||
// MARK: ripgrep arguments
|
||||
|
||||
@Test func contentSearchPassesTheOldAppsRipgrepFlags() {
|
||||
let query = LineQuery(pattern: .smartCaseLiteral("needle"), maxPerFile: 50, maxColumns: 2000)
|
||||
let args = RipgrepScanner.searchArguments(query, filter: defaults, target: "/p")
|
||||
#expect(Array(args.prefix(9)) == ["--json", "--fixed-strings", "--smart-case", "--hidden", "--max-count", "50", "--max-columns", "2000", "--glob"])
|
||||
#expect(args.suffix(4) == ["-e", "needle", "--", "/p"])
|
||||
#expect(args.contains("!node_modules"))
|
||||
#expect(args.contains("--no-ignore"))
|
||||
}
|
||||
|
||||
@Test func aRegexSearchOverPHPFilesIsCaseSensitive() {
|
||||
let filter = ScanFilter(base: IgnoreRules.symbolDeclarationIgnore, files: HelderConfig.Files())
|
||||
let args = RipgrepScanner.searchArguments(LineQuery(pattern: .regex("^class"), fileExtension: "php"), filter: filter, target: "/p")
|
||||
#expect(Array(args.prefix(4)) == ["--json", "--hidden", "--glob", "*.php"])
|
||||
#expect(!args.contains("--smart-case"))
|
||||
#expect(!args.contains("!vendor"))
|
||||
#expect(args.suffix(4) == ["-e", "^class", "--", "/p"])
|
||||
}
|
||||
|
||||
@Test func aWordSearchUsesWordRegexp() {
|
||||
let args = RipgrepScanner.searchArguments(LineQuery(pattern: .word("Agent"), fileExtension: "php"), filter: defaults, target: "/p")
|
||||
#expect(args.contains("--word-regexp"))
|
||||
#expect(args.suffix(4) == ["-e", "Agent", "--", "/p"])
|
||||
}
|
||||
|
||||
@Test func fileListingHonoursExcludesAndGitignore() {
|
||||
var files = HelderConfig.Files()
|
||||
files.exclude = ["*.log", ""]
|
||||
files.followGitignore = true
|
||||
let args = RipgrepScanner.fileArguments(filter: ScanFilter(base: ["vendor"], files: files), target: "/p/src")
|
||||
#expect(args == ["--files", "--hidden", "--glob", "!vendor", "--glob", "!*.log", "--", "/p/src"])
|
||||
}
|
||||
|
||||
@Test func aMissingRipgrepBinaryYieldsNoResultsInsteadOfThrowing() async {
|
||||
let scanner = RipgrepScanner(root: URL(fileURLWithPath: "/tmp"), binary: URL(fileURLWithPath: "/nonexistent/rg"),
|
||||
runner: ProcessRunner())
|
||||
#expect(await scanner.files(under: nil, filter: defaults).isEmpty)
|
||||
}
|
||||
|
||||
@Test func rgBinaryInTheEnvironmentWinsOverTheLocator() {
|
||||
#expect(ProjectScanner.ripgrep(locator: Ripgrep.absent, environment: ["RG_BINARY": "/bin/sh"])?.path == "/bin/sh")
|
||||
#expect(ProjectScanner.ripgrep(locator: Ripgrep.absent, environment: ["RG_BINARY": "/nonexistent"]) == nil)
|
||||
#expect(ProjectScanner.ripgrep(locator: Ripgrep.absent, environment: [:]) == nil)
|
||||
}
|
||||
|
||||
// MARK: walk
|
||||
|
||||
@Test func theWalkListsFilesAndDotfilesButNotTheBaseIgnores() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("src/a.ts", "a")
|
||||
try dir.write(".env", "x")
|
||||
try dir.write(".DS_Store", "x")
|
||||
try dir.write("node_modules/p/i.js", "x")
|
||||
try dir.write("lib/vendor/v.php", "x")
|
||||
#expect(await walk(dir).files(under: nil, filter: defaults) == [".env", "src/a.ts"])
|
||||
#expect(await walk(dir).files(under: "src", filter: defaults) == ["src/a.ts"])
|
||||
}
|
||||
|
||||
@Test func theWalkSkipsGitignoredFilesOnlyWhenAsked() async throws {
|
||||
let repo = try TempGitRepo()
|
||||
try repo.write(".gitignore", "secret.txt\nlogs/\n")
|
||||
try repo.write("secret.txt", "shh")
|
||||
try repo.write("logs/today.txt", "x")
|
||||
try repo.write("keep.txt", "ok")
|
||||
let scanner = WalkScanner(root: repo.root, runner: ProcessRunner())
|
||||
#expect(await scanner.files(under: nil, filter: defaults) == [".gitignore", "keep.txt", "logs/today.txt", "secret.txt"])
|
||||
var following = defaults
|
||||
following.files.followGitignore = true
|
||||
#expect(await scanner.files(under: nil, filter: following) == [".gitignore", "keep.txt"])
|
||||
}
|
||||
|
||||
@Test
|
||||
func theWalkHonoursExcludeGlobs() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("a.log", "x")
|
||||
try dir.write("deep/b.log", "x")
|
||||
try dir.write("keep.txt", "x")
|
||||
var filter = defaults
|
||||
filter.files.exclude = ["*.log"]
|
||||
#expect(await walk(dir).files(under: nil, filter: filter) == ["keep.txt"])
|
||||
}
|
||||
|
||||
@Test func aLowercaseQueryMatchesAnyCase() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("a.ts", "const x = 1\nconst Balance = 2\n")
|
||||
let matches = await walk(dir).matches(LineQuery(pattern: .smartCaseLiteral("balance")), filter: defaults)
|
||||
#expect(matches == [RipgrepMatch(path: dir.root.path + "/a.ts", line: 2, text: "const Balance = 2", byteOffset: 6)])
|
||||
}
|
||||
|
||||
@Test func anUppercaseQueryIsCaseSensitive() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("a.ts", "balance\nBalance\n")
|
||||
let matches = await walk(dir).matches(LineQuery(pattern: .smartCaseLiteral("Balance")), filter: defaults)
|
||||
#expect(matches.map(\.line) == [2])
|
||||
}
|
||||
|
||||
@Test func theByteOffsetCountsUTF8Bytes() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("a.txt", "héllo wörld\n")
|
||||
let matches = await walk(dir).matches(LineQuery(pattern: .smartCaseLiteral("wörld")), filter: defaults)
|
||||
#expect(matches.first?.byteOffset == 7)
|
||||
}
|
||||
|
||||
@Test func theWalkStopsAtTheLimitPerFileAndAtTheFileLimit() async throws {
|
||||
let dir = try TempDirectory()
|
||||
for name in ["a", "b", "c"] { try dir.write("\(name).txt", String(repeating: "hit\n", count: 5)) }
|
||||
let matches = await walk(dir).matches(LineQuery(pattern: .smartCaseLiteral("hit"), maxPerFile: 2, maxFiles: 2), filter: defaults)
|
||||
#expect(matches.map { "\(PathUtils.basename($0.path)):\($0.line)" } == ["a.txt:1", "a.txt:2", "b.txt:1", "b.txt:2"])
|
||||
}
|
||||
|
||||
@Test func theWalkSkipsBinaryAndOversizedFiles() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("bin.dat", Data("needle\0".utf8))
|
||||
try dir.write("big.txt", "needle\n" + String(repeating: "x", count: 400))
|
||||
try dir.write("ok.txt", "needle\n")
|
||||
var scanner = walk(dir)
|
||||
scanner.maxFileBytes = 100
|
||||
let matches = await scanner.matches(LineQuery(pattern: .smartCaseLiteral("needle")), filter: defaults)
|
||||
#expect(matches.map { PathUtils.basename($0.path) } == ["ok.txt"])
|
||||
}
|
||||
|
||||
@Test func aRegexQueryReadsOnlyTheRequestedExtension() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("A.php", "<?php\nnamespace App;\nfinal class A extends B\n")
|
||||
try dir.write("notes.md", "class NotPHP\n")
|
||||
let query = LineQuery(pattern: .regex(PHPSymbols.declarationPattern), fileExtension: "php")
|
||||
let matches = await walk(dir).matches(query, filter: defaults)
|
||||
#expect(matches.map(\.text) == ["namespace App;", "final class A extends B"])
|
||||
}
|
||||
|
||||
@Test func aWordQueryNeedsWordBoundaries() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("a.php", "new Agent();\nnew AgentList();\n$x = Agent::class;\n")
|
||||
let matches = await walk(dir).matches(LineQuery(pattern: .word("Agent")), filter: defaults)
|
||||
#expect(matches.map(\.line) == [1, 3])
|
||||
#expect(matches.map(\.byteOffset) == [4, 5])
|
||||
}
|
||||
|
||||
@Test(.enabled(if: Ripgrep.isPresent))
|
||||
func ripgrepAndTheWalkFindTheSameLines() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("src/a.php", "<?php\nclass Agent extends Base\n{\n use HasFactory;\n}\n")
|
||||
try dir.write("src/b.php", "<?php\n$a = new Agent();\n")
|
||||
try dir.write("vendor/c.php", "<?php\nclass Agent {}\n")
|
||||
let rg = RipgrepScanner(root: dir.root, binary: try #require(Ripgrep.binary), runner: ProcessRunner())
|
||||
for query in [LineQuery(pattern: .word("Agent"), fileExtension: "php"),
|
||||
LineQuery(pattern: .regex(PHPSymbols.declarationPattern), fileExtension: "php"),
|
||||
LineQuery(pattern: .smartCaseLiteral("agent"))] {
|
||||
let sort: (RipgrepMatch, RipgrepMatch) -> Bool = { ($0.path, $0.line) < ($1.path, $1.line) }
|
||||
#expect(await rg.matches(query, filter: defaults).sorted(by: sort) == walk(dir).matches(query, filter: defaults).sorted(by: sort))
|
||||
}
|
||||
}
|
||||
|
||||
@Test(.enabled(if: Ripgrep.isPresent))
|
||||
func ripgrepAndTheWalkListTheSameFiles() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("src/a.ts", "a")
|
||||
try dir.write(".env", "x")
|
||||
try dir.write(".DS_Store", "x")
|
||||
try dir.write("node_modules/p/i.js", "x")
|
||||
try dir.mkdir("empty")
|
||||
let rg = RipgrepScanner(root: dir.root, binary: try #require(Ripgrep.binary), runner: ProcessRunner())
|
||||
#expect(await rg.files(under: nil, filter: defaults) == walk(dir).files(under: nil, filter: defaults))
|
||||
#expect(await rg.files(under: "src", filter: defaults) == ["src/a.ts"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import HelderServices
|
||||
import Testing
|
||||
|
||||
@Suite struct RipgrepSearchServiceTests {
|
||||
private func withRipgrep(_ dir: URL, config: HelderConfig = .defaults) -> RipgrepSearchService {
|
||||
RipgrepSearchService(root: dir, config: FixedConfig(config: config), locator: Ripgrep.locator, environment: [:])
|
||||
}
|
||||
|
||||
private func withoutRipgrep(_ dir: URL, config: HelderConfig = .defaults) -> RipgrepSearchService {
|
||||
RipgrepSearchService(root: dir, config: FixedConfig(config: config), locator: Ripgrep.absent, environment: [:])
|
||||
}
|
||||
|
||||
/// a.ts holds "balance" on line 2; b.ts holds nothing relevant.
|
||||
private func fixture() throws -> TempDirectory {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("a.ts", "const x = 1\nconst balance = 2\n")
|
||||
try dir.write("b.ts", "nothing relevant\n")
|
||||
return dir
|
||||
}
|
||||
|
||||
@Test func availabilityFollowsTheLocatorAndRgBinary() async throws {
|
||||
let dir = try TempDirectory()
|
||||
#expect(await !withoutRipgrep(dir.root).isAvailable())
|
||||
let viaEnvironment = RipgrepSearchService(root: dir.root, config: FixedConfig(), locator: Ripgrep.absent,
|
||||
environment: ["RG_BINARY": "/bin/sh"])
|
||||
#expect(await viaEnvironment.isAvailable())
|
||||
}
|
||||
|
||||
@Test(.enabled(if: Ripgrep.isPresent))
|
||||
func ripgrepIsFoundWhereTheOldAppShipsIt() async throws {
|
||||
let dir = try TempDirectory()
|
||||
#expect(await withRipgrep(dir.root).isAvailable())
|
||||
}
|
||||
|
||||
@Test(arguments: ["", "a", " b "])
|
||||
func aQueryUnderTwoCharactersFindsNothing(_ query: String) async throws {
|
||||
let dir = try fixture()
|
||||
#expect(await withoutRipgrep(dir.root).searchContent(query).isEmpty)
|
||||
#expect(await withRipgrep(dir.root).searchContent(query).isEmpty)
|
||||
}
|
||||
|
||||
@Test(.enabled(if: Ripgrep.isPresent))
|
||||
func ripgrepFindsContentWithLineAndOffset() async throws {
|
||||
let dir = try fixture()
|
||||
let groups = await withRipgrep(dir.root).searchContent("balance")
|
||||
#expect(groups == [ContentGroup(path: "a.ts", hits: [ContentHit(line: 2, text: "const balance = 2", matchOffset: 6)])])
|
||||
}
|
||||
|
||||
@Test
|
||||
func theWalkFindsContentWithLineAndOffset() async throws {
|
||||
let dir = try fixture()
|
||||
let groups = await withoutRipgrep(dir.root).searchContent("balance")
|
||||
#expect(groups == [ContentGroup(path: "a.ts", hits: [ContentHit(line: 2, text: "const balance = 2", matchOffset: 6)])])
|
||||
}
|
||||
|
||||
@Test
|
||||
func theWalkCapsHitsPerFile() async throws {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("many.txt", String(repeating: "needle\n", count: SearchLimits.maxHitsPerFile + 10))
|
||||
let groups = await withoutRipgrep(dir.root).searchContent("needle")
|
||||
#expect(groups.first?.hits.count == SearchLimits.maxHitsPerFile)
|
||||
}
|
||||
|
||||
@Test(.enabled(if: Ripgrep.isPresent))
|
||||
func ripgrepContentSearchSkipsExcludedFiles() async throws {
|
||||
let dir = try fixture()
|
||||
try dir.write("node_modules/p/index.js", "balance\n")
|
||||
try dir.write("build.log", "balance\n")
|
||||
var config = HelderConfig.defaults
|
||||
config.files.exclude = ["*.log"]
|
||||
#expect(await withRipgrep(dir.root, config: config).searchContent("balance").map(\.path) == ["a.ts"])
|
||||
}
|
||||
|
||||
@Test(.enabled(if: Ripgrep.isPresent))
|
||||
func ripgrepListsFilesIncludingDotfiles() async throws {
|
||||
let dir = try fixture()
|
||||
try dir.write(".env", "APP_NAME=x\n")
|
||||
try dir.write(".DS_Store", "x")
|
||||
try dir.write("node_modules/p/index.js", "x")
|
||||
#expect(await withRipgrep(dir.root).listFiles(under: nil) == [".env", "a.ts", "b.ts"])
|
||||
}
|
||||
|
||||
@Test(.enabled(if: Ripgrep.isPresent))
|
||||
func ripgrepListsOneSubtreeWithRootRelativePaths() async throws {
|
||||
let dir = try fixture()
|
||||
try dir.write("src/deep/c.ts", "c")
|
||||
#expect(await withRipgrep(dir.root).listFiles(under: "src") == ["src/deep/c.ts"])
|
||||
}
|
||||
|
||||
@Test(.enabled(if: Ripgrep.isPresent))
|
||||
func ripgrepShowsGitignoredFilesUnlessTheConfigFollowsGitignore() async throws {
|
||||
let repo = try TempGitRepo()
|
||||
try repo.write(".gitignore", "secret.txt\n")
|
||||
try repo.write("secret.txt", "shh")
|
||||
try repo.write("keep.txt", "ok")
|
||||
#expect(await withRipgrep(repo.root).listFiles(under: nil) == [".gitignore", "keep.txt", "secret.txt"])
|
||||
var config = HelderConfig.defaults
|
||||
config.files.followGitignore = true
|
||||
#expect(await withRipgrep(repo.root, config: config).listFiles(under: nil) == [".gitignore", "keep.txt"])
|
||||
}
|
||||
|
||||
@Test(.enabled(if: Ripgrep.isPresent))
|
||||
func ripgrepHonoursExcludeGlobs() async throws {
|
||||
let dir = try fixture()
|
||||
try dir.write("logs/today.log", "x")
|
||||
var config = HelderConfig.defaults
|
||||
config.files.exclude = ["*.log"]
|
||||
#expect(await withRipgrep(dir.root, config: config).listFiles(under: nil) == ["a.ts", "b.ts"])
|
||||
}
|
||||
|
||||
@Test func theWalkListsFilesWithoutRipgrep() async throws {
|
||||
let dir = try fixture()
|
||||
try dir.write(".env", "x")
|
||||
try dir.write("vendor/x.php", "x")
|
||||
try dir.write("src/c.ts", "c")
|
||||
let search = withoutRipgrep(dir.root)
|
||||
#expect(await search.listFiles(under: nil) == [".env", "a.ts", "b.ts", "src/c.ts"])
|
||||
#expect(await search.listFiles(under: "src") == ["src/c.ts"])
|
||||
}
|
||||
|
||||
@Test func listingOutsideTheProjectIsEmpty() async throws {
|
||||
let dir = try fixture()
|
||||
#expect(await withoutRipgrep(dir.root).listFiles(under: "..").isEmpty)
|
||||
#expect(await withRipgrep(dir.root).listFiles(under: "../..").isEmpty)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user