Implements check command.

This commit is contained in:
Dominik Picheta 2017-10-15 15:28:29 +01:00
commit c7b97bb206
7 changed files with 94 additions and 5 deletions

View file

@ -129,6 +129,20 @@ a third-party package list.
Package lists can be specified in Nimble's config. Take a look at the
config section below to see how to do this.
### nimble check
The ``check`` command will read your package's .nimble file. It will then
verify that the package's structure is valid.
Example:
$ nimble check
Error: Package 'x' has an incorrect structure. It should contain a single directory hierarchy for source files, named 'x', but file 'foobar.nim' is in a directory named 'incorrect' instead. This will be an error in the future.
Hint: If 'incorrect' contains source files for building 'x', rename it to 'x'. Otherwise, prevent its installation by adding `skipDirs = @["incorrect"]` to the .nimble file.
Failure: Validation failed
### nimble install
The ``install`` command will download and install a package. You need to pass

View file

@ -919,6 +919,25 @@ proc test(options: Options) =
display("Success:", "All tests passed", Success, HighPriority)
proc check(options: Options) =
## Validates a package a in the current working directory.
let nimbleFile = findNimbleFile(getCurrentDir(), true)
var error: ValidationError
var pkgInfo: PackageInfo
var validationResult = false
try:
validationResult = validate(nimbleFile, options, error, pkgInfo)
except:
raiseNimbleError("Could not validate package:\n" & getCurrentExceptionMsg())
if validationResult:
display("Success:", pkgInfo.name & " is valid!", Success, HighPriority)
else:
display("Error:", error.msg, Error, HighPriority)
display("Hint:", error.hint, Warning, HighPriority)
display("Failure:", "Validation failed", Error, HighPriority)
quit(QuitFailure)
proc doAction(options: Options) =
if options.showHelp:
writeHelp()
@ -972,6 +991,8 @@ proc doAction(options: Options) =
listTasks(options)
of actionDevelop:
develop(options)
of actionCheck:
check(options)
of actionNil:
assert false
of actionCustom:

View file

@ -82,7 +82,6 @@ proc displayLine(category, line: string, displayType: DisplayType,
proc display*(category, msg: string, displayType = Message,
priority = MediumPriority) =
# Don't print any Warning, Message or Success messages when suppression of
# warnings is enabled. That is, unless the user asked for --verbose output.
if globalCLI.suppressMessages and displayType >= Warning and

View file

@ -8,6 +8,11 @@ import packageparser, common, packageinfo, options, nimscriptsupport, cli
proc execHook*(options: Options, before: bool): bool =
## Returns whether to continue.
result = true
# For certain commands hooks should not be evaluated.
if options.action.typ in noHookActions:
return
var nimbleFile = ""
try:
nimbleFile = findNimbleFile(getCurrentDir(), true)

View file

@ -27,11 +27,11 @@ type
actionNil, actionRefresh, actionInit, actionDump, actionPublish,
actionInstall, actionSearch,
actionList, actionBuild, actionPath, actionUninstall, actionCompile,
actionDoc, actionCustom, actionTasks, actionDevelop
actionDoc, actionCustom, actionTasks, actionDevelop, actionCheck
Action* = object
case typ*: ActionType
of actionNil, actionList, actionPublish, actionTasks: nil
of actionNil, actionList, actionPublish, actionTasks, actionCheck: nil
of actionRefresh:
optionalURL*: string # Overrides default package list.
of actionInstall, actionPath, actionUninstall, actionDevelop:
@ -60,6 +60,8 @@ Commands:
develop [pkgname, ...] Clones a list of packages for development.
Symlinks the cloned packages or any package
in the current working directory.
check Verifies the validity of a package in the
current working directory.
init [pkgname] Initializes a new Nimble project.
publish Publishes a package on nim-lang/packages.
The current working directory needs to be the
@ -105,6 +107,8 @@ For more information read the Github readme:
https://github.com/nim-lang/nimble#readme
"""
const noHookActions* = {actionCheck}
proc writeHelp*(quit=true) =
echo(help)
if quit:
@ -151,6 +155,8 @@ proc parseActionType*(action: string): ActionType =
result = actionTasks
of "develop":
result = actionDevelop
of "check":
result = actionCheck
else:
result = actionCustom
@ -178,7 +184,7 @@ proc initAction*(options: var Options, key: string) =
options.action.command = key
options.action.arguments = @[]
options.action.flags = newStringTable()
of actionPublish, actionList, actionTasks,
of actionPublish, actionList, actionTasks, actionCheck,
actionNil: discard
proc prompt*(options: Options, question: string): bool =

View file

@ -329,6 +329,16 @@ proc readPackageInfo(nf: NimbleFile, options: Options,
validateVersion(result.version)
validatePackageInfo(result, options)
proc validate*(file: NimbleFile, options: Options,
error: var ValidationError, pkgInfo: var PackageInfo): bool =
try:
pkgInfo = readPackageInfo(file, options)
except ValidationError as exc:
error = exc[]
return false
return true
proc getPkgInfoFromFile*(file: NimbleFile, options: Options): PackageInfo =
## Reads the specified .nimble file and returns its data as a PackageInfo
## object. Any validation errors are handled and displayed as warnings.

View file

@ -593,4 +593,38 @@ suite "test command":
cd "testCommand/testOverride":
let (outp, exitCode) = execNimble("test")
check exitCode == QuitSuccess
check outp.processOutput.inLines("overriden")
check outp.processOutput.inLines("overriden")
suite "check command":
test "can succeed package":
cd "binaryPackage/v1":
let (outp, exitCode) = execNimble("check")
check exitCode == QuitSuccess
check outp.processOutput.inLines("success")
check outp.processOutput.inLines("binaryPackage is valid")
cd "packageStructure/a":
let (outp, exitCode) = execNimble("check")
check exitCode == QuitSuccess
check outp.processOutput.inLines("success")
check outp.processOutput.inLines("a is valid")
cd "packageStructure/b":
let (outp, exitCode) = execNimble("check")
check exitCode == QuitSuccess
check outp.processOutput.inLines("success")
check outp.processOutput.inLines("b is valid")
cd "packageStructure/c":
let (outp, exitCode) = execNimble("check")
check exitCode == QuitSuccess
check outp.processOutput.inLines("success")
check outp.processOutput.inLines("c is valid")
test "can fail package":
cd "packageStructure/x":
let (outp, exitCode) = execNimble("check")
check exitCode == QuitFailure
check outp.processOutput.inLines("failure")
check outp.processOutput.inLines("validation failed")
check outp.processOutput.inLines("package 'x' has an incorrect structure")