Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up all build files #745

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 60 additions & 35 deletions src/nimble.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1038,53 +1038,58 @@ proc develop(options: Options) =
discard downloadPkg(url, ver, meth, subdir, options, downloadDir)
developFromDir(downloadDir / subdir, options)

proc walkTests(): seq[(string, string)] =
var files = toSeq(walkDir(getCurrentDir() / "tests"))
if files.len < 1:
return
files.sort((a, b) => cmp(a.path, b.path))
for file in files:
let (_, name, ext) = file.path.splitFile()
if ext == ".nim" and name[0] == 't' and file.kind in {pcFile, pcLinkToFile}:
let binFileName = file.path.changeFileExt(ExeExt)
result.add((file.path, binFileName))

proc test(options: Options) =
## Executes all tests starting with 't' in the ``tests`` directory.
## Subdirectories are not walked.
var pkgInfo = getPkgInfo(getCurrentDir(), options)

var
files = toSeq(walkDir(getCurrentDir() / "tests"))
tests, failures: int
var tests, failures: int

let files = walkTests()
if files.len < 1:
display("Warning:", "No tests found!", Warning, HighPriority)
return

files.sort((a, b) => cmp(a.path, b.path))

for file in files:
let (_, name, ext) = file.path.splitFile()
if ext == ".nim" and name[0] == 't' and file.kind in {pcFile, pcLinkToFile}:
var optsCopy = options.briefClone()
optsCopy.action = Action(typ: actionCompile)
optsCopy.action.file = file.path
optsCopy.action.backend = pkgInfo.backend
optsCopy.getCompilationFlags() = @[]
optsCopy.getCompilationFlags().add("-r")
optsCopy.getCompilationFlags().add("--path:.")
let
binFileName = file.path.changeFileExt(ExeExt)
existsBefore = existsFile(binFileName)

if options.continueTestsOnFailure:
inc tests
try:
execBackend(pkgInfo, optsCopy)
except NimbleError:
inc failures
else:
for fileInfo in files:
let (filename, binFileName) = fileInfo
var optsCopy = options.briefClone()
optsCopy.action = Action(typ: actionCompile)
optsCopy.action.file = filename
optsCopy.action.backend = pkgInfo.backend
optsCopy.getCompilationFlags() = @[]
optsCopy.getCompilationFlags().add("-r")
optsCopy.getCompilationFlags().add("--path:.")
let existsBefore = existsFile(binFileName)

if options.continueTestsOnFailure:
inc tests
try:
execBackend(pkgInfo, optsCopy)
except NimbleError:
inc failures
else:
execBackend(pkgInfo, optsCopy)

let
existsAfter = existsFile(binFileName)
canRemove = not existsBefore and existsAfter
if canRemove:
try:
removeFile(binFileName)
except OSError as exc:
display("Warning:", "Failed to delete " & binFileName & ": " &
exc.msg, Warning, MediumPriority)
let
existsAfter = existsFile(binFileName)
canRemove = not existsBefore and existsAfter
if canRemove:
try:
removeFile(binFileName)
except OSError as exc:
display("Warning:", "Failed to delete " & binFileName & ": " &
exc.msg, Warning, MediumPriority)

if failures == 0:
display("Success:", "All tests passed", Success, HighPriority)
Expand Down Expand Up @@ -1134,6 +1139,24 @@ proc run(options: Options) =

doCmd("$# $#" % [binaryPath, args], showOutput = true)

proc clean(options: Options) =
## Clean up all build files.
let cwd = getCurrentDir()
let pkgInfo = getPkgInfo(cwd, options)

for filename in pkgInfo.bin:
if existsFile(filename):
removeFile(filename)
display("Hint:", filename & " removed.", Success, HighPriority)

let tests = walkTests()
for files in tests:
let (_, binFileName) = files
if existsFile(binFileName):
removeFile(binFileName)
let relativeName = binFileName.replace(cwd & "/", "")
display("Hint:", relativeName & " removed.", Success, HighPriority)

proc doAction(options: var Options) =
if options.showHelp:
writeHelp()
Expand Down Expand Up @@ -1192,6 +1215,8 @@ proc doAction(options: var Options) =
develop(options)
of actionCheck:
check(options)
of actionClean:
clean(options)
of actionNil:
assert false
of actionCustom:
Expand Down
10 changes: 7 additions & 3 deletions src/nimblepkg/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ type
actionInstall, actionSearch,
actionList, actionBuild, actionPath, actionUninstall, actionCompile,
actionDoc, actionCustom, actionTasks, actionDevelop, actionCheck,
actionRun
actionRun, actionClean

Action* = object
case typ*: ActionType
of actionNil, actionList, actionPublish, actionTasks, actionCheck: nil
of actionNil, actionList, actionPublish, actionTasks, actionCheck, actionClean:
nil
of actionRefresh:
optionalURL*: string # Overrides default package list.
of actionInstall, actionPath, actionUninstall, actionDevelop:
Expand Down Expand Up @@ -116,6 +117,7 @@ Commands:
external tools. The argument can be a
.nimble file, a project directory or
the name of an installed package.
clean Clean up all build files.


Options:
Expand Down Expand Up @@ -186,6 +188,8 @@ proc parseActionType*(action: string): ActionType =
result = actionDevelop
of "check":
result = actionCheck
of "clean":
result = actionClean
else:
result = actionCustom

Expand Down Expand Up @@ -218,7 +222,7 @@ proc initAction*(options: var Options, key: string) =
options.action.arguments = @[]
options.action.flags = newStringTable()
of actionPublish, actionList, actionTasks, actionCheck, actionRun,
actionNil: discard
actionNil, actionClean: discard

proc prompt*(options: Options, question: string): bool =
## Asks an interactive question and returns the result.
Expand Down
27 changes: 27 additions & 0 deletions tests/tester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1025,3 +1025,30 @@ test "compilation without warnings":
check exitCode == QuitSuccess
linesWithWarningsCount += checkOutput(output)
check linesWithWarningsCount == 0

suite "clean command":

test "can clean binary file":
cd "binaryPackage/v1":
check execNimble("build").exitCode == QuitSuccess
var binPath = "binaryPackage"
when defined(windows):
binPath = binPath & ".exe"
check fileExists(binPath) == true

check execNimble("clean").exitCode == QuitSuccess

check fileExists(binPath) == false

test "can clean test files":
cd "testCommand/testsFail":
var testPath = "tests/t2"
when defined(windows):
testPath = testPath & ".exe"
let f = open(testPath, fmWrite)
f.close()
check fileExists(testPath) == true

check execNimble("clean").exitCode == QuitSuccess

check fileExists(testPath) == false