From 1f1e324237b24ed21383d1467414765239edbebe Mon Sep 17 00:00:00 2001 From: Xie Yanbo Date: Thu, 13 Feb 2020 19:37:17 +0800 Subject: [PATCH] clean up all build files --- src/nimble.nim | 95 ++++++++++++++++++++++++--------------- src/nimblepkg/options.nim | 10 +++-- tests/tester.nim | 27 +++++++++++ 3 files changed, 94 insertions(+), 38 deletions(-) diff --git a/src/nimble.nim b/src/nimble.nim index 6d233f68..9a962741 100644 --- a/src/nimble.nim +++ b/src/nimble.nim @@ -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) @@ -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() @@ -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: diff --git a/src/nimblepkg/options.nim b/src/nimblepkg/options.nim index 14d1c31e..6dae7fd5 100644 --- a/src/nimblepkg/options.nim +++ b/src/nimblepkg/options.nim @@ -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: @@ -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: @@ -186,6 +188,8 @@ proc parseActionType*(action: string): ActionType = result = actionDevelop of "check": result = actionCheck + of "clean": + result = actionClean else: result = actionCustom @@ -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. diff --git a/tests/tester.nim b/tests/tester.nim index c5d9925a..fee44c25 100644 --- a/tests/tester.nim +++ b/tests/tester.nim @@ -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