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 Package lists can be specified in Nimble's config. Take a look at the
config section below to see how to do this. 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 ### nimble install
The ``install`` command will download and install a package. You need to pass 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) 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) = proc doAction(options: Options) =
if options.showHelp: if options.showHelp:
writeHelp() writeHelp()
@ -972,6 +991,8 @@ proc doAction(options: Options) =
listTasks(options) listTasks(options)
of actionDevelop: of actionDevelop:
develop(options) develop(options)
of actionCheck:
check(options)
of actionNil: of actionNil:
assert false assert false
of actionCustom: of actionCustom:

View file

@ -82,7 +82,6 @@ proc displayLine(category, line: string, displayType: DisplayType,
proc display*(category, msg: string, displayType = Message, proc display*(category, msg: string, displayType = Message,
priority = MediumPriority) = priority = MediumPriority) =
# Don't print any Warning, Message or Success messages when suppression of # Don't print any Warning, Message or Success messages when suppression of
# warnings is enabled. That is, unless the user asked for --verbose output. # warnings is enabled. That is, unless the user asked for --verbose output.
if globalCLI.suppressMessages and displayType >= Warning and 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 = proc execHook*(options: Options, before: bool): bool =
## Returns whether to continue. ## Returns whether to continue.
result = true result = true
# For certain commands hooks should not be evaluated.
if options.action.typ in noHookActions:
return
var nimbleFile = "" var nimbleFile = ""
try: try:
nimbleFile = findNimbleFile(getCurrentDir(), true) nimbleFile = findNimbleFile(getCurrentDir(), true)

View file

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

View file

@ -329,6 +329,16 @@ proc readPackageInfo(nf: NimbleFile, options: Options,
validateVersion(result.version) validateVersion(result.version)
validatePackageInfo(result, options) 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 = proc getPkgInfoFromFile*(file: NimbleFile, options: Options): PackageInfo =
## Reads the specified .nimble file and returns its data as a PackageInfo ## Reads the specified .nimble file and returns its data as a PackageInfo
## object. Any validation errors are handled and displayed as warnings. ## object. Any validation errors are handled and displayed as warnings.

View file

@ -593,4 +593,38 @@ suite "test command":
cd "testCommand/testOverride": cd "testCommand/testOverride":
let (outp, exitCode) = execNimble("test") let (outp, exitCode) = execNimble("test")
check exitCode == QuitSuccess 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")