Skip to content

Run tests route #319

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

Merged
merged 3 commits into from
Apr 10, 2025
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
1 change: 1 addition & 0 deletions nimlangserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ proc registerRoutes(srv: RpcSocketServer, ls: LanguageServer) =
srv.register("extension/tasks", wrapRpc(partial(tasks, ls)))
srv.register("extension/runTask", wrapRpc(partial(runTask, ls)))
srv.register("extension/listTests", wrapRpc(partial(listTests, ls)))
srv.register("extension/runTests", wrapRpc(partial(runTests, ls)))
#Notifications
srv.register("$/cancelRequest", wrapRpc(partial(cancelRequest, ls)))
srv.register("initialized", wrapRpc(partial(initialized, ls)))
Expand Down
8 changes: 6 additions & 2 deletions protocol/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,8 @@ type
LspExtensionCapability* = enum
#List of extensions this server support. Useful for clients
excRestartSuggest = "RestartSuggest",
excNimbleTask = "NimbleTask"
excNimbleTask = "NimbleTask",
excRunTests = "RunTests"

ProjectError* = object
projectFile*: string
Expand Down Expand Up @@ -1076,5 +1077,8 @@ type
time*: float
testResults*: seq[RunTestResult]

RunTestParams* = object
entryPoints*: seq[string]

RunTestProjectResult* = object
suites*: seq[RunTestSuiteResult]
suites*: seq[RunTestSuiteResult]
13 changes: 12 additions & 1 deletion routes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,21 @@ proc listTests*(
let config = await ls.getWorkspaceConfiguration()
let nimPath = config.getNimPath()
if nimPath.isNone:
error "Nim path not found when listing tests"
return ListTestsResult(projectInfo: TestProjectInfo(entryPoints: params.entryPoints, suites: initTable[string, TestSuiteInfo]()))
let testProjectInfo = await ls.listTestsForEntryPoint(params.entryPoints, nimPath.get())
let testProjectInfo = await listTests(params.entryPoints, nimPath.get())
result.projectInfo = testProjectInfo

proc runTests*(
ls: LanguageServer, params: RunTestParams
): Future[RunTestProjectResult] {.async.} =
let config = await ls.getWorkspaceConfiguration()
let nimPath = config.getNimPath()
if nimPath.isNone:
error "Nim path not found when running tests"
return RunTestProjectResult()
await runTests(params.entryPoints, nimPath.get())

#Notifications
proc initialized*(ls: LanguageServer, _: JsonNode): Future[void] {.async.} =
debug "Client initialized."
Expand Down
5 changes: 2 additions & 3 deletions testrunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ proc extractTestInfo*(rawOutput: string): TestProjectInfo =
# echo "Adding test: ", testInfo.name, " to suite: ", suiteName
result.suites[suiteName].tests.add(testInfo)

proc listTestsForEntryPoint*(
ls: LanguageServer, entryPoints: seq[string], nimPath: string
proc listTests*(
entryPoints: seq[string], nimPath: string
): Future[TestProjectInfo] {.async.} =
#For now only one entry point is supported
assert entryPoints.len == 1
Expand All @@ -57,7 +57,6 @@ proc listTestsForEntryPoint*(
finally:
await shutdownChildProcess(process)


proc parseObject(obj: var object, node: XmlNode) =
for field, value in obj.fieldPairs:
when value is string:
Expand Down
24 changes: 23 additions & 1 deletion tests/textensions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ suite "Nimlangserver extensions":
check tasks[0].name == "helloWorld"
check tasks[0].description == "hello world"

test "calling extension/test should return all existing tests":
test "calling extension/listTests should return all existing tests":
#We first need to initialize the nimble project
let projectDir = getCurrentDir() / "tests" / "projects" / "testrunner"
cd projectDir:
Expand All @@ -98,3 +98,25 @@ suite "Nimlangserver extensions":
check testProjectInfo.suites["Sample Tests"].tests[0].name == "Sample Test"
check testProjectInfo.suites["Sample Tests"].tests[0].file == "sampletests.nim"
check testProjectInfo.suites["Sample Tests"].tests[0].line == 4

test "calling extension/runTests should run the tests and return the results":
let initParams =
InitializeParams %* {
"processId": %getCurrentProcessId(),
"rootUri": fixtureUri("projects/testrunner/"),
"capabilities":
{"window": {"workDoneProgress": true}, "workspace": {"configuration": true}},
}
let initializeResult = waitFor client.initialize(initParams)

let runTestsParams = RunTestParams(entryPoints: @["tests/projects/testrunner/tests/sampletests.nim".absolutePath])
let runTestsRes = client.call("extension/runTests", jsonutils.toJson(runTestsParams)).waitFor().jsonTo(
RunTestProjectResult
)
check runTestsRes.suites.len == 4
check runTestsRes.suites[0].name == "Sample Tests"
check runTestsRes.suites[0].tests == 1
check runTestsRes.suites[0].failures == 0
check runTestsRes.suites[0].errors == 0
check runTestsRes.suites[0].skipped == 0
check runTestsRes.suites[0].time > 0.0 and runTestsRes.suites[0].time < 1.0