Skip to content

Commit 4dfffaa

Browse files
authored
Run tests route (#319)
* Implements the extension entry point `runTests` * adds missing files * Adds the runTest capability
1 parent de9f682 commit 4dfffaa

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

nimlangserver.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ proc registerRoutes(srv: RpcSocketServer, ls: LanguageServer) =
6767
srv.register("extension/tasks", wrapRpc(partial(tasks, ls)))
6868
srv.register("extension/runTask", wrapRpc(partial(runTask, ls)))
6969
srv.register("extension/listTests", wrapRpc(partial(listTests, ls)))
70+
srv.register("extension/runTests", wrapRpc(partial(runTests, ls)))
7071
#Notifications
7172
srv.register("$/cancelRequest", wrapRpc(partial(cancelRequest, ls)))
7273
srv.register("initialized", wrapRpc(partial(initialized, ls)))

protocol/types.nim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,8 @@ type
10031003
LspExtensionCapability* = enum
10041004
#List of extensions this server support. Useful for clients
10051005
excRestartSuggest = "RestartSuggest",
1006-
excNimbleTask = "NimbleTask"
1006+
excNimbleTask = "NimbleTask",
1007+
excRunTests = "RunTests"
10071008

10081009
ProjectError* = object
10091010
projectFile*: string
@@ -1076,5 +1077,8 @@ type
10761077
time*: float
10771078
testResults*: seq[RunTestResult]
10781079

1080+
RunTestParams* = object
1081+
entryPoints*: seq[string]
1082+
10791083
RunTestProjectResult* = object
1080-
suites*: seq[RunTestSuiteResult]
1084+
suites*: seq[RunTestSuiteResult]

routes.nim

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,10 +854,21 @@ proc listTests*(
854854
let config = await ls.getWorkspaceConfiguration()
855855
let nimPath = config.getNimPath()
856856
if nimPath.isNone:
857+
error "Nim path not found when listing tests"
857858
return ListTestsResult(projectInfo: TestProjectInfo(entryPoints: params.entryPoints, suites: initTable[string, TestSuiteInfo]()))
858-
let testProjectInfo = await ls.listTestsForEntryPoint(params.entryPoints, nimPath.get())
859+
let testProjectInfo = await listTests(params.entryPoints, nimPath.get())
859860
result.projectInfo = testProjectInfo
860861

862+
proc runTests*(
863+
ls: LanguageServer, params: RunTestParams
864+
): Future[RunTestProjectResult] {.async.} =
865+
let config = await ls.getWorkspaceConfiguration()
866+
let nimPath = config.getNimPath()
867+
if nimPath.isNone:
868+
error "Nim path not found when running tests"
869+
return RunTestProjectResult()
870+
await runTests(params.entryPoints, nimPath.get())
871+
861872
#Notifications
862873
proc initialized*(ls: LanguageServer, _: JsonNode): Future[void] {.async.} =
863874
debug "Client initialized."

testrunner.nim

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ proc extractTestInfo*(rawOutput: string): TestProjectInfo =
3030
# echo "Adding test: ", testInfo.name, " to suite: ", suiteName
3131
result.suites[suiteName].tests.add(testInfo)
3232

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

60-
6160
proc parseObject(obj: var object, node: XmlNode) =
6261
for field, value in obj.fieldPairs:
6362
when value is string:

tests/textensions.nim

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ suite "Nimlangserver extensions":
7272
check tasks[0].name == "helloWorld"
7373
check tasks[0].description == "hello world"
7474

75-
test "calling extension/test should return all existing tests":
75+
test "calling extension/listTests should return all existing tests":
7676
#We first need to initialize the nimble project
7777
let projectDir = getCurrentDir() / "tests" / "projects" / "testrunner"
7878
cd projectDir:
@@ -98,3 +98,25 @@ suite "Nimlangserver extensions":
9898
check testProjectInfo.suites["Sample Tests"].tests[0].name == "Sample Test"
9999
check testProjectInfo.suites["Sample Tests"].tests[0].file == "sampletests.nim"
100100
check testProjectInfo.suites["Sample Tests"].tests[0].line == 4
101+
102+
test "calling extension/runTests should run the tests and return the results":
103+
let initParams =
104+
InitializeParams %* {
105+
"processId": %getCurrentProcessId(),
106+
"rootUri": fixtureUri("projects/testrunner/"),
107+
"capabilities":
108+
{"window": {"workDoneProgress": true}, "workspace": {"configuration": true}},
109+
}
110+
let initializeResult = waitFor client.initialize(initParams)
111+
112+
let runTestsParams = RunTestParams(entryPoints: @["tests/projects/testrunner/tests/sampletests.nim".absolutePath])
113+
let runTestsRes = client.call("extension/runTests", jsonutils.toJson(runTestsParams)).waitFor().jsonTo(
114+
RunTestProjectResult
115+
)
116+
check runTestsRes.suites.len == 4
117+
check runTestsRes.suites[0].name == "Sample Tests"
118+
check runTestsRes.suites[0].tests == 1
119+
check runTestsRes.suites[0].failures == 0
120+
check runTestsRes.suites[0].errors == 0
121+
check runTestsRes.suites[0].skipped == 0
122+
check runTestsRes.suites[0].time > 0.0 and runTestsRes.suites[0].time < 1.0

0 commit comments

Comments
 (0)