Fix tests. Add test for hooks.
This commit is contained in:
parent
42ef358459
commit
1fa209a689
6 changed files with 49 additions and 8 deletions
|
|
@ -859,10 +859,15 @@ proc listTasks(options: Options) =
|
||||||
proc execHook(options: Options, before: bool): bool =
|
proc execHook(options: Options, before: bool): bool =
|
||||||
## Returns whether to continue.
|
## Returns whether to continue.
|
||||||
result = true
|
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.
|
# PackageInfos are cached so we can read them as many times as we want.
|
||||||
let pkgInfo = readPackageInfo(nimbleFile, options)
|
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 =
|
let hookExists =
|
||||||
if before: actionName.normalize in pkgInfo.preHooks
|
if before: actionName.normalize in pkgInfo.preHooks
|
||||||
else: actionName.normalize in pkgInfo.postHooks
|
else: actionName.normalize in pkgInfo.postHooks
|
||||||
|
|
@ -877,7 +882,9 @@ proc doAction(options: Options) =
|
||||||
if not existsDir(options.getPkgsDir):
|
if not existsDir(options.getPkgsDir):
|
||||||
createDir(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
|
case options.action.typ
|
||||||
of actionUpdate:
|
of actionUpdate:
|
||||||
update(options)
|
update(options)
|
||||||
|
|
|
||||||
|
|
@ -39,4 +39,6 @@ template after*(action: untyped, body: untyped): untyped =
|
||||||
template builtin = discard
|
template builtin = discard
|
||||||
|
|
||||||
proc getPkgDir*(): string =
|
proc getPkgDir*(): string =
|
||||||
|
## Returns the package directory containing the .nimble file currently
|
||||||
|
## being evaluated.
|
||||||
builtin
|
builtin
|
||||||
|
|
@ -229,7 +229,7 @@ proc execScript(scriptName: string, flags: StringTableRef, options: Options) =
|
||||||
body
|
body
|
||||||
|
|
||||||
cbApi getPkgDir:
|
cbApi getPkgDir:
|
||||||
setResult(a, "FOOBAR")
|
setResult(a, scriptName.splitFile.dir)
|
||||||
|
|
||||||
compileSystemModule()
|
compileSystemModule()
|
||||||
processModule(m, llStreamOpen(scriptName, fmRead), nil)
|
processModule(m, llStreamOpen(scriptName, fmRead), nil)
|
||||||
|
|
|
||||||
|
|
@ -230,6 +230,7 @@ proc readPackageInfo*(nf: NimbleFile, options: Options,
|
||||||
raise newException(NimbleError, msg)
|
raise newException(NimbleError, msg)
|
||||||
|
|
||||||
validatePackageInfo(result, nf)
|
validatePackageInfo(result, nf)
|
||||||
|
if not result.isMinimal:
|
||||||
options.pkgInfoCache[nf] = result
|
options.pkgInfoCache[nf] = result
|
||||||
|
|
||||||
proc getPkgInfo*(dir: string, options: Options): PackageInfo =
|
proc getPkgInfo*(dir: string, options: Options): PackageInfo =
|
||||||
|
|
|
||||||
|
|
@ -23,3 +23,18 @@ task cr, "Testing `nimble c -r nimscript.nim` via setCommand":
|
||||||
|
|
||||||
task api, "Testing nimscriptapi module functionality":
|
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")
|
||||||
|
|
@ -34,8 +34,7 @@ test "issue 113 (uninstallation problems)":
|
||||||
let (output, exitCode) = execCmdEx(path & " remove -y c")
|
let (output, exitCode) = execCmdEx(path & " remove -y c")
|
||||||
let lines = output.strip.splitLines()
|
let lines = output.strip.splitLines()
|
||||||
check exitCode != QuitSuccess
|
check exitCode != QuitSuccess
|
||||||
check "cannot uninstall c (0.1.0) because b (0.1.0) depends on it" in
|
check inLines(lines, "cannot uninstall c (0.1.0) because b (0.1.0) depends on it")
|
||||||
lines[^1].normalize
|
|
||||||
|
|
||||||
check execCmdEx(path & " remove -y a").exitCode == QuitSuccess
|
check execCmdEx(path & " remove -y a").exitCode == QuitSuccess
|
||||||
check execCmdEx(path & " remove -y b").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 "test test description".normalize in output.normalize
|
||||||
check exitCode == QuitSuccess
|
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":
|
test "can install packagebin2":
|
||||||
check execCmdEx(path &
|
check execCmdEx(path &
|
||||||
" install -y https://github.com/nimble-test/packagebin2.git").exitCode ==
|
" install -y https://github.com/nimble-test/packagebin2.git").exitCode ==
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue