Add ability to specify file pattern
This commit is contained in:
parent
ede3434b04
commit
c7f7ca5609
1 changed files with 22 additions and 3 deletions
25
src/tani.nim
25
src/tani.nim
|
|
@ -1,4 +1,4 @@
|
||||||
import macros
|
import macros, regex
|
||||||
import tables, strutils, os, typetraits, strformat
|
import tables, strutils, os, typetraits, strformat
|
||||||
|
|
||||||
export tables, strutils, os, typetraits
|
export tables, strutils, os, typetraits
|
||||||
|
|
@ -564,7 +564,7 @@ macro runTests*(site: varargs[untyped]): untyped =
|
||||||
currentFile = site.lineInfoObj.fileName.string
|
currentFile = site.lineInfoObj.fileName.string
|
||||||
return expandTests(currentDir, currentFile)
|
return expandTests(currentDir, currentFile)
|
||||||
|
|
||||||
macro runTestsMain(cd, cf: string): untyped =
|
macro runTestsMain(cd, cf: string): untyped {.used.} =
|
||||||
let
|
let
|
||||||
currentDir = cd.strVal
|
currentDir = cd.strVal
|
||||||
currentFile = cf.strVal
|
currentFile = cf.strVal
|
||||||
|
|
@ -579,6 +579,17 @@ macro runTestsMain(cd, cf: string): untyped =
|
||||||
|
|
||||||
return getAst(runAll(expTests))
|
return getAst(runAll(expTests))
|
||||||
|
|
||||||
|
# Exported so that it appears in docs
|
||||||
|
const testFilePattern*{.strdefine.}: string = "" ## \
|
||||||
|
## This is a regex pattern specified as a compiler flag that controls
|
||||||
|
## which files will be searched for tests. For example,
|
||||||
|
##
|
||||||
|
## ```
|
||||||
|
## nim c -d:testFilePattern="^test.*" tests/runner.nim
|
||||||
|
## ```
|
||||||
|
##
|
||||||
|
## Will only run tests from files that begin with "test"
|
||||||
|
|
||||||
macro discoverAndRunTests*(site: varargs[untyped]): untyped =
|
macro discoverAndRunTests*(site: varargs[untyped]): untyped =
|
||||||
## This macro must be run in a file that is in the directory containing all the tests
|
## This macro must be run in a file that is in the directory containing all the tests
|
||||||
##
|
##
|
||||||
|
|
@ -594,7 +605,15 @@ macro discoverAndRunTests*(site: varargs[untyped]): untyped =
|
||||||
let stmtList = newNimNode(nnkStmtList)
|
let stmtList = newNimNode(nnkStmtList)
|
||||||
|
|
||||||
for fname in os.walkDirRec(currentDir, relative=true):
|
for fname in os.walkDirRec(currentDir, relative=true):
|
||||||
if fname.splitFile().name.startsWith("test") and fname.splitFile().ext == ".nim":
|
var match: RegexMatch
|
||||||
|
let
|
||||||
|
pattern = re(testFilePattern)
|
||||||
|
validFile = testFilePattern.len == 0 or
|
||||||
|
regex.match(fname.splitFile().name, pattern, match)
|
||||||
|
|
||||||
|
if validFile and fname.splitFile().ext == ".nim":
|
||||||
|
|
||||||
|
# don't import the current file
|
||||||
if (currentDir / fname) != currentFile:
|
if (currentDir / fname) != currentFile:
|
||||||
let moduleName = newLit(currentDir / fname.replace(".nim", ""))
|
let moduleName = newLit(currentDir / fname.replace(".nim", ""))
|
||||||
let shortName = ident(fname.splitFile.name)
|
let shortName = ident(fname.splitFile.name)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue