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')
7 test('correct-mkdir: no race conditions', function (t) {
10 mock_fs.stat = function (path, cb) {
11 if (path === cache_dir) {
12 // Return a non-matching owner
14 uid: +process.uid + 1,
15 isDirectory: function () {
24 assert.ok(false, 'Unhandled stat path: ' + path)
27 var chown_in_progress = 0
28 var mock_chownr = function (path, uid, gid, cb) {
30 process.nextTick(function () {
36 'graceful-fs': mock_fs,
39 var correctMkdir = requireInject('../../lib/utils/correct-mkdir.js', mocks)
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) {
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)
55 correctMkdir(cache_dir, handleCallFinish)
56 // Immediate call again in case of race condition there
57 correctMkdir(cache_dir, handleCallFinish)
60 test('correct-mkdir: ignore ENOENTs from chownr', function (t) {
62 mock_fs.stat = function (path, cb) {
63 if (path === cache_dir) {
65 isDirectory: function () {
70 assert.ok(false, 'Unhandled stat path: ' + path)
73 var mock_chownr = function (path, uid, gid, cb) {
77 'graceful-fs': mock_fs,
80 var correctMkdir = requireInject('../../lib/utils/correct-mkdir.js', mocks)
82 function handleCallFinish (err) {
83 t.ifErr(err, 'chownr\'s ENOENT errors were ignored')
86 correctMkdir(cache_dir, handleCallFinish)
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 }
101 mock_fs.stat = function (path, cb) {
102 if (path === cache_dir) {
105 isDirectory: function () {
110 assert.ok(false, 'Unhandled stat path: ' + path)
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')
119 'graceful-fs': mock_fs,
120 'chownr': mock_chownr
122 var correctMkdir = requireInject('../../lib/utils/correct-mkdir.js', mocks)
124 function handleCallFinish () {
127 correctMkdir(cache_dir, handleCallFinish)
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
136 mock_fs.stat = function (path, cb) {
137 if (path === cache_dir) {
140 isDirectory: function () {
145 assert.ok(false, 'Unhandled stat path: ' + path)
148 var mock_chownr = function (path, uid, gid, cb) {
149 t.fail('chownr should not be called at all on Windows')
153 'graceful-fs': mock_fs,
154 'chownr': mock_chownr
156 var correctMkdir = requireInject('../../lib/utils/correct-mkdir.js', mocks)
158 function handleCallFinish (err) {
159 t.ifErr(err, 'chownr was not called because Windows')
162 correctMkdir(cache_dir, handleCallFinish)