restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import HelderServices
|
||||
import Testing
|
||||
|
||||
/// Each test runs once through ripgrep (when present) and once through the walk.
|
||||
@Suite struct SymbolServiceTests {
|
||||
enum Engine: String, CaseIterable, Sendable { case ripgrep, walk }
|
||||
|
||||
private static let engines: [Engine] = Ripgrep.isPresent ? Engine.allCases : [.walk]
|
||||
|
||||
private func service(_ root: URL, _ engine: Engine) -> SymbolService {
|
||||
SymbolService(root: root, config: FixedConfig(), runner: ProcessRunner(),
|
||||
locator: engine == .ripgrep ? Ripgrep.locator : Ripgrep.absent, environment: [:])
|
||||
}
|
||||
|
||||
/// A small Laravel-shaped project: a model with a trait, a controller using it, and a vendor class.
|
||||
private func project() throws -> TempDirectory {
|
||||
let dir = try TempDirectory()
|
||||
try dir.write("app/Models/Agent.php", """
|
||||
<?php
|
||||
|
||||
namespace App\\Models;
|
||||
|
||||
use Illuminate\\Database\\Eloquent\\Model;
|
||||
|
||||
final class Agent extends Model implements HasLabel
|
||||
{
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
public function run(): void {}
|
||||
}
|
||||
""")
|
||||
try dir.write("app/Http/AgentController.php", """
|
||||
<?php
|
||||
|
||||
namespace App\\Http;
|
||||
|
||||
use App\\Models\\Agent;
|
||||
|
||||
class AgentController
|
||||
{
|
||||
public function show(): Agent
|
||||
{
|
||||
return new Agent();
|
||||
}
|
||||
}
|
||||
""")
|
||||
try dir.write("vendor/laravel/Model.php", "<?php\nnamespace Illuminate\\Database\\Eloquent;\nabstract class Model {}\nclass Agent {}\n")
|
||||
try dir.write("vendor/laravel/Uses.php", "<?php\n$x = new Agent();\n")
|
||||
return dir
|
||||
}
|
||||
|
||||
@Test func aNonIdentifierLooksUpNothing() async throws {
|
||||
let dir = try TempDirectory()
|
||||
let symbols = service(dir.root, .walk)
|
||||
#expect(await symbols.lookup("not a name") == .empty("not a name"))
|
||||
#expect(await symbols.lookup("$agent") == .empty("$agent"))
|
||||
}
|
||||
|
||||
@Test(arguments: engines)
|
||||
func namesIncludeProjectAndVendorDeclarations(_ engine: Engine) async throws {
|
||||
let dir = try project()
|
||||
#expect(await service(dir.root, engine).names() == ["Agent", "AgentController", "Model"])
|
||||
}
|
||||
|
||||
@Test(arguments: engines)
|
||||
func lookupListsTheProjectDeclarationFirstWithItsNamespaceAndTraits(_ engine: Engine) async throws {
|
||||
let dir = try project()
|
||||
let result = await service(dir.root, engine).lookup("Agent")
|
||||
#expect(result.definitions == [
|
||||
SymbolDef(name: "Agent", path: "app/Models/Agent.php", line: 7, kind: "class", namespace: "App\\Models",
|
||||
parents: ["Model"], interfaces: ["HasLabel"], traits: ["HasFactory", "Notifiable"]),
|
||||
SymbolDef(name: "Agent", path: "vendor/laravel/Model.php", line: 4, kind: "class",
|
||||
namespace: "Illuminate\\Database\\Eloquent"),
|
||||
])
|
||||
}
|
||||
|
||||
@Test(arguments: engines)
|
||||
func referencesSkipImportsDeclarationsAndVendor(_ engine: Engine) async throws {
|
||||
let dir = try project()
|
||||
let result = await service(dir.root, engine).lookup("Agent")
|
||||
#expect(result.references == [RefGroup(path: "app/Http/AgentController.php", hits: [
|
||||
ContentHit(line: 9, text: " public function show(): Agent", matchOffset: 28),
|
||||
ContentHit(line: 11, text: " return new Agent();", matchOffset: 19),
|
||||
])])
|
||||
#expect(result.referenceCount == 2)
|
||||
}
|
||||
|
||||
@Test(arguments: engines)
|
||||
func invalidateMakesTheNextCallSeeANewClass(_ engine: Engine) async throws {
|
||||
let dir = try project()
|
||||
let symbols = service(dir.root, engine)
|
||||
#expect(await !symbols.names().contains("Fresh"))
|
||||
try dir.write("app/Fresh.php", "<?php\nenum Fresh: string {}\n")
|
||||
#expect(await !symbols.names().contains("Fresh"))
|
||||
await symbols.invalidate()
|
||||
#expect(await symbols.names().contains("Fresh"))
|
||||
}
|
||||
|
||||
@Test(arguments: engines)
|
||||
func concurrentCallersShareOneIndex(_ engine: Engine) async throws {
|
||||
let dir = try project()
|
||||
let symbols = service(dir.root, engine)
|
||||
let results = await withTaskGroup(of: Set<String>.self) { group in
|
||||
for _ in 0..<8 { group.addTask { await symbols.names() } }
|
||||
return await group.reduce(into: [Set<String>]()) { $0.append($1) }
|
||||
}
|
||||
#expect(Set(results).count == 1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user