Improves pre and post hooks. Fixes #524.
This commit is contained in:
parent
2e803ec9cf
commit
db222bbae1
4 changed files with 107 additions and 76 deletions
|
|
@ -330,6 +330,13 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
|
||||||
## to the packages this package depends on.
|
## to the packages this package depends on.
|
||||||
## The return value of this function is used by
|
## The return value of this function is used by
|
||||||
## ``processDeps`` to gather a list of paths to pass to the nim compiler.
|
## ``processDeps`` to gather a list of paths to pass to the nim compiler.
|
||||||
|
|
||||||
|
# Handle pre-`install` hook.
|
||||||
|
if not options.depsOnly:
|
||||||
|
cd dir: # Make sure `execHook` executes the correct .nimble file.
|
||||||
|
if not execHook(options, true):
|
||||||
|
raise newException(NimbleError, "Pre-hook prevented further execution.")
|
||||||
|
|
||||||
var pkgInfo = getPkgInfo(dir, options)
|
var pkgInfo = getPkgInfo(dir, options)
|
||||||
let realDir = pkgInfo.getRealDir()
|
let realDir = pkgInfo.getRealDir()
|
||||||
let binDir = options.getBinDir()
|
let binDir = options.getBinDir()
|
||||||
|
|
@ -433,6 +440,12 @@ proc installFromDir(dir: string, requestedVer: VersionRange, options: Options,
|
||||||
display("Success:", pkgInfo.name & " installed successfully.",
|
display("Success:", pkgInfo.name & " installed successfully.",
|
||||||
Success, HighPriority)
|
Success, HighPriority)
|
||||||
|
|
||||||
|
# Run post-install hook now that package is installed. The `execHook` proc
|
||||||
|
# executes the hook defined in the CWD, so we set it to where the package
|
||||||
|
# has been installed.
|
||||||
|
cd dest.splitFile.dir:
|
||||||
|
discard execHook(options, false)
|
||||||
|
|
||||||
proc getDownloadInfo*(pv: PkgTuple, options: Options,
|
proc getDownloadInfo*(pv: PkgTuple, options: Options,
|
||||||
doPrompt: bool): (DownloadMethod, string,
|
doPrompt: bool): (DownloadMethod, string,
|
||||||
Table[string, string]) =
|
Table[string, string]) =
|
||||||
|
|
@ -472,17 +485,7 @@ proc install(packages: seq[PkgTuple],
|
||||||
let (downloadDir, downloadVersion) =
|
let (downloadDir, downloadVersion) =
|
||||||
downloadPkg(url, pv.ver, meth, subdir, options)
|
downloadPkg(url, pv.ver, meth, subdir, options)
|
||||||
try:
|
try:
|
||||||
# Run pre-install hook in download directory now that package is downloaded
|
|
||||||
cd downloadDir:
|
|
||||||
if not execHook(options, true):
|
|
||||||
raise newException(NimbleError, "Pre-hook prevented further execution.")
|
|
||||||
|
|
||||||
result = installFromDir(downloadDir, pv.ver, options, url)
|
result = installFromDir(downloadDir, pv.ver, options, url)
|
||||||
|
|
||||||
# Run post-install hook in installed directory now that package is installed
|
|
||||||
# Standard hooks run in current directory so it won't detect this new package
|
|
||||||
cd result.pkg.myPath.parentDir():
|
|
||||||
discard execHook(options, false)
|
|
||||||
except BuildFailed:
|
except BuildFailed:
|
||||||
# The package failed to build.
|
# The package failed to build.
|
||||||
# Check if we tried building a tagged version of the package.
|
# Check if we tried building a tagged version of the package.
|
||||||
|
|
@ -945,6 +948,10 @@ proc developFromDir(dir: string, options: Options) =
|
||||||
if options.depsOnly:
|
if options.depsOnly:
|
||||||
raiseNimbleError("Cannot develop dependencies only.")
|
raiseNimbleError("Cannot develop dependencies only.")
|
||||||
|
|
||||||
|
cd dir: # Make sure `execHook` executes the correct .nimble file.
|
||||||
|
if not execHook(options, true):
|
||||||
|
raise newException(NimbleError, "Pre-hook prevented further execution.")
|
||||||
|
|
||||||
var pkgInfo = getPkgInfo(dir, options)
|
var pkgInfo = getPkgInfo(dir, options)
|
||||||
if pkgInfo.bin.len > 0:
|
if pkgInfo.bin.len > 0:
|
||||||
if "nim" in pkgInfo.skipExt:
|
if "nim" in pkgInfo.skipExt:
|
||||||
|
|
@ -994,6 +1001,10 @@ proc developFromDir(dir: string, options: Options) =
|
||||||
display("Success:", (pkgInfo.name & " linked successfully to '$1'.") %
|
display("Success:", (pkgInfo.name & " linked successfully to '$1'.") %
|
||||||
dir, Success, HighPriority)
|
dir, Success, HighPriority)
|
||||||
|
|
||||||
|
# Execute the post-develop hook.
|
||||||
|
cd dir:
|
||||||
|
discard execHook(options, false)
|
||||||
|
|
||||||
proc develop(options: Options) =
|
proc develop(options: Options) =
|
||||||
if options.action.packages == @[]:
|
if options.action.packages == @[]:
|
||||||
developFromDir(getCurrentDir(), options)
|
developFromDir(getCurrentDir(), options)
|
||||||
|
|
@ -1064,10 +1075,6 @@ 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):
|
|
||||||
display("Warning", "Pre-hook prevented further execution.", Warning,
|
|
||||||
HighPriority)
|
|
||||||
return
|
|
||||||
case options.action.typ
|
case options.action.typ
|
||||||
of actionRefresh:
|
of actionRefresh:
|
||||||
refresh(options)
|
refresh(options)
|
||||||
|
|
@ -1111,6 +1118,10 @@ proc doAction(options: Options) =
|
||||||
of actionNil:
|
of actionNil:
|
||||||
assert false
|
assert false
|
||||||
of actionCustom:
|
of actionCustom:
|
||||||
|
if not execHook(options, true):
|
||||||
|
display("Warning", "Pre-hook prevented further execution.", Warning,
|
||||||
|
HighPriority)
|
||||||
|
return
|
||||||
let isPreDefined = options.action.command.normalize == "test"
|
let isPreDefined = options.action.command.normalize == "test"
|
||||||
|
|
||||||
var execResult: ExecutionResult[void]
|
var execResult: ExecutionResult[void]
|
||||||
|
|
@ -1125,9 +1136,6 @@ proc doAction(options: Options) =
|
||||||
# Run the post hook for `test` in case it exists.
|
# Run the post hook for `test` in case it exists.
|
||||||
discard execHook(options, false)
|
discard execHook(options, false)
|
||||||
|
|
||||||
if options.action.typ != actionCustom:
|
|
||||||
discard execHook(options, false)
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
var error = ""
|
var error = ""
|
||||||
var hint = ""
|
var hint = ""
|
||||||
|
|
|
||||||
|
|
@ -362,7 +362,7 @@ proc execScript(scriptName: string, flags: Flags, options: Options): PSym =
|
||||||
|
|
||||||
# Ensure that "nimblepkg/nimscriptapi" is in the PATH.
|
# Ensure that "nimblepkg/nimscriptapi" is in the PATH.
|
||||||
block:
|
block:
|
||||||
let t = options.getNimbleDir / "nimblecache"
|
let t = getTempDir() / "nimblecache"
|
||||||
let tmpNimscriptApiPath = t / "nimblepkg" / "nimscriptapi.nim"
|
let tmpNimscriptApiPath = t / "nimblepkg" / "nimscriptapi.nim"
|
||||||
createDir(tmpNimscriptApiPath.splitFile.dir)
|
createDir(tmpNimscriptApiPath.splitFile.dir)
|
||||||
writeFile(tmpNimscriptApiPath, nimscriptApi)
|
writeFile(tmpNimscriptApiPath, nimscriptApi)
|
||||||
|
|
|
||||||
|
|
@ -43,3 +43,9 @@ before hooks2:
|
||||||
|
|
||||||
task hooks2, "Testing the hooks again":
|
task hooks2, "Testing the hooks again":
|
||||||
echo("Shouldn't happen")
|
echo("Shouldn't happen")
|
||||||
|
|
||||||
|
before install:
|
||||||
|
echo("Before PkgDir: ", getPkgDir())
|
||||||
|
|
||||||
|
after install:
|
||||||
|
echo("After PkgDir: ", getPkgDir())
|
||||||
133
tests/tester.nim
133
tests/tester.nim
|
|
@ -36,7 +36,10 @@ proc execNimble(args: varargs[string]): tuple[output: string, exitCode: int] =
|
||||||
quotedArgs.add("--nimbleDir:" & installDir)
|
quotedArgs.add("--nimbleDir:" & installDir)
|
||||||
quotedArgs = quotedArgs.map((x: string) => ("\"" & x & "\""))
|
quotedArgs = quotedArgs.map((x: string) => ("\"" & x & "\""))
|
||||||
|
|
||||||
result = execCmdEx(quotedArgs.join(" "))
|
let path = getCurrentDir().parentDir() / "src"
|
||||||
|
|
||||||
|
let cmd = "PATH=" & path & ":$PATH " & quotedArgs.join(" ")
|
||||||
|
result = execCmdEx(cmd)
|
||||||
checkpoint(result.output)
|
checkpoint(result.output)
|
||||||
|
|
||||||
proc execNimbleYes(args: varargs[string]): tuple[output: string, exitCode: int]=
|
proc execNimbleYes(args: varargs[string]): tuple[output: string, exitCode: int]=
|
||||||
|
|
@ -235,71 +238,82 @@ test "package list can only have one source":
|
||||||
check inLines(lines, "Attempted to specify `url` and `path` for the same package list 'local'")
|
check inLines(lines, "Attempted to specify `url` and `path` for the same package list 'local'")
|
||||||
check exitCode == QuitFailure
|
check exitCode == QuitFailure
|
||||||
|
|
||||||
test "can install nimscript package":
|
suite "nimscript":
|
||||||
cd "nimscript":
|
test "can install nimscript package":
|
||||||
check execNimble(["install", "-y"]).exitCode == QuitSuccess
|
cd "nimscript":
|
||||||
|
check execNimble(["install", "-y"]).exitCode == QuitSuccess
|
||||||
|
|
||||||
test "can execute nimscript tasks":
|
test "before/after install pkg dirs are correct":
|
||||||
cd "nimscript":
|
cd "nimscript":
|
||||||
let (output, exitCode) = execNimble("--verbose", "work")
|
let (output, exitCode) = execNimble(["install", "-y"])
|
||||||
let lines = output.strip.splitLines()
|
check exitCode == QuitSuccess
|
||||||
check exitCode == QuitSuccess
|
let lines = output.strip.splitLines()
|
||||||
check lines[^1] == "10"
|
check lines[0].startsWith("Before PkgDir:")
|
||||||
|
check lines[0].endsWith("tests/nimscript")
|
||||||
|
check lines[^1].startsWith("After PkgDir:")
|
||||||
|
check lines[^1].endsWith("tests/nimbleDir/pkgs/nimscript-0.1.0")
|
||||||
|
|
||||||
test "can use nimscript's setCommand":
|
test "can execute nimscript tasks":
|
||||||
cd "nimscript":
|
cd "nimscript":
|
||||||
let (output, exitCode) = execNimble("--verbose", "cTest")
|
let (output, exitCode) = execNimble("--verbose", "work")
|
||||||
let lines = output.strip.splitLines()
|
let lines = output.strip.splitLines()
|
||||||
check exitCode == QuitSuccess
|
check exitCode == QuitSuccess
|
||||||
check "Execution finished".normalize in lines[^1].normalize
|
check lines[^1] == "10"
|
||||||
|
|
||||||
test "can use nimscript's setCommand with flags":
|
test "can use nimscript's setCommand":
|
||||||
cd "nimscript":
|
cd "nimscript":
|
||||||
let (output, exitCode) = execNimble("--debug", "cr")
|
let (output, exitCode) = execNimble("--verbose", "cTest")
|
||||||
let lines = output.strip.splitLines()
|
let lines = output.strip.splitLines()
|
||||||
check exitCode == QuitSuccess
|
check exitCode == QuitSuccess
|
||||||
check inLines(lines, "Hello World")
|
check "Execution finished".normalize in lines[^1].normalize
|
||||||
|
|
||||||
test "can use nimscript with repeated flags (issue #329)":
|
test "can use nimscript's setCommand with flags":
|
||||||
cd "nimscript":
|
cd "nimscript":
|
||||||
let (output, exitCode) = execNimble("--debug", "repeated")
|
let (output, exitCode) = execNimble("--debug", "cr")
|
||||||
let lines = output.strip.splitLines()
|
let lines = output.strip.splitLines()
|
||||||
check exitCode == QuitSuccess
|
check exitCode == QuitSuccess
|
||||||
var found = false
|
check inLines(lines, "Hello World")
|
||||||
for line in lines:
|
|
||||||
if line.contains("--define:foo"):
|
|
||||||
found = true
|
|
||||||
check found == true
|
|
||||||
|
|
||||||
test "can list nimscript tasks":
|
test "can use nimscript with repeated flags (issue #329)":
|
||||||
cd "nimscript":
|
cd "nimscript":
|
||||||
let (output, exitCode) = execNimble("tasks")
|
let (output, exitCode) = execNimble("--debug", "repeated")
|
||||||
check "work test description".normalize in output.normalize
|
let lines = output.strip.splitLines()
|
||||||
check exitCode == QuitSuccess
|
check exitCode == QuitSuccess
|
||||||
|
var found = false
|
||||||
|
for line in lines:
|
||||||
|
if line.contains("--define:foo"):
|
||||||
|
found = true
|
||||||
|
check found == true
|
||||||
|
|
||||||
test "can use pre/post hooks":
|
test "can list nimscript tasks":
|
||||||
cd "nimscript":
|
cd "nimscript":
|
||||||
let (output, exitCode) = execNimble("hooks")
|
let (output, exitCode) = execNimble("tasks")
|
||||||
let lines = output.strip.splitLines()
|
check "work test description".normalize in output.normalize
|
||||||
check exitCode == QuitSuccess
|
check exitCode == QuitSuccess
|
||||||
check inLines(lines, "First")
|
|
||||||
check inLines(lines, "middle")
|
|
||||||
check inLines(lines, "last")
|
|
||||||
|
|
||||||
test "pre hook can prevent action":
|
test "can use pre/post hooks":
|
||||||
cd "nimscript":
|
cd "nimscript":
|
||||||
let (output, exitCode) = execNimble("hooks2")
|
let (output, exitCode) = execNimble("hooks")
|
||||||
let lines = output.strip.splitLines()
|
let lines = output.strip.splitLines()
|
||||||
check exitCode == QuitSuccess
|
check exitCode == QuitSuccess
|
||||||
check(not inLines(lines, "Shouldn't happen"))
|
check inLines(lines, "First")
|
||||||
check inLines(lines, "Hook prevented further execution")
|
check inLines(lines, "middle")
|
||||||
|
check inLines(lines, "last")
|
||||||
|
|
||||||
test "nimble script api":
|
test "pre hook can prevent action":
|
||||||
cd "nimscript":
|
cd "nimscript":
|
||||||
let (output, exitCode) = execNimble("api")
|
let (output, exitCode) = execNimble("hooks2")
|
||||||
let lines = output.strip.splitLines()
|
let lines = output.strip.splitLines()
|
||||||
check exitCode == QuitSuccess
|
check exitCode == QuitSuccess
|
||||||
check inLines(lines, "PKG_DIR: " & getCurrentDir())
|
check(not inLines(lines, "Shouldn't happen"))
|
||||||
|
check inLines(lines, "Hook prevented further execution")
|
||||||
|
|
||||||
|
test "nimble script api":
|
||||||
|
cd "nimscript":
|
||||||
|
let (output, exitCode) = execNimble("api")
|
||||||
|
let lines = output.strip.splitLines()
|
||||||
|
check exitCode == QuitSuccess
|
||||||
|
check inLines(lines, "PKG_DIR: " & getCurrentDir())
|
||||||
|
|
||||||
test "can install packagebin2":
|
test "can install packagebin2":
|
||||||
let args = ["install", "-y", "https://github.com/nimble-test/packagebin2.git"]
|
let args = ["install", "-y", "https://github.com/nimble-test/packagebin2.git"]
|
||||||
|
|
@ -491,6 +505,9 @@ test "can install diamond deps (#184)":
|
||||||
checkpoint(output)
|
checkpoint(output)
|
||||||
check exitCode == 0
|
check exitCode == 0
|
||||||
|
|
||||||
|
test "issues #280 and #524":
|
||||||
|
check execNimble("install", "-y", "https://github.com/nimble-test/issue280and524.git").exitCode == 0
|
||||||
|
|
||||||
suite "can handle two binary versions":
|
suite "can handle two binary versions":
|
||||||
setup:
|
setup:
|
||||||
cd "binaryPackage/v1":
|
cd "binaryPackage/v1":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue