Skip to content

Commit 2b5841c

Browse files
authored
fix testament regression: installed testament works again with testament r path (#16767)
* fix testament regression: installed testament works again with testament r path * fixup
1 parent 00d9176 commit 2b5841c

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

testament/lib/stdtest/specialpaths.nim

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,30 @@ const
2424
# robust way to derive other paths here
2525
# We don't depend on PATH so this is robust to having multiple nim binaries
2626
nimRootDir* = sourcePath.parentDir.parentDir.parentDir.parentDir ## root of Nim repo
27+
testsFname* = "tests"
2728
stdlibDir* = nimRootDir / "lib"
2829
systemPath* = stdlibDir / "system.nim"
29-
testsDir* = nimRootDir / "tests"
30+
testsDir* = nimRootDir / testsFname
3031
buildDir* = nimRootDir / "build"
3132
## refs #10268: all testament generated files should go here to avoid
3233
## polluting .gitignore
3334

35+
proc splitTestFile*(file: string): tuple[cat: string, path: string] =
36+
## At least one directory is required in the path, to use as a category name
37+
runnableExamples:
38+
doAssert splitTestFile("tests/fakedir/tfakename.nim") == ("fakedir", "tests/fakedir/tfakename.nim".unixToNativePath)
39+
for p in file.parentDirs(inclusive = false):
40+
let parent = p.parentDir
41+
if parent.lastPathPart == testsFname:
42+
result.cat = p.lastPathPart
43+
let dir = getCurrentDir()
44+
if file.isRelativeTo(dir):
45+
result.path = file.relativePath(dir)
46+
else:
47+
result.path = file
48+
return result
49+
doAssert false, "file must match this pattern: '/pathto/tests/dir/**/tfile.nim', got: '" & file & "'"
50+
3451
static:
3552
# sanity check
3653
doAssert fileExists(systemPath)

testament/testament.nim

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import
1616
from std/sugar import dup
1717
import compiler/nodejs
1818
import lib/stdtest/testutils
19+
from lib/stdtest/specialpaths import splitTestFile
1920

2021
var useColors = true
2122
var backendLogging = true
@@ -791,16 +792,9 @@ proc main() =
791792
p.next
792793
processPattern(r, pattern, p.cmdLineRest.string, simulate)
793794
of "r", "run":
794-
# "/pathto/tests/stdlib/nre/captures.nim" -> "stdlib" + "tests/stdlib/nre/captures.nim"
795-
var subPath = p.key.string
796-
let nimRoot = currentSourcePath / "../.."
797-
# makes sure points to this regardless of cwd or which nim is used to compile this.
798-
doAssert(dirExists(nimRoot/testsDir), nimRoot/testsDir & " doesn't exist!") # sanity check
799-
if subPath.isAbsolute: subPath = subPath.relativePath(nimRoot)
800-
# at least one directory is required in the path, to use as a category name
801-
let pathParts = subPath.relativePath(testsDir).split({DirSep, AltSep})
802-
let cat = Category(pathParts[0])
803-
processSingleTest(r, cat, p.cmdLineRest.string, subPath, gTargets, targetsSet)
795+
var subPath = p.key
796+
let (cat, path) = splitTestFile(subPath)
797+
processSingleTest(r, cat.Category, p.cmdLineRest, path, gTargets, targetsSet)
804798
of "html":
805799
generateHtml(resultsFile, optFailing)
806800
else:

tests/testament/tspecialpaths.nim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import stdtest/specialpaths
2+
import std/os
3+
block: # splitTestFile
4+
doAssert splitTestFile("tests/fakedir/tfakename.nim") == ("fakedir", "tests/fakedir/tfakename.nim".unixToNativePath)
5+
doAssert splitTestFile("/pathto/tests/fakedir/tfakename.nim") == ("fakedir", "/pathto/tests/fakedir/tfakename.nim".unixToNativePath)
6+
doAssert splitTestFile(getCurrentDir() / "tests/fakedir/tfakename.nim") == ("fakedir", "tests/fakedir/tfakename.nim".unixToNativePath)
7+
doAssert splitTestFile(getCurrentDir() / "sub/tests/fakedir/tfakename.nim") == ("fakedir", "sub/tests/fakedir/tfakename.nim".unixToNativePath)
8+
doAssertRaises(AssertionDefect): discard splitTestFile("testsbad/fakedir/tfakename.nim")
9+
doAssertRaises(AssertionDefect): discard splitTestFile("tests/tfakename.nim")

0 commit comments

Comments
 (0)