Skip to content

Commit 122938c

Browse files
committed
chore: use chdir for mockNpm
1 parent f963180 commit 122938c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+314
-268
lines changed

tap-snapshots/test/lib/commands/dist-tag.js.test.cjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ latest-a: 1.0.0
9090
latest: 1.0.0
9191
`
9292

93-
exports[`test/lib/commands/dist-tag.js TAP workspaces one arg -- . > printed the expected output 1`] = `
93+
exports[`test/lib/commands/dist-tag.js TAP workspaces one arg -- .@1, ignores version spec > printed the expected output 1`] = `
9494
workspace-a:
9595
latest-a: 1.0.0
9696
latest: 1.0.0
@@ -102,7 +102,7 @@ latest-c: 3.0.0
102102
latest: 3.0.0
103103
`
104104

105-
exports[`test/lib/commands/dist-tag.js TAP workspaces one arg -- .@1, ignores version spec > printed the expected output 1`] = `
105+
exports[`test/lib/commands/dist-tag.js TAP workspaces one arg -- cwd > printed the expected output 1`] = `
106106
workspace-a:
107107
latest-a: 1.0.0
108108
latest: 1.0.0
@@ -126,7 +126,7 @@ latest-c: 3.0.0
126126
latest: 3.0.0
127127
`
128128

129-
exports[`test/lib/commands/dist-tag.js TAP workspaces two args -- list, . > printed the expected output 1`] = `
129+
exports[`test/lib/commands/dist-tag.js TAP workspaces two args -- list, .@1, ignores version spec > printed the expected output 1`] = `
130130
workspace-a:
131131
latest-a: 1.0.0
132132
latest: 1.0.0
@@ -138,7 +138,13 @@ latest-c: 3.0.0
138138
latest: 3.0.0
139139
`
140140

