From 1fa209a689a7271a09becec6f598163094a55a8e Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Wed, 30 Dec 2015 20:20:04 +0000 Subject: [PATCH] Fix tests. Add test for hooks. --- src/nimble.nim | 13 ++++++++++--- src/nimblepkg/nimscriptapi.nim | 2 ++ src/nimblepkg/nimscriptsupport.nim | 2 +- src/nimblepkg/packageparser.nim | 3 ++- tests/nimscript/nimscript.nimble | 17 ++++++++++++++++- tests/tester.nim | 20 ++++++++++++++++++-- 6 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index 334e1c2..f28f4c6 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -859,10 +859,15 @@ proc listTasks(options: Options) = proc execHook(options: Options, before: bool): bool = ## Returns whether to continue. result = true - let nimbleFile = findNimbleFile(getCurrentDir(), true) + var nimbleFile = "" + try: + nimbleFile = findNimbleFile(getCurrentDir(), true) + except NimbleError: return true # PackageInfos are cached so we can read them as many times as we want. let pkgInfo = readPackageInfo(nimbleFile, options) - let actionName = ($options.action.typ)[6 .. ^1] + let actionName = + if options.action.typ == actionCustom: options.action.command + else: ($options.action.typ)[6 .. ^1] let hookExists = if before: actionName.normalize in pkgInfo.preHooks else: actionName.normalize in pkgInfo.postHooks @@ -877,7 +882,9 @@ proc doAction(options: Options) = if not existsDir(options.getPkgsDir): createDir(options.getPkgsDir) - if not execHook(options, true): return + if not execHook(options, true): + echo("Pre-hook prevented further execution.") + return case options.action.typ of actionUpdate: update(options) diff --git a/src/nimblepkg/nimscriptapi.nim b/src/nimblepkg/nimscriptapi.nim index b264a07..1d7c4bd 100644 --- a/src/nimblepkg/nimscriptapi.nim +++ b/src/nimblepkg/nimscriptapi.nim @@ -39,4 +39,6 @@ template after*(action: untyped, body: untyped): untyped = template builtin = discard proc getPkgDir*(): string = + ## Returns the package directory containing the .nimble file currently + ## being evaluated. builtin \ No newline at end of file diff --git a/src/nimblepkg/nimscriptsupport.nim b/src/nimblepkg/nimscriptsupport.nim index d62a208..267c4d3 100644 --- a/src/nimblepkg/nimscriptsupport.nim +++ b/src/nimblepkg/nimscriptsupport.nim @@ -229,7 +229,7 @@ proc execScript(scriptName: string, flags: StringTableRef, options: Options) = body cbApi getPkgDir: - setResult(a, "FOOBAR") + setResult(a, scriptName.splitFile.dir) compileSystemModule() processModule(m, llStreamOpen(scriptName, fmRead), nil) diff --git a/src/nimblepkg/packageparser.nim b/src/nimblepkg/packageparser.nim index ed9f4ac..c8555d4 100644 --- a/src/nimblepkg/packageparser.nim +++ b/src/nimblepkg/packageparser.nim @@ -230,7 +230,8 @@ proc readPackageInfo*(nf: NimbleFile, options: Options, raise newException(NimbleError, msg) validatePackageInfo(result, nf) - options.pkgInfoCache[nf] = result + if not result.isMinimal: + options.pkgInfoCache[nf] = result proc getPkgInfo*(dir: string, options: Options): PackageInfo = ## Find the .nimble file in ``dir`` and parses it, returning a PackageInfo. diff --git a/tests/nimscript/nimscript.nimble b/tests/nimscript/nimscript.nimble index ab56b06..bfa0405 100644 --- a/tests/nimscript/nimscript.nimble +++ b/tests/nimscript/nimscript.nimble @@ -22,4 +22,19 @@ task cr, "Testing `nimble c -r nimscript.nim` via setCommand": setCommand "c", "nimscript.nim" task api, "Testing nimscriptapi module functionality": - echo(getPkgDir()) \ No newline at end of file + echo(getPkgDir()) + +before hooks: + echo("First") + +task hooks, "Testing the hooks": + echo("Middle") + +after hooks: + echo("last") + +before hooks2: + return false + +task hooks2, "Testing the hooks again": + echo("Shouldn't happen") \ No newline at end of file diff --git a/tests/tester.nim b/tests/tester.nim index 69b238a..eb73db5 100644 --- a/tests/tester.nim +++ b/tests/tester.nim @@ -34,8 +34,7 @@ test "issue 113 (uninstallation problems)": let (output, exitCode) = execCmdEx(path & " remove -y c") let lines = output.strip.splitLines() check exitCode != QuitSuccess - check "cannot uninstall c (0.1.0) because b (0.1.0) depends on it" in - lines[^1].normalize + check inLines(lines, "cannot uninstall c (0.1.0) because b (0.1.0) depends on it") check execCmdEx(path & " remove -y a").exitCode == QuitSuccess check execCmdEx(path & " remove -y b").exitCode == QuitSuccess @@ -113,6 +112,23 @@ test "can list nimscript tasks": check "test test description".normalize in output.normalize check exitCode == QuitSuccess +test "can use pre/post hooks": + cd "nimscript": + let (output, exitCode) = execCmdEx("../" & path & " hooks") + let lines = output.strip.splitLines() + check exitCode == QuitSuccess + check inLines(lines, "First") + check inLines(lines, "middle") + check inLines(lines, "last") + +test "pre hook can prevent action": + cd "nimscript": + let (output, exitCode) = execCmdEx("../" & path & " hooks2") + let lines = output.strip.splitLines() + check exitCode == QuitSuccess + check(not inLines(lines, "Shouldn't happen")) + check inLines(lines, "Hook prevented further execution") + test "can install packagebin2": check execCmdEx(path & " install -y https://github.com/nimble-test/packagebin2.git").exitCode ==