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-03-11 03:11:34 +0300
committerRuy Adorno <ruyadorno@hotmail.com>2021-03-18 22:54:36 +0300
commit41facf6435ced4e416d74111d9c3ff00ee19ab7d (patch)
tree1a6644f756670ee6fad67a9a59d135dc0f3e00ef /test/lib/help-search.js
parenta8d0751e4b7c7d8b808c8a49f288fc7272f729b0 (diff)
feat(help): refactor npm help/help-search
Lots of dead code removed thanks to streamlining of logic. `npm help` `npm <command>` and `npm help-search` are all now separated concerns, handling their own use cases. `help` calls `help-search` as a last resort, but `npm <command>` no longer tries to wind its way through to `help-search` just to get the basic npm usage displayed. The `did you mean` output has been expanded. It now always suggests top level commands, scripts, and bins, and suggests them in the way they should be called. PR-URL: https://github.com/npm/cli/pull/2859 Credit: @wraithgar Close: #2859 Reviewed-by: @ruyadorno
Diffstat (limited to 'test/lib/help-search.js')
-rw-r--r--test/lib/help-search.js72
1 files changed, 16 insertions, 56 deletions
diff --git a/test/lib/help-search.js b/test/lib/help-search.js
index cbb3947d3..f046ba3d2 100644
--- a/test/lib/help-search.js
+++ b/test/lib/help-search.js
@@ -2,33 +2,29 @@ const { test } = require('tap')
const { join } = require('path')
const requireInject = require('require-inject')
const ansicolors = require('ansicolors')
-const mockNpm = require('../fixtures/mock-npm')
const OUTPUT = []
const output = (msg) => {
OUTPUT.push(msg)
}
-let npmHelpArgs = null
-let npmHelpErr = null
-const config = {
+const config = new Map(Object.entries({
long: false,
-}
-const npm = mockNpm({
+}))
+const npmHelpErr = null
+const npm = {
color: false,
config,
+ flatOptions: {
+ long: false,
+ },
+ usage: 'npm test usage',
commands: {
help: (args, cb) => {
- npmHelpArgs = args
return cb(npmHelpErr)
},
},
output,
-})
-
-let npmUsageArg = null
-const npmUsage = (npm, arg) => {
- npmUsageArg = arg
}
let globRoot = null
@@ -47,7 +43,6 @@ const glob = (p, cb) =>
cb(null, Object.keys(globDir).map((file) => join(globRoot, file)))
const HelpSearch = requireInject('../../lib/help-search.js', {
- '../../lib/utils/npm-usage.js': npmUsage,
glob,
})
const helpSearch = new HelpSearch(npm)
@@ -63,8 +58,7 @@ test('npm help-search', t => {
if (err)
throw err
- t.match(OUTPUT, /Top hits for/, 'outputs results')
- t.match(OUTPUT, /Did you mean this\?\n\s+exec/, 'matched command, so suggest it')
+ t.match(OUTPUT, /Top hits for "exec"/, 'outputs results')
t.end()
})
})
@@ -86,46 +80,12 @@ test('npm help-search multiple terms', t => {
})
})
-test('npm help-search single result prints full section', t => {
- globRoot = t.testdir(globDir)
- t.teardown(() => {
- OUTPUT.length = 0
- npmHelpArgs = null
- globRoot = null
- })
-
- return helpSearch.exec(['does not exist in'], (err) => {
- if (err)
- throw err
-
- t.strictSame(npmHelpArgs, ['npm-install'], 'identified the correct man page and called help with it')
- t.end()
- })
-})
-
-test('npm help-search single result propagates error', t => {
- globRoot = t.testdir(globDir)
- npmHelpErr = new Error('help broke')
- t.teardown(() => {
- OUTPUT.length = 0
- npmHelpArgs = null
- npmHelpErr = null
- globRoot = null
- })
-
- return helpSearch.exec(['does not exist in'], (err) => {
- t.strictSame(npmHelpArgs, ['npm-install'], 'identified the correct man page and called help with it')
- t.match(err, /help broke/, 'propagated the error from help')
- t.end()
- })
-})
-
test('npm help-search long output', t => {
globRoot = t.testdir(globDir)
- config.long = true
+ config.set('long', true)
t.teardown(() => {
OUTPUT.length = 0
- config.long = false
+ config.set('long', false)
globRoot = null
})
@@ -140,11 +100,11 @@ test('npm help-search long output', t => {
test('npm help-search long output with color', t => {
globRoot = t.testdir(globDir)
- config.long = true
+ config.set('long', true)
npm.color = true
t.teardown(() => {
OUTPUT.length = 0
- config.long = false
+ config.set('long', false)
npm.color = false
globRoot = null
})
@@ -161,7 +121,8 @@ test('npm help-search long output with color', t => {
test('npm help-search no args', t => {
return helpSearch.exec([], (err) => {
- t.match(err, /npm help-search/, 'throws usage')
+ t.notOk(err)
+ t.match(OUTPUT, /npm help-search/, 'outputs usage')
t.end()
})
})
@@ -170,7 +131,6 @@ test('npm help-search no matches', t => {
globRoot = t.testdir(globDir)
t.teardown(() => {
OUTPUT.length = 0
- npmUsageArg = null
globRoot = null
})
@@ -178,7 +138,7 @@ test('npm help-search no matches', t => {
if (err)
throw err
- t.equal(npmUsageArg, false, 'called npmUsage for no matches')
+ t.match(OUTPUT, /No matches/)
t.end()
})
})