-
Notifications
You must be signed in to change notification settings - Fork 15
Atlas native test runner #157
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
Open
elcritch
wants to merge
16
commits into
nim-lang:master
Choose a base branch
from
elcritch:atlas-native-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8283175
add test runner
elcritch e204a83
add test runner
elcritch 6294b24
add parallel test runner
elcritch e988cd7
add parallel test runner
elcritch b66923c
add parallel test runner
elcritch c3f464d
add parallel test runner
elcritch 1493c71
add parallel test runner
elcritch 36d9db5
add parallel test runner test
elcritch 97cb5d8
add parallel test runner test
elcritch feeaacf
add parallel test runner test
elcritch cb0a2f6
add parallel test runner test
elcritch 78bee7f
add parallel test runner test
elcritch e5e3127
add parallel test runner test
elcritch 62e1963
add parallel test runner test
elcritch 03c6693
Update doc/atlas.md
elcritch d251185
Merge branch 'master' into atlas-native-test
elcritch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import std/[osproc, os, strutils, sequtils, math, paths, algorithm] | ||
| import basic/reporters | ||
|
|
||
| ## Parallel test runner utilities using osproc.execProcesses. | ||
|
|
||
| proc buildTestCommand*(nimPath, testFile: string; extraArgs: seq[string] = @[]; runCode = true): string = | ||
| ## Compose a Nim compile (and optional run) command for a test file. | ||
| var cmd = quoteShell(nimPath) & " c -d:debug" | ||
| if extraArgs.len > 0: | ||
| for a in extraArgs: | ||
| cmd.add " " & quoteShell(a) | ||
| if runCode: | ||
| cmd.add " -r" | ||
| cmd.add " " & quoteShell(testFile) | ||
| result = cmd | ||
|
|
||
| proc runCommandsParallel*(commands: seq[string]; parallel: int = 0): int = | ||
| ## Run commands with limited parallelism; returns first non‑zero exit code or 0. | ||
| ## If `parallel <= 0`, uses `countProcessors()`; otherwise, runs in batches of `parallel`. | ||
| if commands.len == 0: | ||
| return 0 | ||
| if parallel <= 0: | ||
| return execProcesses(commands, n = countProcessors(), | ||
| beforeRunEvent = proc (idx: int) = discard, | ||
| afterRunEvent = proc (idx: int, p: Process) = discard | ||
| ) | ||
| var i = 0 | ||
| while i < commands.len: | ||
| let j = min(i + parallel, commands.len) | ||
| let code = execProcesses(commands[i ..< j], n = parallel, | ||
| beforeRunEvent = proc (idx: int) = discard, | ||
| afterRunEvent = proc (idx: int, p: Process) = discard | ||
| ) | ||
| if code != 0: | ||
| return code | ||
| i = j | ||
| return 0 | ||
|
|
||
| proc runTestsParallel*(tests: seq[string]; nimPath = findExe("nim"); extraArgs: seq[string] = @[]; runCode = true; parallel: int = countProcessors()): int = | ||
| ## Build and execute Nim test commands in parallel. | ||
| if nimPath.len == 0: | ||
| return -1 | ||
| var cmds: seq[string] = @[] | ||
| for tf in tests: | ||
| cmds.add buildTestCommand(nimPath, tf, extraArgs, runCode) | ||
| result = runCommandsParallel(cmds, parallel) | ||
|
|
||
| proc discoverTests*(projectDir: Path): seq[string] = | ||
| ## Find and return all tests matching tests/t*.nim inside projectDir. | ||
| let old = os.getCurrentDir() | ||
| defer: os.setCurrentDir(old) | ||
| os.setCurrentDir($projectDir) | ||
| for f in walkFiles("tests/t*.nim"): | ||
| result.add f | ||
| result.sort(system.cmp[string]) | ||
|
|
||
| proc runTestsSerial*(projectDir: Path; extraArgs: seq[string] = @[]; runCode = true; onlyTests: seq[string] = @[]): int = | ||
| ## Sequentially compile and (optionally) run each discovered test. | ||
| if projectDir.len == 0 or not dirExists($projectDir): | ||
| fatal "No project directory detected", "atlas:test" | ||
| return 1 | ||
| let nimPath = findExe("nim") | ||
| if nimPath.len == 0: | ||
| fatal "Nim compiler not found in PATH", "atlas:test" | ||
| return 1 | ||
| let old = os.getCurrentDir() | ||
| defer: os.setCurrentDir(old) | ||
| os.setCurrentDir($projectDir) | ||
| var tests = | ||
| if onlyTests.len > 0: onlyTests | ||
| else: discoverTests(projectDir) | ||
| if tests.len == 0: | ||
| warn "atlas:test", "No tests found matching 'tests/t*.nim'" | ||
| return 0 | ||
| for tf in tests: | ||
| info "atlas:test", "running:", tf | ||
| let cmd = buildTestCommand(nimPath, tf, extraArgs, runCode) | ||
| let code = execShellCmd(cmd) | ||
| if code != 0: | ||
| fatal "Test failed: " & tf, "atlas:test", code | ||
| return code | ||
| notice "atlas:test", "All tests passed" | ||
| return 0 | ||
|
|
||
| proc runTests*(projectDir: Path; extraArgs: seq[string] = @[]; runCode = true; parallel: int; onlyTests: seq[string] = @[]): int = | ||
| ## Run tests either serially or in parallel (parallel<=0 uses CPU count). | ||
| if projectDir.len == 0 or not dirExists($projectDir): | ||
| fatal "No project directory detected", "atlas:test" | ||
| return 1 | ||
| let nimPath = findExe("nim") | ||
| if nimPath.len == 0: | ||
| fatal "Nim compiler not found in PATH", "atlas:test" | ||
| return 1 | ||
| let tests = | ||
| if onlyTests.len > 0: onlyTests | ||
| else: discoverTests(projectDir) | ||
| if tests.len == 0: | ||
| warn "atlas:test", "No tests found matching 'tests/t*.nim'" | ||
| return 0 | ||
| if parallel > 1: | ||
| let code = runTestsParallel(tests, nimPath, extraArgs, runCode, parallel) | ||
| if code != 0: | ||
| fatal "A test failed", "atlas:test", code | ||
| else: | ||
| notice "atlas:test", "All tests passed" | ||
| return code | ||
| else: | ||
| return runTestsSerial(projectDir, extraArgs, runCode, tests) | ||
|
|
||
| when isMainModule: | ||
| # Example usage: run all tests matching tests/t*.nim in parallel. | ||
| var tests: seq[string] = @[] | ||
| for f in walkFiles("tests/t*.nim"): | ||
| tests.add(f) | ||
| let code = runTestsParallel(tests) | ||
| quit(code) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import std/[os, strutils, unittest] | ||
| import testerutils | ||
|
|
||
| suite "atlas test runner": | ||
| # Ensure atlas binary exists | ||
| if execShellCmd("nim c --nimcache:.nimcache -o:$# -d:release src/atlas.nim" % [atlasExe]) != 0: | ||
| quit "Failed to build atlas binary" | ||
|
|
||
| let ws = "tests/ws_runtests" | ||
| if not dirExists(ws): createDir(ws) | ||
|
|
||
| withDir ws: | ||
| # Clean previous markers and prepare tests dir | ||
| if fileExists("ran_a.txt"): removeFile("ran_a.txt") | ||
| if fileExists("ran_b.txt"): removeFile("ran_b.txt") | ||
| if not dirExists("tests"): createDir("tests") | ||
|
|
||
| test "runs all tests by default": | ||
| exec atlasExe & " --project:. test" | ||
| check fileExists("ran_a.txt") | ||
| check fileExists("ran_b.txt") | ||
|
|
||
| # Reset markers | ||
| if fileExists("ran_a.txt"): removeFile("ran_a.txt") | ||
| if fileExists("ran_b.txt"): removeFile("ran_b.txt") | ||
|
|
||
| test "runs a single specified test": | ||
| exec atlasExe & " --project:. test tests/ta.nim" | ||
| check fileExists("ran_a.txt") | ||
| check not fileExists("ran_b.txt") | ||
|
|
||
| # Reset markers | ||
| if fileExists("ran_a.txt"): removeFile("ran_a.txt") | ||
| if fileExists("ran_b.txt"): removeFile("ran_b.txt") | ||
|
|
||
| test "runs multiple specified tests": | ||
| exec atlasExe & " --project:. test tests/ta.nim tests/tb.nim" | ||
| check fileExists("ran_a.txt") | ||
| check fileExists("ran_b.txt") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ok |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ok |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import std/[unittest, os] | ||
| suite "A": | ||
| test "a": | ||
| writeFile("ran_a.txt", "ok") | ||
| check true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import std/[unittest, os] | ||
| suite "B": | ||
| test "b": | ||
| writeFile("ran_b.txt", "ok") | ||
| check true |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.