From 254658ee5d95514bc7a087fa7fa7c79f621e9b34 Mon Sep 17 00:00:00 2001 From: Nycto Date: Tue, 18 Jul 2017 09:23:47 -0700 Subject: [PATCH] Add test target --- src/nimble.nim | 24 ++++++++++++++++++++++++ src/nimblepkg/options.nim | 13 ++++++++----- tests/nimscript/nimscript.nimble | 4 ++-- tests/tester.nim | 24 ++++++++++++++++++++++-- tests/testsFail/testing123.nim | 4 ++++ tests/testsFail/testing123.nimble | 4 ++++ tests/testsFail/tests/a.nim | 6 ++++++ tests/testsFail/tests/b.nim | 6 ++++++ tests/testsFail/tests/c.nim | 7 +++++++ tests/testsPass/testing123.nim | 4 ++++ tests/testsPass/testing123.nimble | 4 ++++ tests/testsPass/tests/one.nim | 6 ++++++ tests/testsPass/tests/three.nim | 7 +++++++ tests/testsPass/tests/two.nim | 7 +++++++ 14 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 tests/testsFail/testing123.nim create mode 100644 tests/testsFail/testing123.nimble create mode 100644 tests/testsFail/tests/a.nim create mode 100644 tests/testsFail/tests/b.nim create mode 100644 tests/testsFail/tests/c.nim create mode 100644 tests/testsPass/testing123.nim create mode 100644 tests/testsPass/testing123.nimble create mode 100644 tests/testsPass/tests/one.nim create mode 100644 tests/testsPass/tests/three.nim create mode 100644 tests/testsPass/tests/two.nim diff --git a/src/nimble.nim b/src/nimble.nim index 6770694..42cce57 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -673,6 +673,28 @@ proc execBackend(options: Options) = [backend, args, bin], showOutput = true) display("Success:", "Execution finished", Success, HighPriority) +proc tempOutArg(file: string): string = + ## Returns the `--out` argument to pass to the compiler, using a temp file + let (_, name, _) = splitFile(file) + let dir = getNimbleTempDir() / "tests" + createDir(dir) + result = "--out:" & (dir / name) + +proc test(options: Options) = + ## Executes all tests + var files = toSeq(walkDir(getCurrentDir() / "tests")) + files.sort do (a, b: auto) -> int: + result = cmp(a.path, b.path) + + for file in files: + if file.path.endsWith(".nim") and file.kind in { pcFile, pcLinkToFile }: + var optsCopy = options + optsCopy.action.file = file.path + optsCopy.action.backend = "c -r" + optsCopy.action.compileOptions.add("--path:.") + optsCopy.action.compileOptions.add(file.path.tempOutArg) + execBackend(optsCopy) + proc search(options: Options) = ## Searches for matches in ``options.action.search``. ## @@ -1030,6 +1052,8 @@ proc doAction(options: Options) = listPaths(options) of actionBuild: build(options) + of actionTest: + test(options) of actionCompile, actionDoc: execBackend(options) of actionInit: diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index d68e524..15d510f 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -25,7 +25,7 @@ type ActionType* = enum actionNil, actionRefresh, actionInit, actionDump, actionPublish, actionInstall, actionSearch, - actionList, actionBuild, actionPath, actionUninstall, actionCompile, + actionList, actionBuild, actionTest, actionPath, actionUninstall, actionCompile, actionDoc, actionCustom, actionTasks Action* = object @@ -39,7 +39,7 @@ type search*: seq[string] # Search string. of actionInit, actionDump: projName*: string - of actionCompile, actionDoc, actionBuild: + of actionCompile, actionDoc, actionBuild, actionTest: file*: string backend*: string compileOptions*: seq[string] @@ -63,6 +63,7 @@ Commands: build Builds a package. c, cc, js [opts, ...] f.nim Builds a file inside a package. Passes options to the Nim compiler. + test Compiles and executes tests doc, doc2 [opts, ...] f.nim Builds documentation for a file inside a package. Passes options to the Nim compiler. refresh [url] Refreshes the package list. A package list URL @@ -117,6 +118,8 @@ proc parseActionType*(action: string): ActionType = result = actionPath of "build": result = actionBuild + of "test": + result = actionTest of "c", "compile", "js", "cpp", "cc": result = actionCompile of "doc", "doc2": @@ -147,7 +150,7 @@ proc initAction*(options: var Options, key: string) = case options.action.typ of actionInstall, actionPath: options.action.packages = @[] - of actionCompile, actionDoc, actionBuild: + of actionCompile, actionDoc, actionBuild, actionTest: options.action.compileOptions = @[] options.action.file = "" if keyNorm == "c" or keyNorm == "compile": options.action.backend = "" @@ -231,7 +234,7 @@ proc parseArgument*(key: string, result: var Options) = result.action.projName = key of actionCompile, actionDoc: result.action.file = key - of actionList, actionBuild, actionPublish: + of actionList, actionBuild, actionTest, actionPublish: result.showHelp = true of actionCustom: result.action.arguments.add(key) @@ -269,7 +272,7 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) = result.depsOnly = true else: wasFlagHandled = false - of actionCompile, actionDoc, actionBuild: + of actionCompile, actionDoc, actionBuild, actionTest: let prefix = if kind == cmdShortOption: "-" else: "--" if val == "": result.action.compileOptions.add(prefix & flag) diff --git a/tests/nimscript/nimscript.nimble b/tests/nimscript/nimscript.nimble index ff8c5c6..d1adfba 100644 --- a/tests/nimscript/nimscript.nimble +++ b/tests/nimscript/nimscript.nimble @@ -11,7 +11,7 @@ bin = @["nimscript"] requires "nim >= 0.12.1" -task test, "test description": +task work, "test description": echo(5+5) task c_test, "Testing `setCommand \"c\", \"nimscript.nim\"`": @@ -42,4 +42,4 @@ before hooks2: return false task hooks2, "Testing the hooks again": - echo("Shouldn't happen") \ No newline at end of file + echo("Shouldn't happen") diff --git a/tests/tester.nim b/tests/tester.nim index 3bad7d0..ff64918 100644 --- a/tests/tester.nim +++ b/tests/tester.nim @@ -33,6 +33,7 @@ proc execNimble(args: varargs[string]): tuple[output: string, exitCode: int] = quotedArgs = quoted_args.map((x: string) => ("\"" & x & "\"")) result = execCmdEx(quotedArgs.join(" ")) + checkpoint(result.output) proc processOutput(output: string): seq[string] = output.strip.splitLines().filter((x: string) => (x.len > 0)) @@ -220,7 +221,7 @@ test "can install nimscript package": test "can execute nimscript tasks": cd "nimscript": - let (output, exitCode) = execNimble("--verbose", "test") + let (output, exitCode) = execNimble("--verbose", "work") let lines = output.strip.splitLines() check exitCode == QuitSuccess check lines[^1] == "10" @@ -253,7 +254,7 @@ test "can use nimscript with repeated flags (issue #329)": test "can list nimscript tasks": cd "nimscript": let (output, exitCode) = execNimble("tasks") - check "test test description".normalize in output.normalize + check "work test description".normalize in output.normalize check exitCode == QuitSuccess test "can use pre/post hooks": @@ -400,3 +401,22 @@ test "can dump for installed package": let (outp, exitCode) = execNimble("dump", "testdump") check: exitCode == 0 check: outp.processOutput.inLines("desc: \"Test package for dump command\"") + +test "Runs passing unit tests": + cd "testsPass": + let (outp, exitCode) = execNimble("test") + check: exitCode == QuitSuccess + check: outp.processOutput.inLines("First test") + check: outp.processOutput.inLines("Second test") + check: outp.processOutput.inLines("Third test") + check: outp.processOutput.inLines("Executing my func") + +test "Runs failing unit tests": + cd "testsFail": + let (outp, exitCode) = execNimble("test") + check: exitCode == QuitFailure + check: outp.processOutput.inLines("First test") + check: outp.processOutput.inLines("Failing Second test") + check: not outp.processOutput.inLines("Third test") + + diff --git a/tests/testsFail/testing123.nim b/tests/testsFail/testing123.nim new file mode 100644 index 0000000..2c96a26 --- /dev/null +++ b/tests/testsFail/testing123.nim @@ -0,0 +1,4 @@ + +proc myFunc*() = + echo "Executing my func" + diff --git a/tests/testsFail/testing123.nimble b/tests/testsFail/testing123.nimble new file mode 100644 index 0000000..00e43bf --- /dev/null +++ b/tests/testsFail/testing123.nimble @@ -0,0 +1,4 @@ +version = "0.1.0" +author = "John Doe" +description = "Nimble Test" +license = "BSD" diff --git a/tests/testsFail/tests/a.nim b/tests/testsFail/tests/a.nim new file mode 100644 index 0000000..09f3215 --- /dev/null +++ b/tests/testsFail/tests/a.nim @@ -0,0 +1,6 @@ +import testing123, unittest + +test "can compile nimble": + echo "First test" + myFunc() + diff --git a/tests/testsFail/tests/b.nim b/tests/testsFail/tests/b.nim new file mode 100644 index 0000000..da5ab05 --- /dev/null +++ b/tests/testsFail/tests/b.nim @@ -0,0 +1,6 @@ +import testing123, unittest + +test "can compile nimble": + echo "Failing Second test" + assert(false) + diff --git a/tests/testsFail/tests/c.nim b/tests/testsFail/tests/c.nim new file mode 100644 index 0000000..0aae686 --- /dev/null +++ b/tests/testsFail/tests/c.nim @@ -0,0 +1,7 @@ +import testing123, unittest + +test "can compile nimble": + echo "Third test" + myFunc() + + diff --git a/tests/testsPass/testing123.nim b/tests/testsPass/testing123.nim new file mode 100644 index 0000000..2c96a26 --- /dev/null +++ b/tests/testsPass/testing123.nim @@ -0,0 +1,4 @@ + +proc myFunc*() = + echo "Executing my func" + diff --git a/tests/testsPass/testing123.nimble b/tests/testsPass/testing123.nimble new file mode 100644 index 0000000..00e43bf --- /dev/null +++ b/tests/testsPass/testing123.nimble @@ -0,0 +1,4 @@ +version = "0.1.0" +author = "John Doe" +description = "Nimble Test" +license = "BSD" diff --git a/tests/testsPass/tests/one.nim b/tests/testsPass/tests/one.nim new file mode 100644 index 0000000..09f3215 --- /dev/null +++ b/tests/testsPass/tests/one.nim @@ -0,0 +1,6 @@ +import testing123, unittest + +test "can compile nimble": + echo "First test" + myFunc() + diff --git a/tests/testsPass/tests/three.nim b/tests/testsPass/tests/three.nim new file mode 100644 index 0000000..0aae686 --- /dev/null +++ b/tests/testsPass/tests/three.nim @@ -0,0 +1,7 @@ +import testing123, unittest + +test "can compile nimble": + echo "Third test" + myFunc() + + diff --git a/tests/testsPass/tests/two.nim b/tests/testsPass/tests/two.nim new file mode 100644 index 0000000..91413e1 --- /dev/null +++ b/tests/testsPass/tests/two.nim @@ -0,0 +1,7 @@ +import testing123, unittest + +test "can compile nimble": + echo "Second test" + myFunc() + +