/
git.cwd-owner.js
75 lines (70 loc) · 1.78 KB
/
git.cwd-owner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict'
const t = require('tap')
process.getuid = () => 0
process.getgid = () => 0
const requireInject = require('require-inject')
const fs = require('fs')
const { _cwdOwner: cwdOwner } = requireInject('../lib/util/git.js', {
fs: Object.assign({}, fs, {
lstat: (path, cb) => {
if (path === '/var/root/.npm/_cacache/bleep/boop') {
return process.nextTick(() => cb(null, { uid: 0, gid: 0 }))
}
if (path === '/home/user/.npm/_cacache/bleep/boop') {
return process.nextTick(() => cb(null, { uid: 69, gid: 420 }))
}
fs.lstat(path, cb)
},
stat: (path, cb) => {
if (path === '/var/root/.npm/_cacache/bleep/boop') {
return process.nextTick(() => cb(null, { uid: 0, gid: 0 }))
}
if (path === '/home/user/.npm/_cacache/bleep/boop') {
return process.nextTick(() => cb(null, { uid: 69, gid: 420 }))
}
fs.stat(path, cb)
}
})
})
t.test('running in root-owned folder, but with non-root uid/gid opts', t => {
const gitOpts = {
cwd: '/var/root/.npm/_cacache/bleep/boop'
}
const opts = {
uid: 69,
gid: 420
}
return cwdOwner(gitOpts, opts).then(() => {
t.strictSame(gitOpts, {
cwd: '/var/root/.npm/_cacache/bleep/boop',
uid: 0,
gid: 0
})
})
})
t.test('running in non-root-owned folder', t => {
const gitOpts = {
cwd: '/home/user/.npm/_cacache/bleep/boop'
}
const opts = {
uid: 12,
gid: 34
}
return cwdOwner(gitOpts, opts).then(() => {
t.strictSame(gitOpts, {
cwd: '/home/user/.npm/_cacache/bleep/boop',
uid: 69,
gid: 420
})
})
})
t.test('running without a cwd', t => {
const gitOpts = {}
const opts = {
uid: 12,
gid: 34
}
return cwdOwner(gitOpts, opts).then(() => {
t.strictSame(gitOpts, {})
})
})