Skip to content

Commit 4943401

Browse files
authored
cancelTest entry point implemented (#329)
1 parent d828118 commit 4943401

File tree

6 files changed

+25
-4
lines changed

6 files changed

+25
-4
lines changed

ls.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ type
160160
nimDumpCache*: Table[string, NimbleDumpInfo] #path to NimbleDumpInfo
161161
entryPoints*: seq[string]
162162
responseMap*: TableRef[string, Future[JsonNode]]
163+
testRunProcess*: Option[AsyncProcessRef] #There is only one test run process at a time
164+
163165
#id to future. Represents the pending requests as result of calling ls.call
164166
srv*: RpcSocketServer
165167
#Both modes uses it to store the routes. Only actually started in socket mode

nimlangserver.nim

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

protocol/types.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,3 +1088,5 @@ type
10881088
suites*: seq[RunTestSuiteResult]
10891089
fullOutput*: string
10901090

1091+
CancelTestResult* = object
1092+
cancelled*: bool

routes.nim

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,18 @@ proc runTests*(
869869
error "Nim path not found when running tests"
870870
return RunTestProjectResult()
871871
let workspaceRoot = ls.initializeParams.getRootPath
872-
await runTests(params.entryPoint, nimPath.get(), params.suiteName, params.testNames.get(@[]), workspaceRoot)
872+
await runTests(params.entryPoint, nimPath.get(), params.suiteName, params.testNames.get(@[]), workspaceRoot, ls)
873+
874+
proc cancelTest*(
875+
ls: LanguageServer, params: JsonNode
876+
): Future[CancelTestResult] {.async.} =
877+
debug "Cancelling test"
878+
if ls.testRunProcess.isSome: #No need to cancel the runTests request. The client should handle it.
879+
await shutdownChildProcess(ls.testRunProcess.get)
880+
ls.testRunProcess = none(AsyncProcessRef)
881+
CancelTestResult(cancelled: true)
882+
else:
883+
CancelTestResult(cancelled: false)
873884

874885
#Notifications
875886
proc initialized*(ls: LanguageServer, _: JsonNode): Future[void] {.async.} =

testrunner.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ proc runTests*(
9696
nimPath: string,
9797
suiteName: Option[string],
9898
testNames: seq[string],
99-
workspaceRoot: string
99+
workspaceRoot: string,
100+
ls: LanguageServer
100101
): Future[RunTestProjectResult] {.async.} =
101102
var entryPoint = getFullPath(entryPoint, workspaceRoot)
102103
if not fileExists(entryPoint):
@@ -117,6 +118,7 @@ proc runTests*(
117118
stderrHandle = AsyncProcess.Pipe,
118119
stdoutHandle = AsyncProcess.Pipe,
119120
)
121+
ls.testRunProcess = some(process)
120122
try:
121123
let res = await process.waitForExit(15.seconds)
122124
let processOutput = string.fromBytes(process.stdoutStream.read().await)
@@ -138,4 +140,5 @@ proc runTests*(
138140
error "Output from process", output = processOutput
139141
finally:
140142
await shutdownChildProcess(process)
141-
143+
if ls.testRunProcess.isSome:
144+
ls.testRunProcess = none(AsyncProcessRef)

tests/ttestrunner.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import std/[os, osproc, strscans, tables, sequtils, enumerate, strutils, options
33
import testhelpers
44
import testrunner
55
import chronos
6+
import ls
67

78
suite "Test Parser":
89
test "should be able to list tests from an entry point":
@@ -36,7 +37,8 @@ suite "Test Parser":
3637
suite "Test Runner":
3738
test "should be able to run tests and retrieve results":
3839
let entryPoint = getCurrentDir() / "tests" / "projects" / "testrunner" / "tests" / "sampletests.nim"
39-
let testProjectResult = waitFor runTests(entryPoint, "nim", none(string), @[], "")
40+
let ls = LanguageServer()
41+
let testProjectResult = waitFor runTests(entryPoint, "nim", none(string), @[], "", ls)
4042
check testProjectResult.suites.len == 4
4143
check testProjectResult.suites[0].name == "Sample Tests"
4244
check testProjectResult.suites[0].tests == 1

0 commit comments

Comments
 (0)