Skip to content

Captures output. Refactors entrypoints #328

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 17, 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
8 changes: 5 additions & 3 deletions protocol/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1055,12 +1055,12 @@ type
tests*: seq[TestInfo]

TestProjectInfo* = object
entryPoints*: seq[string]
entryPoint*: string
suites*: Table[string, TestSuiteInfo]
error*: Option[string]

ListTestsParams* = object
entryPoints*: seq[string] #can be patterns? if empty we could do the same as nimble does or just run `nimble test args`
entryPoint*: string #can be patterns? if empty we could do the same as nimble does or just run `nimble test args`

ListTestsResult* = object
projectInfo*: TestProjectInfo
Expand All @@ -1080,9 +1080,11 @@ type
testResults*: seq[RunTestResult]

RunTestParams* = object
entryPoints*: seq[string]
entryPoint*: string
suiteName*: Option[string] #Optional, if provided, only run tests in the suite. Takes precedence over testName
testNames*: Option[seq[string]] #Optional, if provided, only run the specific tests

RunTestProjectResult* = object
suites*: seq[RunTestSuiteResult]
fullOutput*: string

6 changes: 3 additions & 3 deletions routes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -855,9 +855,9 @@ proc listTests*(
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]()))
return ListTestsResult(projectInfo: TestProjectInfo(entryPoint: params.entryPoint, suites: initTable[string, TestSuiteInfo]()))
let workspaceRoot = ls.initializeParams.getRootPath
let testProjectInfo = await listTests(params.entryPoints, nimPath.get(), workspaceRoot)
let testProjectInfo = await listTests(params.entryPoint, nimPath.get(), workspaceRoot)
result.projectInfo = testProjectInfo