141-
exports[`test/lib/commands/dist-tag.js TAP workspaces two args -- list, .@1, ignores version spec > printed the expected output 1`] = `
141+
exports[`test/lib/commands/dist-tag.js TAP workspaces two args -- list, @scoped/pkg, logs a warning and ignores workspaces > printed the expected output 1`] = `
142+
a: 0.0.1
143+
b: 0.5.0
144+
latest: 1.0.0
145+
`
146+
147+
exports[`test/lib/commands/dist-tag.js TAP workspaces two args -- list, cwd > printed the expected output 1`] = `
142148
workspace-a:
143149
latest-a: 1.0.0
144150
latest: 1.0.0
@@ -149,9 +155,3 @@ workspace-c:
149155
latest-c: 3.0.0
150156
latest: 3.0.0
151157
`
152-
153-
exports[`test/lib/commands/dist-tag.js TAP workspaces two args -- list, @scoped/pkg, logs a warning and ignores workspaces > printed the expected output 1`] = `
154-
a: 0.0.1
155-
b: 0.5.0
156-
latest: 1.0.0
157-
`

test/bin/npm-cli.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const t = require('tap')
2+
const tmock = require('../fixtures/tmock')
3+
24
t.test('loading the bin calls the implementation', t => {
3-
t.mock('../../bin/npm-cli.js', {
4-
'../../lib/cli.js': proc => {
5+
tmock(t, '{BIN}/npm-cli.js', {
6+
'{LIB}/cli.js': proc => {
57
t.equal(proc, process, 'called implementation with process object')
68
t.end()
79
},

test/bin/npx-cli.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
11
const t = require('tap')
22
const mockGlobals = require('../fixtures/mock-globals')
3-
const npx = require.resolve('../../bin/npx-cli.js')
4-
const cli = require.resolve('../../lib/cli.js')
3+
const tmock = require('../fixtures/tmock')
4+
55
const npm = require.resolve('../../bin/npm-cli.js')
6+
const npx = require.resolve('../../bin/npx-cli.js')
67

7-
const logs = []
8-
t.afterEach(() => (logs.length = 0))
9-
mockGlobals(t, { 'console.error': (...msg) => logs.push(msg) })
8+
const mockNpx = (t, argv) => {
9+
const logs = []
10+
mockGlobals(t, {
11+
'process.argv': argv,
12+
'console.error': (...msg) => logs.push(msg),
13+
})
14+
tmock(t, '{BIN}/npx-cli.js', { '{LIB}/cli.js': () => {} })
15+
return {
16+
logs,
17+
argv: process.argv,
18+
}
19+
}
1020

11-
t.test('npx foo -> npm exec -- foo', t => {
12-
process.argv = ['node', npx, 'foo']
13-
t.mock(npx, { [cli]: () => {} })
14-
t.strictSame(process.argv, ['node', npm, 'exec', '--', 'foo'])
15-
t.end()
21+
t.test('npx foo -> npm exec -- foo', async t => {
22+
const { argv } = mockNpx(t, ['node', npx, 'foo'])
23+
t.strictSame(argv, ['node', npm, 'exec', '--', 'foo'])
1624
})
1725

18-
t.test('npx -- foo -> npm exec -- foo', t => {
19-
process.argv = ['node', npx, '--', 'foo']
20-
t.mock(npx, { [cli]: () => {} })
21-
t.strictSame(process.argv, ['node', npm, 'exec', '--', 'foo'])
22-
t.end()
26+
t.test('npx -- foo -> npm exec -- foo', async t => {
27+
const { argv } = mockNpx(t, ['node', npx, '--', 'foo'])
28+
t.strictSame(argv, ['node', npm, 'exec', '--', 'foo'])
2329
})
2430

25-
t.test('npx -x y foo -z -> npm exec -x y -- foo -z', t => {
26-
process.argv = ['node', npx, '-x', 'y', 'foo', '-z']
27-
t.mock(npx, { [cli]: () => {} })
28-
t.strictSame(process.argv, ['node', npm, 'exec', '-x', 'y', '--', 'foo', '-z'])
29-
t.end()
31+
t.test('npx -x y foo -z -> npm exec -x y -- foo -z', async t => {
32+
const { argv } = mockNpx(t, ['node', npx, '-x', 'y', 'foo', '-z'])
33+
t.strictSame(argv, ['node', npm, 'exec', '-x', 'y', '--', 'foo', '-z'])
3034
})
3135

32-
t.test('npx --x=y --no-install foo -z -> npm exec --x=y -- foo -z', t => {
33-
process.argv = ['node', npx, '--x=y', '--no-install', 'foo', '-z']
34-
t.mock(npx, { [cli]: () => {} })
35-
t.strictSame(process.argv, ['node', npm, 'exec', '--x=y', '--yes=false', '--', 'foo', '-z'])
36-
t.end()
36+
t.test('npx --x=y --no-install foo -z -> npm exec --x=y -- foo -z', async t => {
37+
const { argv } = mockNpx(t, ['node', npx, '--x=y', '--no-install', 'foo', '-z'])
38+
t.strictSame(argv, ['node', npm, 'exec', '--x=y', '--yes=false', '--', 'foo', '-z'])
3739
})
3840

39-
t.test('transform renamed options into proper values', t => {
40-
process.argv = ['node', npx, '-y', '--shell=bash', '-p', 'foo', '-c', 'asdf']
41-
t.mock(npx, { [cli]: () => {} })
42-
t.strictSame(process.argv, [
41+
t.test('transform renamed options into proper values', async t => {
42+
const { argv } = mockNpx(t, ['node', npx, '-y', '--shell=bash', '-p', 'foo', '-c', 'asdf'])
43+
t.strictSame(argv, [
4344
'node',
4445
npm,
4546
'exec',
@@ -50,12 +51,11 @@ t.test('transform renamed options into proper values', t => {
5051
'--call',
5152
'asdf',
5253
])
53-
t.end()
5454
})
5555

5656
// warn if deprecated switches/options are used
57-
t.test('use a bunch of deprecated switches and options', t => {
58-
process.argv = [
57+
t.test('use a bunch of deprecated switches and options', async t => {
58+
const { argv, logs } = mockNpx(t, [
5959
'node',
6060
npx,
6161
'--npm',
@@ -71,7 +71,7 @@ t.test('use a bunch of deprecated switches and options', t => {
7171
'--ignore-existing',
7272
'-q',
7373
'foobar',
74-
]
74+
])
7575

7676
const expect = [
7777
'node',
@@ -86,8 +86,7 @@ t.test('use a bunch of deprecated switches and options', t => {
8686
'--',
8787
'foobar',
8888
]
89-
t.mock(npx, { [cli]: () => {} })
90-
t.strictSame(process.argv, expect)
89+
t.strictSame(argv, expect)
9190
t.strictSame(logs, [
9291
['npx: the --npm argument has been removed.'],
9392
['npx: the --node-arg argument has been removed.'],
@@ -97,5 +96,4 @@ t.test('use a bunch of deprecated switches and options', t => {
9796
['npx: the --ignore-existing argument has been removed.'],
9897
['See `npm help exec` for more information'],
9998
])
100-
t.end()
10199
})

test/fixtures/mock-globals.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Hopefully it can be removed for a feature in tap in the future
55

66
const sep = '.'
7-
const reLastSep = new RegExp(`\\${sep}(?=[^${sep}]+$)`)
7+
const escapeSep = '"'
88
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k)
99
const opd = (o, k) => Object.getOwnPropertyDescriptor(o, k)
1010
const po = (o) => Object.getPrototypeOf(o)
@@ -13,14 +13,28 @@ const last = (arr) => arr[arr.length - 1]
1313
const dupes = (arr) => arr.filter((k, i) => arr.indexOf(k) !== i)
1414
const dupesStartsWith = (arr) => arr.filter((k1) => arr.some((k2) => k2.startsWith(k1 + sep)))
1515

16-
const splitLast = (str) => {
17-
const hasNerf = str.includes('process.env') && str.includes('//')
18-
if (hasNerf) {
19-
const startPosition = str.indexOf('//')
20-
const index = str.lastIndexOf(sep, startPosition)
21-
return [str.slice(0, index), str.slice(index + 1)]
16+
const splitLastSep = (str) => {
17+
let escaped = false
18+
for (let i = str.length - 1; i >= 0; i--) {
19+
const c = str[i]
20+
const cp = str[i + 1]
21+
const cn = str[i - 1]
22+
if (!escaped && c === escapeSep && (cp == null || cp === sep)) {
23+
escaped = true
24+
continue
25+
}
26+
if (escaped && c === escapeSep && cn === sep) {
27+
escaped = false
28+
continue
29+
}
30+
if (!escaped && c === sep) {
31+
return [
32+
str.slice(0, i),
33+
str.slice(i + 1).replace(new RegExp(`^${escapeSep}(.*)${escapeSep}$`), '$1'),
34+
]
35+
}
2236
}
23-
return str.split(reLastSep)
37+
return [str]
2438
}
2539

2640
// A weird getter that can look up keys on nested objects but also
@@ -29,8 +43,10 @@ const splitLast = (str) => {
2943
const get = (obj, key, childKey = '') => {
3044
if (has(obj, key)) {
3145
return childKey ? get(obj[key], childKey) : obj[key]
32-
} else if (key.includes(sep)) {
33-
const [parentKey, prefix] = splitLast(key)
46+
}
47+
const split = splitLastSep(key)
48+
if (split.length === 2) {
49+
const [parentKey, prefix] = split
3450
return get(
3551
obj,
3652
parentKey,
@@ -91,7 +107,7 @@ class DescriptorStack {
91107
#isDelete = (o) => o && o.DELETE === true
92108

93109
constructor (key) {
94-
const keys = splitLast(key)
110+
const keys = splitLastSep(key)
95111
this.#global = keys.length === 1 ? global : get(global, keys[0])
96112
this.#valueKey = specialCaseKeys(key) || last(keys)
97113
// If the global object doesnt return a descriptor for the key

0 commit comments

Comments
 (0)