From 0b8348ea5148b2bd28b286b01b9900970c20ca92 Mon Sep 17 00:00:00 2001 From: jmgomez Date: Thu, 17 Apr 2025 11:50:08 +0100 Subject: [PATCH] Captures output. Refactors entrypoints --- protocol/types.nim | 8 +++++--- routes.nim | 6 +++--- testrunner.nim | 15 +++++++-------- tests/textensions.nim | 12 ++++++------ tests/ttestrunner.nim | 2 +- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/protocol/types.nim b/protocol/types.nim index 99a877b..4eb98a4 100644 --- a/protocol/types.nim +++ b/protocol/types.nim @@ -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 @@ -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 + diff --git a/routes.nim b/routes.nim index 16573ad..fe65428 100644 --- a/routes.nim +++ b/routes.nim @@ -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*( @@ -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.} = diff --git a/testrunner.nim b/testrunner.nim index 6ff929d..b75fd25 100644 --- a/testrunner.nim +++ b/testrunner.nim @@ -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 @@ -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], @@ -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() @@ -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 @@ -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) diff --git a/tests/textensions.nim b/tests/textensions.nim index 762a56d..1d9df6a 100644 --- a/tests/textensions.nim +++ b/tests/textensions.nim @@ -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 ) @@ -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 ) @@ -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 ) @@ -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 @@ -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 @@ -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 diff --git a/tests/ttestrunner.nim b/tests/ttestrunner.nim index b38604d..b5113b0 100644 --- a/tests/ttestrunner.nim +++ b/tests/ttestrunner.nim @@ -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