Skip to content

cancelTest entry point implemented #329

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 1 commit into from
Apr 18, 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
2 changes: 2 additions & 0 deletions ls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ type
nimDumpCache*: Table[string, NimbleDumpInfo] #path to NimbleDumpInfo
entryPoints*: seq[string]
responseMap*: TableRef[string, Future[JsonNode]]
testRunProcess*: Option[AsyncProcessRef] #There is only one test run process at a time

#id to future. Represents the pending requests as result of calling ls.call
srv*: RpcSocketServer
#Both modes uses it to store the routes. Only actually started in socket mode
Expand Down
1 change: 1 addition & 0 deletions nimlangserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ proc registerRoutes(srv: RpcSocketServer, ls: LanguageServer) =
srv.register("extension/runTask", wrapRpc(partial(runTask, ls)))
srv.register("extension/listTests", wrapRpc(partial(listTests, ls)))
srv.register("extension/runTests", wrapRpc(partial(runTests, ls)))
srv.register("extension/cancelTest", wrapRpc(partial(cancelTest, ls)))
#Notifications
srv.register("$/cancelRequest", wrapRpc(partial(cancelRequest, ls)))
srv.register("initialized", wrapRpc(partial(initialized, ls)))
Expand Down
2 changes: 2 additions & 0 deletions protocol/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1088,3 +1088,5 @@ type
suites*: seq[RunTestSuiteResult]
fullOutput*: string

CancelTestResult* = object
cancelled*: bool
13 changes: 12 additions & 1 deletion routes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,18 @@ proc runTests*(
error "Nim path not found when running tests"
return RunTestProjectResult()
let workspaceRoot = ls.initializeParams.getRootPath
await runTests(params.entryPoint, nimPath.get(), params.suiteName, params.testNames.get(@[]), workspaceRoot)
await runTests(params.entryPoint, nimPath.get(), params.suiteName, params.testNames.get(@[]), workspaceRoot, ls)

proc cancelTest*(
ls: LanguageServer, params: JsonNode
): Future[CancelTestResult] {.async.} =
debug "Cancelling test"
if ls.testRunProcess.isSome: #No need to cancel the runTests request. The client should handle it.
await shutdownChildProcess(ls.testRunProcess.get)
ls.testRunProcess = none(AsyncProcessRef)
CancelTestResult(cancelled: true)
else:
CancelTestResult(cancelled: false)

#Notifications
proc initialized*(ls: LanguageServer, _: JsonNode): Future[void] {.async.} =
Expand Down
7 changes: 5 additions & 2 deletions testrunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ proc runTests*(
nimPath: string,
suiteName: Option[string],
testNames: seq[string],
workspaceRoot: string
workspaceRoot: string,
ls: LanguageServer
): Future[RunTestProjectResult] {.async.} =
var entryPoint = getFullPath(entryPoint, workspaceRoot)
if not fileExists(entryPoint):
Expand All @@ -117,6 +118,7 @@ proc runTests*(
stderrHandle = AsyncProcess.Pipe,
stdoutHandle = AsyncProcess.Pipe,
)
ls.testRunProcess = some(process)
try:
let res = await process.waitForExit(15.seconds)
let processOutput = string.fromBytes(process.stdoutStream.read().await)
Expand All @@ -138,4 +140,5 @@ proc runTests*(
error "Output from process", output = processOutput
finally:
await shutdownChildProcess(process)

if ls.testRunProcess.isSome:
ls.testRunProcess = none(AsyncProcessRef)
4 changes: 3 additions & 1 deletion tests/ttestrunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import std/[os, osproc, strscans, tables, sequtils, enumerate, strutils, options
import testhelpers
import testrunner
import chronos
import ls

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