1var test = require('tap').test 2var path = require('path') 3var fs = require('graceful-fs') 4var crypto = require('crypto') 5var mkdirp = require('mkdirp') 6var npm = require('../../') 7var locker = require('../../lib/utils/locker.js') 8var lock = locker.lock 9var unlock = locker.unlock 10 11const common = require('../common-tap.js') 12var pkg = common.pkg 13var cache = path.join(pkg, '/cache') 14var tmp = path.join(pkg, '/tmp') 15var nm = path.join(pkg, '/node_modules') 16 17test('setup', function (t) { 18 mkdirp.sync(cache) 19 mkdirp.sync(tmp) 20 t.end() 21}) 22 23test('locking file puts lock in correct place', function (t) { 24 npm.load({cache: cache, tmpdir: tmp}, function (er) { 25 t.ifError(er, 'npm bootstrapped OK') 26 27 var n = 'correct' 28 var c = n.replace(/[^a-zA-Z0-9]+/g, '-').replace(/^-+|-+$/g, '') 29 var p = path.resolve(nm, n) 30 var h = crypto.createHash('sha1').update(p).digest('hex') 31 var l = c.substr(0, 24) + '-' + h.substr(0, 16) + '.lock' 32 var v = path.join(cache, '_locks', l) 33 34 lock(nm, n, function (er) { 35 t.ifError(er, 'locked path') 36 37 fs.exists(v, function (found) { 38 t.ok(found, 'lock found OK') 39 40 unlock(nm, n, function (er) { 41 t.ifError(er, 'unlocked path') 42 43 fs.exists(v, function (found) { 44 t.notOk(found, 'lock deleted OK') 45 t.end() 46 }) 47 }) 48 }) 49 }) 50 }) 51}) 52 53test('unlocking out of order errors out', function (t) { 54 npm.load({cache: cache, tmpdir: tmp}, function (er) { 55 t.ifError(er, 'npm bootstrapped OK') 56 57 var n = 'busted' 58 var c = n.replace(/[^a-zA-Z0-9]+/g, '-').replace(/^-+|-+$/g, '') 59 var p = path.resolve(nm, n) 60 var h = crypto.createHash('sha1').update(p).digest('hex') 61 var l = c.substr(0, 24) + '-' + h.substr(0, 16) + '.lock' 62 var v = path.join(cache, '_locks', l) 63 64 fs.exists(v, function (found) { 65 t.notOk(found, 'no lock to unlock') 66 67 t.throws(function () { 68 unlock(nm, n, function () { 69 t.fail("shouldn't get here") 70 t.end() 71 }) 72 }, 'blew up as expected') 73 74 t.end() 75 }) 76 }) 77}) 78