From c7f7ca5609eebd8aab72c1439fea0d2126f55085 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Tue, 5 Nov 2019 15:54:31 -0700 Subject: [PATCH] Add ability to specify file pattern --- src/tani.nim | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/tani.nim b/src/tani.nim index 5cabe45..7bf9ea9 100644 --- a/src/tani.nim +++ b/src/tani.nim @@ -1,4 +1,4 @@ -import macros +import macros, regex import tables, strutils, os, typetraits, strformat export tables, strutils, os, typetraits @@ -564,7 +564,7 @@ macro runTests*(site: varargs[untyped]): untyped = currentFile = site.lineInfoObj.fileName.string return expandTests(currentDir, currentFile) -macro runTestsMain(cd, cf: string): untyped = +macro runTestsMain(cd, cf: string): untyped {.used.} = let currentDir = cd.strVal currentFile = cf.strVal @@ -579,6 +579,17 @@ macro runTestsMain(cd, cf: string): untyped = 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 = ## 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) 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: let moduleName = newLit(currentDir / fname.replace(".nim", "")) let shortName = ident(fname.splitFile.name)