Fix tests. Add test for hooks.

This commit is contained in:
Dominik Picheta 2015-12-30 20:20:04 +00:00
commit 1fa209a689
6 changed files with 49 additions and 8 deletions

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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.

View file

@ -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())
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")

View file

@ -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 ==