2025-01-24 17:42:29 -06:00
|
|
|
import std/[compilesettings, os, osproc, strutils, times, unittest, terminal]
|
2024-11-18 08:49:54 -06:00
|
|
|
|
|
|
|
const pathToSrc = querySetting(SingleValueSetting.projectPath)
|
|
|
|
const binDir = pathToSrc / "bin"
|
|
|
|
const hwylCliSrc = pathToSrc / "../../src/hwylterm/hwylcli.nim"
|
|
|
|
let hwylCliWriteTime = getFileInfo(hwylCliSrc).lastWriteTime
|
|
|
|
|
|
|
|
if not dirExists(binDir):
|
|
|
|
createDir(binDir)
|
|
|
|
|
2025-01-13 17:13:22 -06:00
|
|
|
proc runTestCli(module: string, args: string, code: int = 0): (string, int) =
|
2024-11-18 08:49:54 -06:00
|
|
|
let cmd = binDir / module & " " & args
|
2025-01-13 17:13:22 -06:00
|
|
|
let (output, code) = execCmdEx(cmd)
|
|
|
|
result = (output.strip(), code)
|
2024-11-18 08:49:54 -06:00
|
|
|
|
2025-01-24 17:42:29 -06:00
|
|
|
# poor man's progress meter
|
|
|
|
proc status(s: string) =
|
|
|
|
eraseLine stdout
|
|
|
|
stdout.write(s.alignLeft(terminalWidth()).substr(0, terminalWidth()-1))
|
|
|
|
flushFile stdout
|
|
|
|
|
2024-11-18 08:49:54 -06:00
|
|
|
proc preCompileWorkingModule(module: string) =
|
|
|
|
let exe = binDir / module
|
|
|
|
let srcModule = pathToSrc / "clis" / (module & ".nim")
|
2025-01-24 17:42:29 -06:00
|
|
|
if not exe.fileExists or getFileInfo(exe).lastWriteTime < max(getFileInfo(srcModule).lastWriteTime, hwylCliWriteTime) or defined(forceSetup):
|
2024-11-18 08:49:54 -06:00
|
|
|
let cmd = "nim c -o:$1 $2" % [exe, srcModule]
|
2025-01-24 17:42:29 -06:00
|
|
|
let (output, code) = execCmdEx(cmd)
|
2024-11-18 08:49:54 -06:00
|
|
|
if code != 0:
|
|
|
|
echo "cmd: ", cmd
|
2025-01-24 17:42:29 -06:00
|
|
|
quit "failed to precompile test module:\n" & output
|
2024-11-18 08:49:54 -06:00
|
|
|
|
2025-01-23 19:25:09 -06:00
|
|
|
proc preCompileTestModules*() =
|
2025-01-24 17:42:29 -06:00
|
|
|
var modules: seq[string]
|
2025-01-23 19:25:09 -06:00
|
|
|
for srcModule in walkDirRec(pathToSrc / "clis"):
|
|
|
|
if srcModule.endsWith(".nim"):
|
2025-01-24 17:42:29 -06:00
|
|
|
modules.add srcModule.splitFile().name
|
|
|
|
|
|
|
|
for i, module in modules:
|
|
|
|
status "compiling [$2/$3] $1" % [ module, $(i+1), $modules.len]
|
|
|
|
preCompileWorkingModule(module)
|
|
|
|
|
|
|
|
eraseLine stdout
|
|
|
|
|
2025-01-13 17:13:22 -06:00
|
|
|
template okWithArgs*(module: string, args = "", output = "") =
|
2024-11-18 08:49:54 -06:00
|
|
|
preCompileWorkingModule(module)
|
2025-01-29 15:35:05 -06:00
|
|
|
let normalizedOutput = output.strip().strip(leading = false, chars = {'\n'}).dedent()
|
2025-01-23 19:25:09 -06:00
|
|
|
test (module & "|" & args):
|
2025-01-13 17:13:22 -06:00
|
|
|
let (actualOutput, code) = runTestCli(module, args)
|
|
|
|
check code == 0
|
2025-01-29 15:35:05 -06:00
|
|
|
check normalizedOutput == actualOutput
|
2025-01-13 17:13:22 -06:00
|
|
|
|
|
|
|
template failWithArgs*(module: string, args = "", output = "") =
|
|
|
|
preCompileWorkingModule(module)
|
2025-01-29 15:35:05 -06:00
|
|
|
let normalizedOutput = output.strip().strip(leading = false, chars = {'\n'}).dedent()
|
2025-01-23 19:25:09 -06:00
|
|
|
test (module & "|" & args):
|
2025-01-13 17:13:22 -06:00
|
|
|
let (actualOutput, code) = runTestCli(module, args)
|
|
|
|
check code == 1
|
2025-01-29 15:35:05 -06:00
|
|
|
check normalizedOutput == actualOutput
|