]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/test/tap/correct-mkdir.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / test / tap / correct-mkdir.js
1 var test = require('tap').test
2 var assert = require('assert')
3 var path = require('path')
4 var requireInject = require('require-inject')
5 var cache_dir = path.resolve(__dirname, 'correct-mkdir')
6
7 test('correct-mkdir: no race conditions', function (t) {
8   var mock_fs = {}
9   var did_hook = false
10   mock_fs.stat = function (path, cb) {
11     if (path === cache_dir) {
12       // Return a non-matching owner
13       cb(null, {
14         uid: +process.uid + 1,
15         isDirectory: function () {
16           return true
17         }
18       })
19       if (!did_hook) {
20         did_hook = true
21         doHook()
22       }
23     } else {
24       assert.ok(false, 'Unhandled stat path: ' + path)
25     }
26   }
27   var chown_in_progress = 0
28   var mock_chownr = function (path, uid, gid, cb) {
29     ++chown_in_progress
30     process.nextTick(function () {
31       --chown_in_progress
32       cb(null)
33     })
34   }
35   var mocks = {
36     'graceful-fs': mock_fs,
37     'chownr': mock_chownr
38   }
39   var correctMkdir = requireInject('../../lib/utils/correct-mkdir.js', mocks)
40
41   var calls_in_progress = 3
42   function handleCallFinish () {
43     t.equal(chown_in_progress, 0, 'should not return while chown still in progress')
44     if (!--calls_in_progress) {
45       t.end()
46     }
47   }
48   function doHook () {
49     // This is fired during the first correctMkdir call, after the stat has finished
50     // but before the chownr has finished
51     // Buggy old code will fail and return a cached value before initial call is done
52     correctMkdir(cache_dir, handleCallFinish)
53   }
54   // Initial call
55   correctMkdir(cache_dir, handleCallFinish)
56   // Immediate call again in case of race condition there
57   correctMkdir(cache_dir, handleCallFinish)
58 })
59
60 test('correct-mkdir: ignore ENOENTs from chownr', function (t) {
61   var mock_fs = {}
62   mock_fs.stat = function (path, cb) {
63     if (path === cache_dir) {
64       cb(null, {
65         isDirectory: function () {
66           return true
67         }
68       })
69     } else {
70       assert.ok(false, 'Unhandled stat path: ' + path)
71     }
72   }
73   var mock_chownr = function (path, uid, gid, cb) {
74     cb({code: 'ENOENT'})
75   }
76   var mocks = {
77     'graceful-fs': mock_fs,
78     'chownr': mock_chownr
79   }
80   var correctMkdir = requireInject('../../lib/utils/correct-mkdir.js', mocks)
81
82   function handleCallFinish (err) {
83     t.ifErr(err, 'chownr\'s ENOENT errors were ignored')
84     t.end()
85   }
86   correctMkdir(cache_dir, handleCallFinish)
87 })
88
89 // NEED TO RUN LAST
90
91 // These test checks that Windows users are protected by crashes related to
92 // unexpectedly having a UID/GID other than 0 if a user happens to add these
93 // variables to their environment. There are assumptions in correct-mkdir
94 // that special-case Windows by checking on UID-related things.
95 test('correct-mkdir: SUDO_UID and SUDO_GID non-Windows', function (t) {
96   process.env.SUDO_UID = 999
97   process.env.SUDO_GID = 999
98   process.getuid = function () { return 0 }
99   process.getgid = function () { return 0 }
100   var mock_fs = {}
101   mock_fs.stat = function (path, cb) {
102     if (path === cache_dir) {
103       cb(null, {
104         uid: 0,
105         isDirectory: function () {
106           return true
107         }
108       })
109     } else {
110       assert.ok(false, 'Unhandled stat path: ' + path)
111     }
112   }
113   var mock_chownr = function (path, uid, gid, cb) {
114     t.is(uid, +process.env.SUDO_UID, 'using the environment\'s UID')
115     t.is(gid, +process.env.SUDO_GID, 'using the environment\'s GID')
116     cb(null, {})
117   }
118   var mocks = {
119     'graceful-fs': mock_fs,
120     'chownr': mock_chownr
121   }
122   var correctMkdir = requireInject('../../lib/utils/correct-mkdir.js', mocks)
123
124   function handleCallFinish () {
125     t.end()
126   }
127   correctMkdir(cache_dir, handleCallFinish)
128 })
129
130 test('correct-mkdir: SUDO_UID and SUDO_GID Windows', function (t) {
131   process.env.SUDO_UID = 999
132   process.env.SUDO_GID = 999
133   delete process.getuid
134   delete process.getgid
135   var mock_fs = {}
136   mock_fs.stat = function (path, cb) {
137     if (path === cache_dir) {
138       cb(null, {
139         uid: 0,
140         isDirectory: function () {
141           return true
142         }
143       })
144     } else {
145       assert.ok(false, 'Unhandled stat path: ' + path)
146     }
147   }
148   var mock_chownr = function (path, uid, gid, cb) {
149     t.fail('chownr should not be called at all on Windows')
150     cb('nope')
151   }
152   var mocks = {
153     'graceful-fs': mock_fs,
154     'chownr': mock_chownr
155   }
156   var correctMkdir = requireInject('../../lib/utils/correct-mkdir.js', mocks)
157
158   function handleCallFinish (err) {
159     t.ifErr(err, 'chownr was not called because Windows')
160     t.end()
161   }
162   correctMkdir(cache_dir, handleCallFinish)
163 })