Skip to content

Commit

Permalink
chore: use chdir for mockNpm
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Jan 1, 2023
1 parent 3750f9c commit 547f0c0
Show file tree
Hide file tree
Showing 48 changed files with 314 additions and 268 deletions.
20 changes: 10 additions & 10 deletions tap-snapshots/test/lib/commands/dist-tag.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ latest-a: 1.0.0
latest: 1.0.0
`

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

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

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

exports[`test/lib/commands/dist-tag.js TAP workspaces two args -- list, .@1, ignores version spec > printed the expected output 1`] = `
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`] = `
a: 0.0.1
b: 0.5.0
latest: 1.0.0
`

exports[`test/lib/commands/dist-tag.js TAP workspaces two args -- list, cwd > printed the expected output 1`] = `
workspace-a:
latest-a: 1.0.0
latest: 1.0.0
Expand All @@ -149,9 +155,3 @@ workspace-c:
latest-c: 3.0.0
latest: 3.0.0
`

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`] = `
a: 0.0.1
b: 0.5.0
latest: 1.0.0
`
6 changes: 4 additions & 2 deletions test/bin/npm-cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const t = require('tap')
const tmock = require('../fixtures/tmock')

t.test('loading the bin calls the implementation', t => {
t.mock('../../bin/npm-cli.js', {
'../../lib/cli.js': proc => {
tmock(t, '{BIN}/npm-cli.js', {
'{LIB}/cli.js': proc => {
t.equal(proc, process, 'called implementation with process object')
t.end()
},
Expand Down
70 changes: 34 additions & 36 deletions test/bin/npx-cli.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
const t = require('tap')
const mockGlobals = require('../fixtures/mock-globals')
const npx = require.resolve('../../bin/npx-cli.js')
const cli = require.resolve('../../lib/cli.js')
const tmock = require('../fixtures/tmock')

const npm = require.resolve('../../bin/npm-cli.js')
const npx = require.resolve('../../bin/npx-cli.js')

const logs = []
t.afterEach(() => (logs.length = 0))
mockGlobals(t, { 'console.error': (...msg) => logs.push(msg) })
const mockNpx = (t, argv) => {
const logs = []
mockGlobals(t, {
'process.argv': argv,
'console.error': (...msg) => logs.push(msg),
})
tmock(t, '{BIN}/npx-cli.js', { '{LIB}/cli.js': () => {} })
return {
logs,
argv: process.argv,
}
}

t.test('npx foo -> npm exec -- foo', t => {
process.argv = ['node', npx, 'foo']
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, ['node', npm, 'exec', '--', 'foo'])
t.end()
t.test('npx foo -> npm exec -- foo', async t => {
const { argv } = mockNpx(t, ['node', npx, 'foo'])
t.strictSame(argv, ['node', npm, 'exec', '--', 'foo'])
})

t.test('npx -- foo -> npm exec -- foo', t => {
process.argv = ['node', npx, '--', 'foo']
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, ['node', npm, 'exec', '--', 'foo'])
t.end()
t.test('npx -- foo -> npm exec -- foo', async t => {
const { argv } = mockNpx(t, ['node', npx, '--', 'foo'])
t.strictSame(argv, ['node', npm, 'exec', '--', 'foo'])
})

t.test('npx -x y foo -z -> npm exec -x y -- foo -z', t => {
process.argv = ['node', npx, '-x', 'y', 'foo', '-z']
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, ['node', npm, 'exec', '-x', 'y', '--', 'foo', '-z'])
t.end()
t.test('npx -x y foo -z -> npm exec -x y -- foo -z', async t => {
const { argv } = mockNpx(t, ['node', npx, '-x', 'y', 'foo', '-z'])
t.strictSame(argv, ['node', npm, 'exec', '-x', 'y', '--', 'foo', '-z'])
})

t.test('npx --x=y --no-install foo -z -> npm exec --x=y -- foo -z', t => {
process.argv = ['node', npx, '--x=y', '--no-install', 'foo', '-z']
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, ['node', npm, 'exec', '--x=y', '--yes=false', '--', 'foo', '-z'])
t.end()
t.test('npx --x=y --no-install foo -z -> npm exec --x=y -- foo -z', async t => {
const { argv } = mockNpx(t, ['node', npx, '--x=y', '--no-install', 'foo', '-z'])
t.strictSame(argv, ['node', npm, 'exec', '--x=y', '--yes=false', '--', 'foo', '-z'])
})

t.test('transform renamed options into proper values', t => {
process.argv = ['node', npx, '-y', '--shell=bash', '-p', 'foo', '-c', 'asdf']
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, [
t.test('transform renamed options into proper values', async t => {
const { argv } = mockNpx(t, ['node', npx, '-y', '--shell=bash', '-p', 'foo', '-c', 'asdf'])
t.strictSame(argv, [
'node',
npm,
'exec',
Expand All @@ -50,12 +51,11 @@ t.test('transform renamed options into proper values', t => {
'--call',
'asdf',
])
t.end()
})

// warn if deprecated switches/options are used
t.test('use a bunch of deprecated switches and options', t => {
process.argv = [
t.test('use a bunch of deprecated switches and options', async t => {
const { argv, logs } = mockNpx(t, [
'node',
npx,
'--npm',
Expand All @@ -71,7 +71,7 @@ t.test('use a bunch of deprecated switches and options', t => {
'--ignore-existing',
'-q',
'foobar',
]
])

const expect = [
'node',
Expand All @@ -86,8 +86,7 @@ t.test('use a bunch of deprecated switches and options', t => {
'--',
'foobar',
]
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, expect)
t.strictSame(argv, expect)
t.strictSame(logs, [
['npx: the --npm argument has been removed.'],
['npx: the --node-arg argument has been removed.'],
Expand All @@ -97,5 +96,4 @@ t.test('use a bunch of deprecated switches and options', t => {
['npx: the --ignore-existing argument has been removed.'],
['See `npm help exec` for more information'],
])
t.end()
})
38 changes: 27 additions & 11 deletions test/fixtures/mock-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Hopefully it can be removed for a feature in tap in the future

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

const splitLast = (str) => {
const hasNerf = str.includes('process.env') && str.includes('//')
if (hasNerf) {
const startPosition = str.indexOf('//')
const index = str.lastIndexOf(sep, startPosition)
return [str.slice(0, index), str.slice(index + 1)]
const splitLastSep = (str) => {
let escaped = false
for (let i = str.length - 1; i >= 0; i--) {
const c = str[i]
const cp = str[i + 1]
const cn = str[i - 1]
if (!escaped && c === escapeSep && (cp == null || cp === sep)) {
escaped = true
continue
}
if (escaped && c === escapeSep && cn === sep) {
escaped = false
continue
}
if (!escaped && c === sep) {
return [
str.slice(0, i),
str.slice(i + 1).replace(new RegExp(`^${escapeSep}(.*)${escapeSep}$`), '$1'),
]
}
}
return str.split(reLastSep)
return [str]
}

// A weird getter that can look up keys on nested objects but also
Expand All @@ -29,8 +43,10 @@ const splitLast = (str) => {
const get = (obj, key, childKey = '') => {
if (has(obj, key)) {
return childKey ? get(obj[key], childKey) : obj[key]
} else if (key.includes(sep)) {
const [parentKey, prefix] = splitLast(key)
}
const split = splitLastSep(key)
if (split.length === 2) {
const [parentKey, prefix] = split
return get(
obj,
parentKey,
Expand Down Expand Up @@ -91,7 +107,7 @@ class DescriptorStack {
#isDelete = (o) => o && o.DELETE === true

constructor (key) {
const keys = splitLast(key)
const keys = splitLastSep(key)
this.#global = keys.length === 1 ? global : get(global, keys[0])
this.#valueKey = specialCaseKeys(key) || last(keys)
// If the global object doesnt return a descriptor for the key
Expand Down
Loading

0 comments on commit 547f0c0

Please sign in to comment.