1 var test = require("tap").test
2 , path = require("path")
3 , fs = require("graceful-fs")
4 , crypto = require("crypto")
5 , rimraf = require("rimraf")
6 , osenv = require("osenv")
7 , mkdirp = require("mkdirp")
8 , npm = require("../../")
9 , locker = require("../../lib/utils/locker.js")
11 , unlock = locker.unlock
13 var pkg = path.join(__dirname, "/locker")
14 , cache = path.join(pkg, "/cache")
15 , tmp = path.join(pkg, "/tmp")
16 , nm = path.join(pkg, "/node_modules")
19 process.chdir(osenv.tmpdir())
23 test("setup", function (t) {
30 test("locking file puts lock in correct place", function (t) {
31 npm.load({cache: cache, tmpdir: tmp}, function (er) {
32 t.ifError(er, "npm bootstrapped OK")
35 , c = n.replace(/[^a-zA-Z0-9]+/g, "-").replace(/^-+|-+$/g, "")
36 , p = path.resolve(nm, n)
37 , h = crypto.createHash("sha1").update(p).digest("hex")
38 , l = c.substr(0, 24)+"-"+h.substr(0, 16)+".lock"
39 , v = path.join(cache, "_locks", l)
41 lock(nm, n, function (er) {
42 t.ifError(er, "locked path")
44 fs.exists(v, function (found) {
45 t.ok(found, "lock found OK")
47 unlock(nm, n, function (er) {
48 t.ifError(er, "unlocked path")
50 fs.exists(v, function (found) {
51 t.notOk(found, "lock deleted OK")
60 test("unlocking out of order errors out", function (t) {
61 npm.load({cache: cache, tmpdir: tmp}, function (er) {
62 t.ifError(er, "npm bootstrapped OK")
65 , c = n.replace(/[^a-zA-Z0-9]+/g, "-").replace(/^-+|-+$/g, "")
66 , p = path.resolve(nm, n)
67 , h = crypto.createHash("sha1").update(p).digest("hex")
68 , l = c.substr(0, 24)+"-"+h.substr(0, 16)+".lock"
69 , v = path.join(cache, "_locks", l)
71 fs.exists(v, function (found) {
72 t.notOk(found, "no lock to unlock")
74 t.throws(function () {
75 unlock(nm, n, function () {
76 t.fail("shouldn't get here")
79 }, "blew up as expected")
86 test("cleanup", function (t) {