diff options
-rw-r--r-- | test/common-tap.js | 33 | ||||
-rw-r--r-- | test/tap/zz-cleanup.js | 15 |
2 files changed, 43 insertions, 5 deletions
diff --git a/test/common-tap.js b/test/common-tap.js index 745528944..2efca30c4 100644 --- a/test/common-tap.js +++ b/test/common-tap.js @@ -1,13 +1,13 @@ var spawn = require("child_process").spawn +var path = require("path") +var fs = require("fs") var port = exports.port = 1337 exports.registry = "http://localhost:" + port process.env.npm_config_loglevel = "error" -var npm_config_cache = __dirname + '/npm_cache' -exports.child_env = { - npm_config_cache: npm_config_cache, -} +var npm_config_cache = path.resolve(__dirname, "npm_cache") +exports.npm_config_cache = npm_config_cache var bin = exports.bin = require.resolve("../bin/npm-cli.js") var once = require("once") @@ -19,7 +19,7 @@ exports.npm = function (cmd, opts, cb) { opts.env = opts.env ? opts.env : process.env if (!opts.env.npm_config_cache) { - opts.env.npm_config_cache = __dirname + "/npm_cache" + opts.env.npm_config_cache = npm_config_cache } var stdout = "" @@ -42,3 +42,26 @@ exports.npm = function (cmd, opts, cb) { }) return child } + +// based on http://bit.ly/1tkI6DJ +function deleteNpmCacheRecursivelySync(cache) { + cache = cache ? cache : npm_config_cache + var files = [] + var res + if( fs.existsSync(cache) ) { + files = fs.readdirSync(cache) + files.forEach(function(file,index) { + var curPath = path.resolve(cache, file) + if(fs.lstatSync(curPath).isDirectory()) { // recurse + deleteNpmCacheRecursivelySync(curPath) + } else { // delete file + if (res = fs.unlinkSync(curPath)) + throw Error("Failed to delete file " + curPath + ", error " + res) + } + }) + if (res = fs.rmdirSync(cache)) + throw Error("Failed to delete directory " + cache + ", error " + res) + } + return 0 +} +exports.deleteNpmCacheRecursivelySync = deleteNpmCacheRecursivelySync
\ No newline at end of file diff --git a/test/tap/zz-cleanup.js b/test/tap/zz-cleanup.js new file mode 100644 index 000000000..6c57f1eb4 --- /dev/null +++ b/test/tap/zz-cleanup.js @@ -0,0 +1,15 @@ +var common = require("../common-tap") +var test = require("tap").test +var fs = require("fs") + +test("cleanup", function (t) { + var res = common.deleteNpmCacheRecursivelySync() + t.equal(res, 0, "Deleted test npm cache successfully") + + // ensure cache is clean + fs.readdir(common.npm_config_cache, function(err) { + t.ok(err, "error expected") + t.equal(err.code, "ENOENT", "npm cache directory no longer exists") + t.end() + }) +}) |