Skip to content

Commit 843dabe

Browse files
authored
Fix test runner (#337)
* Upgrades tests to `unittest2` * Better way to get the tests output. Fixes an issue where the output wasnt being retrieved
1 parent 506de1b commit 843dabe

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

testrunner.nim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,15 @@ proc listTests*(
8181
stdoutHandle = AsyncProcess.Pipe,
8282
)
8383
try:
84-
let (error, res) = await readErrorOutputUntilExit(process, 15.seconds)
84+
let (rawOutput, error, res) = await readOutputUntilExit(process, 15.seconds)
8585
if res != 0:
8686
error "Failed to list tests", nimPath = nimPath, entryPoint = entryPoint, res = res
8787
error "An error occurred while listing tests"
8888
for line in error.splitLines:
8989
error "Error line: ", line = line
9090
error "Command args: ", args = args
91-
result = TestProjectInfo(error: some error)
91+
result = TestProjectInfo(error: some error)
9292
else:
93-
let rawOutput = await readAllOutput(process.stdoutStream)
9493
debug "list test raw output", rawOutput = rawOutput
9594
result = extractTestInfo(rawOutput)
9695
finally:
@@ -126,7 +125,7 @@ proc runTests*(
126125
ls.testRunProcess = some(process)
127126
try:
128127
removeFile(resultFile)
129-
let (error, res) = await readErrorOutputUntilExit(process, 15.seconds)
128+
let (output, error, res) = await readOutputUntilExit(process, 15.seconds)
130129
if res != 0: #When a test fails, the process will exit with a non-zero code
131130
if fileExists(resultFile):
132131
result = parseTestResults(readFile(resultFile))
@@ -137,11 +136,12 @@ proc runTests*(
137136
error "An error occurred while running tests"
138137
error "Error from process", error = error
139138
result = RunTestProjectResult(fullOutput: error)
139+
result.fullOutput = output
140140
else:
141141
let xmlContent = readFile(resultFile)
142142
# echo "XML CONTENT: ", xmlContent
143143
result = parseTestResults(xmlContent)
144-
result.fullOutput = error
144+
result.fullOutput = output
145145
removeFile(resultFile)
146146
except Exception as e:
147147
let processOutput = string.fromBytes(process.stdoutStream.read().await)

utils.nim

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,45 @@ proc readErrorOutputUntilExit*(process: AsyncProcessRef, duration: Duration): Fu
386386
let data = await process.stderrStream.read()
387387
output.add(string.fromBytes(data))
388388
return (output, res)
389+
390+
391+
proc readOutputUntilExit*(process: AsyncProcessRef, duration: Duration): Future[tuple[output: string, error: string, code: int]] {.async.} =
392+
var output = ""
393+
var error = ""
394+
var res = 0
395+
# debug "Starting read output until exit"
396+
397+
while true:
398+
let hasExited = try:
399+
res = await process.waitForExit(duration)
400+
debug "Process exit check", hasExited = true, res = res
401+
true
402+
except AsyncTimeoutError:
403+
debug "Process still running"
404+
false
405+
406+
# Quick non-blocking reads
407+
try:
408+
if not process.stdoutStream.atEof:
409+
# debug "Attempting stdout read"
410+
let data = await process.stdoutStream.read() #
411+
if data.len > 0:
412+
# debug "Got stdout data", len = data.len
413+
output.add(string.fromBytes(data))
414+
except CatchableError as e:
415+
debug "Stdout read error", msg = e.msg
416+
417+
try:
418+
if not process.stderrStream.atEof:
419+
# debug "Attempting stderr read"
420+
let data = await process.stderrStream.read()
421+
if data.len > 0:
422+
# debug "Got stderr data", len = data.len
423+
error.add(string.fromBytes(data))
424+
except CatchableError as e:
425+
debug "Stderr read error", msg = e.msg
426+
427+
if hasExited:
428+
# debug "Process has exited, final cleanup", output = output, error = error, code = res
429+
return (output, error, res)
430+

0 commit comments

Comments
 (0)