|
1 | 1 | const t = require('tap')
|
| 2 | +const requireInject = require('require-inject') |
| 3 | + |
| 4 | +// load up the posix and windows versions of the reserver |
| 5 | +if (process.platform === 'win32') |
| 6 | + process.env.TESTING_TAR_FAKE_PLATFORM = 'posix' |
2 | 7 | const { reserve } = require('../lib/path-reservations.js')()
|
| 8 | +delete process.env.TESTING_TAR_FAKE_PLATFORM |
| 9 | +if (process.platform !== 'win32') |
| 10 | + process.env.TESTING_TAR_FAKE_PLATFORM = 'win32' |
| 11 | +const { reserve: winReserve } = requireInject('../lib/path-reservations.js')() |
3 | 12 |
|
4 | 13 | t.test('basic race', t => {
|
5 | 14 | // simulate the race conditions we care about
|
@@ -54,3 +63,87 @@ t.test('basic race', t => {
|
54 | 63 | t.notOk(reserve(['a/b'], dir2), 'dir2 waits')
|
55 | 64 | t.notOk(reserve(['a/b/x'], dir3), 'dir3 waits')
|
56 | 65 | })
|
| 66 | + |
| 67 | +t.test('unicode shenanigans', t => { |
| 68 | + const e1 = Buffer.from([0xc3, 0xa9]) |
| 69 | + const e2 = Buffer.from([0x65, 0xcc, 0x81]) |
| 70 | + let didCafe1 = false |
| 71 | + const cafe1 = done => { |
| 72 | + t.equal(didCafe1, false, 'did cafe1 only once') |
| 73 | + t.equal(didCafe2, false, 'did cafe1 before cafe2') |
| 74 | + didCafe1 = true |
| 75 | + setTimeout(done) |
| 76 | + } |
| 77 | + let didCafe2 = false |
| 78 | + const cafe2 = done => { |
| 79 | + t.equal(didCafe1, true, 'did cafe1 before cafe2') |
| 80 | + t.equal(didCafe2, false, 'did cafe2 only once') |
| 81 | + didCafe2 = true |
| 82 | + done() |
| 83 | + t.end() |
| 84 | + } |
| 85 | + const cafePath1 = `c/a/f/${e1}` |
| 86 | + const cafePath2 = `c/a/f/${e2}` |
| 87 | + t.ok(reserve([cafePath1], cafe1)) |
| 88 | + t.notOk(reserve([cafePath2], cafe2)) |
| 89 | +}) |
| 90 | + |
| 91 | +t.test('absolute paths and trailing slash', t => { |
| 92 | + let calledA1 = false |
| 93 | + let calledA2 = false |
| 94 | + const a1 = done => { |
| 95 | + t.equal(calledA1, false, 'called a1 only once') |
| 96 | + t.equal(calledA2, false, 'called a1 before 2') |
| 97 | + calledA1 = true |
| 98 | + setTimeout(done) |
| 99 | + } |
| 100 | + const a2 = done => { |
| 101 | + t.equal(calledA1, true, 'called a1 before 2') |
| 102 | + t.equal(calledA2, false, 'called a2 only once') |
| 103 | + calledA2 = true |
| 104 | + done() |
| 105 | + if (calledR2) |
| 106 | + t.end() |
| 107 | + } |
| 108 | + let calledR1 = false |
| 109 | + let calledR2 = false |
| 110 | + const r1 = done => { |
| 111 | + t.equal(calledR1, false, 'called r1 only once') |
| 112 | + t.equal(calledR2, false, 'called r1 before 2') |
| 113 | + calledR1 = true |
| 114 | + setTimeout(done) |
| 115 | + } |
| 116 | + const r2 = done => { |
| 117 | + t.equal(calledR1, true, 'called r1 before 2') |
| 118 | + t.equal(calledR2, false, 'called r1 only once') |
| 119 | + calledR2 = true |
| 120 | + done() |
| 121 | + if (calledA2) |
| 122 | + t.end() |
| 123 | + } |
| 124 | + t.ok(reserve(['/p/a/t/h'], a1)) |
| 125 | + t.notOk(reserve(['/p/a/t/h/'], a2)) |
| 126 | + t.ok(reserve(['p/a/t/h'], r1)) |
| 127 | + t.notOk(reserve(['p/a/t/h/'], r2)) |
| 128 | +}) |
| 129 | + |
| 130 | +t.test('on windows, everything collides with everything', t => { |
| 131 | + const reserve = winReserve |
| 132 | + let called1 = false |
| 133 | + let called2 = false |
| 134 | + const f1 = done => { |
| 135 | + t.equal(called1, false, 'only call 1 once') |
| 136 | + t.equal(called2, false, 'call 1 before 2') |
| 137 | + called1 = true |
| 138 | + setTimeout(done) |
| 139 | + } |
| 140 | + const f2 = done => { |
| 141 | + t.equal(called1, true, 'call 1 before 2') |
| 142 | + t.equal(called2, false, 'only call 2 once') |
| 143 | + called2 = true |
| 144 | + done() |
| 145 | + t.end() |
| 146 | + } |
| 147 | + t.equal(reserve(['some/path'], f1), true) |
| 148 | + t.equal(reserve(['other/path'], f2), false) |
| 149 | +}) |
0 commit comments