Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGar <gar+gh@danger.computer>2021-02-25 02:54:50 +0300
committerRuy Adorno <ruyadorno@hotmail.com>2021-03-05 00:05:08 +0300
commit4a5dd3a5a200b3f4f7b47168497d8e03dca3a2ca (patch)
treed34a1ea229b719c3cfbdce85899ceaf67b43e7ab /test/lib/explore.js
parentb33c760cea7fe2696d35b5530abc1b455980fef1 (diff)
fix(npm) pass npm context everywhere
Instead of files randomly requiring the npm singleton, we pass it where it needs to go so that tests don't need to do so much require mocking everywhere PR-URL: https://github.com/npm/cli/pull/2772 Credit: @wraithgar Close: #2772 Reviewed-by: @ruyadorno
Diffstat (limited to 'test/lib/explore.js')
-rw-r--r--test/lib/explore.js97
1 files changed, 55 insertions, 42 deletions
diff --git a/test/lib/explore.js b/test/lib/explore.js
index 23eab1172..6f1f3bb47 100644
--- a/test/lib/explore.js
+++ b/test/lib/explore.js
@@ -46,14 +46,20 @@ const mockRunScript = ({ pkg, banner, path, event, stdio }) => {
const output = []
let ERROR_HANDLER_CALLED = null
const logs = []
-const getExplore = windows => requireInject('../../lib/explore.js', {
- '../../lib/utils/is-windows.js': windows,
- path: require('path')[windows ? 'win32' : 'posix'],
- '../../lib/utils/error-handler.js': er => {
- ERROR_HANDLER_CALLED = er
- },
- 'read-package-json-fast': mockRPJ,
- '../../lib/npm.js': {
+const getExplore = (windows) => {
+ const Explore = requireInject('../../lib/explore.js', {
+ '../../lib/utils/is-windows.js': windows,
+ path: require('path')[windows ? 'win32' : 'posix'],
+ '../../lib/utils/error-handler.js': er => {
+ ERROR_HANDLER_CALLED = er
+ },
+ 'read-package-json-fast': mockRPJ,
+ '@npmcli/run-script': mockRunScript,
+ '../../lib/utils/output.js': out => {
+ output.push(out)
+ },
+ })
+ const npm = {
dir: windows ? 'c:\\npm\\dir' : '/npm/dir',
log: {
error: (...msg) => logs.push(msg),
@@ -63,12 +69,9 @@ const getExplore = windows => requireInject('../../lib/explore.js', {
flatOptions: {
shell: 'shell-command',
},
- },
- '@npmcli/run-script': mockRunScript,
- '../../lib/utils/output.js': out => {
- output.push(out)
- },
-})
+ }
+ return new Explore(npm)
+}
const windowsExplore = getExplore(true)
const posixExplore = getExplore(false)
@@ -79,7 +82,7 @@ t.test('basic interactive', t => {
cb()
})
- t.test('windows', t => windowsExplore(['pkg'], er => {
+ t.test('windows', t => windowsExplore.exec(['pkg'], er => {
if (er)
throw er
@@ -95,9 +98,10 @@ t.test('basic interactive', t => {
t.strictSame(output, [
"\nExploring c:\\npm\\dir\\pkg\nType 'exit' or ^D when finished\n",
])
+ t.end()
}))
- t.test('posix', t => posixExplore(['pkg'], er => {
+ t.test('posix', t => posixExplore.exec(['pkg'], er => {
if (er)
throw er
@@ -113,6 +117,7 @@ t.test('basic interactive', t => {
t.strictSame(output, [
"\nExploring /npm/dir/pkg\nType 'exit' or ^D when finished\n",
])
+ t.end()
}))
t.end()
@@ -132,7 +137,7 @@ t.test('interactive tracks exit code', t => {
cb()
})
- t.test('windows', t => windowsExplore(['pkg'], er => {
+ t.test('windows', t => windowsExplore.exec(['pkg'], er => {
if (er)
throw er
@@ -149,9 +154,10 @@ t.test('interactive tracks exit code', t => {
"\nExploring c:\\npm\\dir\\pkg\nType 'exit' or ^D when finished\n",
])
t.equal(process.exitCode, 99)
+ t.end()
}))
- t.test('posix', t => posixExplore(['pkg'], er => {
+ t.test('posix', t => posixExplore.exec(['pkg'], er => {
if (er)
throw er
@@ -168,18 +174,20 @@ t.test('interactive tracks exit code', t => {
"\nExploring /npm/dir/pkg\nType 'exit' or ^D when finished\n",
])
t.equal(process.exitCode, 99)
+ t.end()
}))
t.test('posix spawn fail', t => {
RUN_SCRIPT_ERROR = Object.assign(new Error('glorb'), {
code: 33,
})
- return posixExplore(['pkg'], er => {
+ posixExplore.exec(['pkg'], er => {
t.match(er, { message: 'glorb', code: 33 })
t.strictSame(output, [
"\nExploring /npm/dir/pkg\nType 'exit' or ^D when finished\n",
])
t.equal(process.exitCode, 33)
+ t.end()
})
})
@@ -187,12 +195,13 @@ t.test('interactive tracks exit code', t => {
RUN_SCRIPT_ERROR = Object.assign(new Error('glorb'), {
code: 0,
})
- return posixExplore(['pkg'], er => {
+ posixExplore.exec(['pkg'], er => {
t.match(er, { message: 'glorb', code: 0 })
t.strictSame(output, [
"\nExploring /npm/dir/pkg\nType 'exit' or ^D when finished\n",
])
t.equal(process.exitCode, 1)
+ t.end()
})
})
@@ -200,12 +209,13 @@ t.test('interactive tracks exit code', t => {
RUN_SCRIPT_ERROR = Object.assign(new Error('command failed'), {
code: 'EPROBLEM',
})
- return posixExplore(['pkg'], er => {
+ posixExplore.exec(['pkg'], er => {
t.match(er, { message: 'command failed', code: 'EPROBLEM' })
t.strictSame(output, [
"\nExploring /npm/dir/pkg\nType 'exit' or ^D when finished\n",
])
t.equal(process.exitCode, 1)
+ t.end()
})
})
@@ -218,7 +228,7 @@ t.test('basic non-interactive', t => {
cb()
})
- t.test('windows', t => windowsExplore(['pkg', 'ls'], er => {
+ t.test('windows', t => windowsExplore.exec(['pkg', 'ls'], er => {
if (er)
throw er
@@ -232,9 +242,10 @@ t.test('basic non-interactive', t => {
RUN_SCRIPT_EXEC: 'ls',
})
t.strictSame(output, [])
+ t.end()
}))
- t.test('posix', t => posixExplore(['pkg', 'ls'], er => {
+ t.test('posix', t => posixExplore.exec(['pkg', 'ls'], er => {
if (er)
throw er
@@ -248,6 +259,7 @@ t.test('basic non-interactive', t => {
RUN_SCRIPT_EXEC: 'ls',
})
t.strictSame(output, [])
+ t.end()
}))
t.end()
@@ -272,7 +284,7 @@ t.test('signal fails non-interactive', t => {
cb()
})
- t.test('windows', t => windowsExplore(['pkg', 'ls'], er => {
+ t.test('windows', t => windowsExplore.exec(['pkg', 'ls'], er => {
t.match(er, {
message: 'command failed',
signal: 'SIGPROBLEM',
@@ -286,9 +298,10 @@ t.test('signal fails non-interactive', t => {
RUN_SCRIPT_EXEC: 'ls',
})
t.strictSame(output, [])
+ t.end()
}))
- t.test('posix', t => posixExplore(['pkg', 'ls'], er => {
+ t.test('posix', t => posixExplore.exec(['pkg', 'ls'], er => {
t.match(er, {
message: 'command failed',
signal: 'SIGPROBLEM',
@@ -302,6 +315,7 @@ t.test('signal fails non-interactive', t => {
RUN_SCRIPT_EXEC: 'ls',
})
t.strictSame(output, [])
+ t.end()
}))
t.end()
@@ -322,29 +336,28 @@ t.test('usage if no pkg provided', t => {
]
t.plan(noPkg.length)
for (const args of noPkg) {
- t.test(JSON.stringify(args), t => posixExplore(args, er => {
- t.equal(er, 'npm explore <pkg> [ -- <command>]')
- t.strictSame({
- ERROR_HANDLER_CALLED: null,
- RPJ_CALLED,
- RUN_SCRIPT_EXEC,
- }, {
- ERROR_HANDLER_CALLED: null,
- RPJ_CALLED: '/npm/dir/pkg/package.json',
- RUN_SCRIPT_EXEC: 'ls',
+ t.test(JSON.stringify(args), t => {
+ posixExplore.exec(args, er => {
+ t.equal(er, 'npm explore <pkg> [ -- <command>]')
+ t.strictSame({
+ ERROR_HANDLER_CALLED: null,
+ RPJ_CALLED,
+ RUN_SCRIPT_EXEC,
+ }, {
+ ERROR_HANDLER_CALLED: null,
+ RPJ_CALLED: '/npm/dir/pkg/package.json',
+ RUN_SCRIPT_EXEC: 'ls',
+ })
+ t.end()
})
- }))
+ })
}
})
t.test('pkg not installed', t => {
RPJ_ERROR = new Error('plurple')
- t.plan(2)
-
- posixExplore(['pkg', 'ls'], er => {
- if (er)
- throw er
+ posixExplore.exec(['pkg', 'ls'], er => {
t.strictSame({
ERROR_HANDLER_CALLED,
RPJ_CALLED,
@@ -355,9 +368,9 @@ t.test('pkg not installed', t => {
RUN_SCRIPT_EXEC: 'ls',
})
t.strictSame(output, [])
- }).catch(er => {
t.match(er, { message: 'plurple' })
t.match(logs, [['explore', `It doesn't look like pkg is installed.`]])
+ t.end()
logs.length = 0
})
})