proc runTests*(
Expand All @@ -869,7 +869,7 @@ proc runTests*(
error "Nim path not found when running tests"
return RunTestProjectResult()
let workspaceRoot = ls.initializeParams.getRootPath
await runTests(params.entryPoints, nimPath.get(), params.suiteName, params.testNames.get(@[]), workspaceRoot)
await runTests(params.entryPoint, nimPath.get(), params.suiteName, params.testNames.get(@[]), workspaceRoot)

#Notifications
proc initialized*(ls: LanguageServer, _: JsonNode): Future[void] {.async.} =
Expand Down
15 changes: 7 additions & 8 deletions testrunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ proc extractTestInfo*(rawOutput: string): TestProjectInfo =
result.suites = initTable[string, TestSuiteInfo]()
let lines = rawOutput.split("\n")
var currentSuite = ""

for i, line in enumerate(lines):
var name, file, ignore: string
var lineNumber: int
Expand Down Expand Up @@ -38,12 +37,11 @@ proc getFullPath*(entryPoint: string, workspaceRoot: string): string =
return entryPoint

proc listTests*(
entryPoints: seq[string],
entryPoint: string,
nimPath: string,
workspaceRoot: string
): Future[TestProjectInfo] {.async.} =
assert entryPoints.len == 1
var entryPoint = getFullPath(entryPoints[0], workspaceRoot)
var entryPoint = getFullPath(entryPoint, workspaceRoot)
let process = await startProcess(
nimPath,
arguments = @["c", "-d:unittest2ListTests", "-r", "--listFullPaths", entryPoint],
Expand Down Expand Up @@ -94,14 +92,13 @@ proc parseTestResults*(xmlContent: string): RunTestProjectResult =
result.suites.add(suite)

proc runTests*(
entryPoints: seq[string],
entryPoint: string,
nimPath: string,
suiteName: Option[string],
testNames: seq[string],
workspaceRoot: string
): Future[RunTestProjectResult] {.async.} =
assert entryPoints.len == 1
var entryPoint = getFullPath(entryPoints[0], workspaceRoot)
var entryPoint = getFullPath(entryPoint, workspaceRoot)
if not fileExists(entryPoint):
error "Entry point does not exist", entryPoint = entryPoint
return RunTestProjectResult()
Expand All @@ -122,8 +119,9 @@ proc runTests*(
)
try:
let res = await process.waitForExit(15.seconds)
let processOutput = string.fromBytes(process.stdoutStream.read().await)

if not fileExists(resultFile):
let processOutput = string.fromBytes(process.stdoutStream.read().await)
let processError = string.fromBytes(process.stderrStream.read().await)
error "Result file does not exist meaning tests were not run"
error "Output from process", output = processOutput
Expand All @@ -132,6 +130,7 @@ proc runTests*(
let xmlContent = readFile(resultFile)
# echo "XML CONTENT: ", xmlContent
result = parseTestResults(xmlContent)
result.fullOutput = processOutput
removeFile(resultFile)
except Exception as e:
let processOutput = string.fromBytes(process.stdoutStream.read().await)
Expand Down
12 changes: 6 additions & 6 deletions tests/textensions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ suite "Nimlangserver extensions":
}
let initializeResult = waitFor client.initialize(initParams)

let listTestsParams = ListTestsParams(entryPoints: @["tests/projects/testrunner/tests/sampletests.nim".absolutePath])
let listTestsParams = ListTestsParams(entryPoint: "tests/projects/testrunner/tests/sampletests.nim".absolutePath)
let tests = client.call("extension/listTests", jsonutils.toJson(listTestsParams)).waitFor().jsonTo(
ListTestsResult
)
Expand All @@ -109,7 +109,7 @@ suite "Nimlangserver extensions":
}
let initializeResult = waitFor client.initialize(initParams)

let runTestsParams = RunTestParams(entryPoints: @["tests/projects/testrunner/tests/sampletests.nim".absolutePath])
let runTestsParams = RunTestParams(entryPoint: "tests/projects/testrunner/tests/sampletests.nim".absolutePath)
let runTestsRes = client.call("extension/runTests", jsonutils.toJson(runTestsParams)).waitFor().jsonTo(
RunTestProjectResult
)
Expand All @@ -132,7 +132,7 @@ suite "Nimlangserver extensions":
let initializeResult = waitFor client.initialize(initParams)

let suiteName = "Sample Suite"
let runTestsParams = RunTestParams(entryPoints: @["tests/projects/testrunner/tests/sampletests.nim".absolutePath], suiteName: suiteName)
let runTestsParams = RunTestParams(entryPoint: "tests/projects/testrunner/tests/sampletests.nim".absolutePath, suiteName: some suiteName)
let runTestsRes = client.call("extension/runTests", jsonutils.toJson(runTestsParams)).waitFor().jsonTo(
RunTestProjectResult
)
Expand All @@ -152,7 +152,7 @@ suite "Nimlangserver extensions":
let initializeResult = waitFor client.initialize(initParams)

let testName = "Sample Test"
let runTestsParams = RunTestParams(entryPoints: @["tests/projects/testrunner/tests/sampletests.nim".absolutePath], testNames: @[testName])
let runTestsParams = RunTestParams(entryPoint: "tests/projects/testrunner/tests/sampletests.nim".absolutePath, testNames: some @[testName])
let runTestsRes = client.call("extension/runTests", jsonutils.toJson(runTestsParams)).waitFor().jsonTo(RunTestProjectResult)

check runTestsRes.suites.len == 1
Expand All @@ -170,7 +170,7 @@ suite "Nimlangserver extensions":
let initializeResult = waitFor client.initialize(initParams)

let testNames = @["Sample Test", "Sample Test 2"]
let runTestsParams = RunTestParams(entryPoints: @["tests/projects/testrunner/tests/sampletests.nim".absolutePath], testNames: testNames)
let runTestsParams = RunTestParams(entryPoint: "tests/projects/testrunner/tests/sampletests.nim".absolutePath, testNames: some testNames)
let runTestsRes = client.call("extension/runTests", jsonutils.toJson(runTestsParams)).waitFor().jsonTo(RunTestProjectResult)

check runTestsRes.suites.len == 1
Expand All @@ -187,7 +187,7 @@ suite "Nimlangserver extensions":

let initializeResult = waitFor client.initialize(initParams)

let runTestsParams = RunTestParams(entryPoints: @["tests/projects/testrunner/tests/failingtest.nim".absolutePath])
let runTestsParams = RunTestParams(entryPoint: "tests/projects/testrunner/tests/failingtest.nim".absolutePath)
let runTestsRes = client.call("extension/runTests", jsonutils.toJson(runTestsParams)).waitFor().jsonTo(RunTestProjectResult)

check runTestsRes.suites.len == 1
Expand Down
2 changes: 1 addition & 1 deletion tests/ttestrunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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 testProjectResult = waitFor runTests(entryPoint, "nim", none(string), @[], "")
check testProjectResult.suites.len == 4
check testProjectResult.suites[0].name == "Sample Tests"
check testProjectResult.suites[0].tests == 1
Expand Down