]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/test/tap/locker.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / test / tap / locker.js
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")
10   , lock = locker.lock
11   , unlock = locker.unlock
12
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")
17
18 function cleanup () {
19   process.chdir(osenv.tmpdir())
20   rimraf.sync(pkg)
21 }
22
23 test("setup", function (t) {
24   cleanup()
25   mkdirp.sync(cache)
26   mkdirp.sync(tmp)
27   t.end()
28 })
29
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")
33
34     var n = "correct"
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)
40
41     lock(nm, n, function (er) {
42       t.ifError(er, "locked path")
43
44       fs.exists(v, function (found) {
45         t.ok(found, "lock found OK")
46
47         unlock(nm, n, function (er) {
48           t.ifError(er, "unlocked path")
49
50           fs.exists(v, function (found) {
51             t.notOk(found, "lock deleted OK")
52             t.end()
53           })
54         })
55       })
56     })
57   })
58 })
59
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")
63
64     var n = "busted"
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)
70
71     fs.exists(v, function (found) {
72       t.notOk(found, "no lock to unlock")
73
74       t.throws(function () {
75         unlock(nm, n, function () {
76           t.fail("shouldn't get here")
77           t.end()
78         })
79       }, "blew up as expected")
80
81       t.end()
82     })
83   })
84 })
85
86 test("cleanup", function (t) {
87   cleanup()
88   t.end()
89 })