129 lines
5.5 KiB
Swift
129 lines
5.5 KiB
Swift
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)
|
|
}
|
|
}
|