From 8c5ca2f516f5ac87f3bbd7f1fd95c0b283a21f14 Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 1 Feb 2021 12:34:05 -0800 Subject: [PATCH] test: Add test for npm-usage.js, and fix 'npm --long' output --- lib/access.js | 3 +- lib/utils/npm-usage.js | 58 +- .../test-lib-utils-npm-usage.js-TAP.test.js | 495 ++++++++++++++++++ test/lib/utils/npm-usage.js | 126 +++++ 4 files changed, 647 insertions(+), 35 deletions(-) create mode 100644 tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js create mode 100644 test/lib/utils/npm-usage.js diff --git a/lib/access.js b/lib/access.js index 68c6e628d0187..8a372d90cb55c 100644 --- a/lib/access.js +++ b/lib/access.js @@ -8,7 +8,6 @@ const output = require('./utils/output.js') const otplease = require('./utils/otplease.js') const usageUtil = require('./utils/usage.js') const getIdentity = require('./utils/get-identity.js') -const { prefix } = npm const usage = usageUtil( 'npm access', @@ -165,7 +164,7 @@ const getPackage = async (name, requireScope) => { return name.trim() else { try { - const pkg = await readPackageJson(path.resolve(prefix, 'package.json')) + const pkg = await readPackageJson(path.resolve(npm.prefix, 'package.json')) name = pkg.name } catch (err) { if (err.code === 'ENOENT') { diff --git a/lib/utils/npm-usage.js b/lib/utils/npm-usage.js index 3ecf068ed6494..d4261f79dcb71 100644 --- a/lib/utils/npm-usage.js +++ b/lib/utils/npm-usage.js @@ -6,6 +6,8 @@ const { cmdList } = require('./cmd-list') module.exports = (valid = true) => { npm.config.set('loglevel', 'silent') + const usesBrowser = npm.config.get('viewer') === 'browser' + ? ' (in a browser)' : '' npm.log.level = 'silent' output(` Usage: npm @@ -16,8 +18,8 @@ npm test run this project's tests npm run run the script named npm -h quick help on npm -l display usage info for all commands -npm help search for help on (in a browser) -npm help npm more involved overview (in a browser) +npm help search for help on ${usesBrowser} +npm help npm more involved overview${usesBrowser} All commands: ${npm.config.get('long') ? usages() : ('\n ' + wrap(cmdList))} @@ -40,44 +42,34 @@ npm@${npm.version} ${dirname(dirname(__dirname))} } const wrap = (arr) => { - var out = [''] - var l = 0 - var line + const out = [''] - line = process.stdout.columns - if (!line) - line = 60 - else - line = Math.min(60, Math.max(line - 16, 24)) + const line = !process.stdout.columns ? 60 + : Math.min(60, Math.max(process.stdout.columns - 16, 24)) - arr.sort(function (a, b) { - return a < b ? -1 : 1 - }) - .forEach(function (c) { - if (out[l].length + c.length + 2 < line) - out[l] += ', ' + c - else { - out[l++] += ',' - out[l] = c - } - }) + let l = 0 + for (const c of arr.sort((a, b) => a < b ? -1 : 1)) { + if (out[l].length + c.length + 2 < line) + out[l] += ', ' + c + else { + out[l++] += ',' + out[l] = c + } + } return out.join('\n ').substr(2) } const usages = () => { // return a string of : - var maxLen = 0 - return cmdList.reduce(function (set, c) { - set.push([c, require(`./${npm.deref(c)}.js`).usage || '']) + let maxLen = 0 + return cmdList.reduce((set, c) => { + set.push([c, require(`../${npm.deref(c)}.js`).usage || + /* istanbul ignore next - all commands should have usage */ '']) maxLen = Math.max(maxLen, c.length) return set - }, []).sort((a, b) => { - return a[0].localeCompare(b[0]) - }).map(function (item) { - var c = item[0] - var usage = item[1] - return '\n ' + - c + (new Array(maxLen - c.length + 2).join(' ')) + - (usage.split('\n').join('\n' + (new Array(maxLen + 6).join(' ')))) - }).join('\n') + }, []) + .sort((a, b) => a[0].localeCompare(b[0])) + .map(([c, usage]) => `\n ${c}${' '.repeat(maxLen - c.length + 1)}${ + (usage.split('\n').join('\n' + ' '.repeat(maxLen + 5)))}`) + .join('\n') } diff --git a/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js b/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js new file mode 100644 index 0000000000000..72c2c81583a5b --- /dev/null +++ b/tap-snapshots/test-lib-utils-npm-usage.js-TAP.test.js @@ -0,0 +1,495 @@ +/* IMPORTANT + * This snapshot file is auto-generated, but designed for humans. + * It should be checked into source control and tracked carefully. + * Re-generate by setting TAP_SNAPSHOT=1 and running tests. + * Make sure to inspect the output below. Do not ignore changes! + */ +'use strict' +exports[`test/lib/utils/npm-usage.js TAP basic usage > must match snapshot 1`] = ` + +Usage: npm + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +All commands: + + access, adduser, audit, bin, bugs, cache, ci, completion, + config, dedupe, deprecate, diff, dist-tag, docs, doctor, + edit, exec, explain, explore, find-dupes, fund, get, help, + hook, init, install, install-ci-test, install-test, link, + ll, login, logout, ls, org, outdated, owner, pack, ping, + prefix, profile, prune, publish, rebuild, repo, restart, + root, run-script, search, set, set-script, shrinkwrap, star, + stars, start, stop, team, test, token, uninstall, unpublish, + unstar, update, version, view, whoami + +Specify configs in the ini-formatted file: + /some/config/file/.npmrc +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@7.99.9999 {BASEDIR} + +` + +exports[`test/lib/utils/npm-usage.js TAP did you mean? > must match snapshot 1`] = ` + +Usage: npm + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +All commands: + + access, adduser, audit, bin, bugs, cache, ci, completion, + config, dedupe, deprecate, diff, dist-tag, docs, doctor, + edit, exec, explain, explore, find-dupes, fund, get, help, + hook, init, install, install-ci-test, install-test, link, + ll, login, logout, ls, org, outdated, owner, pack, ping, + prefix, profile, prune, publish, rebuild, repo, restart, + root, run-script, search, set, set-script, shrinkwrap, star, + stars, start, stop, team, test, token, uninstall, unpublish, + unstar, update, version, view, whoami + +Specify configs in the ini-formatted file: + /some/config/file/.npmrc +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@7.99.9999 {BASEDIR} + +` + +exports[`test/lib/utils/npm-usage.js TAP did you mean? > must match snapshot 2`] = ` + +Did you mean one of these? + install + uninstall +` + +exports[`test/lib/utils/npm-usage.js TAP set process.stdout.columns columns=0 > must match snapshot 1`] = ` + +Usage: npm + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +All commands: + + access, adduser, audit, bin, bugs, cache, ci, completion, + config, dedupe, deprecate, diff, dist-tag, docs, doctor, + edit, exec, explain, explore, find-dupes, fund, get, help, + hook, init, install, install-ci-test, install-test, link, + ll, login, logout, ls, org, outdated, owner, pack, ping, + prefix, profile, prune, publish, rebuild, repo, restart, + root, run-script, search, set, set-script, shrinkwrap, star, + stars, start, stop, team, test, token, uninstall, unpublish, + unstar, update, version, view, whoami + +Specify configs in the ini-formatted file: + /some/config/file/.npmrc +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@7.99.9999 {BASEDIR} + +` + +exports[`test/lib/utils/npm-usage.js TAP set process.stdout.columns columns=90 > must match snapshot 1`] = ` + +Usage: npm + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +All commands: + + access, adduser, audit, bin, bugs, cache, ci, completion, + config, dedupe, deprecate, diff, dist-tag, docs, doctor, + edit, exec, explain, explore, find-dupes, fund, get, help, + hook, init, install, install-ci-test, install-test, link, + ll, login, logout, ls, org, outdated, owner, pack, ping, + prefix, profile, prune, publish, rebuild, repo, restart, + root, run-script, search, set, set-script, shrinkwrap, star, + stars, start, stop, team, test, token, uninstall, unpublish, + unstar, update, version, view, whoami + +Specify configs in the ini-formatted file: + /some/config/file/.npmrc +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@7.99.9999 {BASEDIR} + +` + +exports[`test/lib/utils/npm-usage.js TAP with browser > must match snapshot 1`] = ` + +Usage: npm + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on (in a browser) +npm help npm more involved overview (in a browser) + +All commands: + + access, adduser, audit, bin, bugs, cache, ci, completion, + config, dedupe, deprecate, diff, dist-tag, docs, doctor, + edit, exec, explain, explore, find-dupes, fund, get, help, + hook, init, install, install-ci-test, install-test, link, + ll, login, logout, ls, org, outdated, owner, pack, ping, + prefix, profile, prune, publish, rebuild, repo, restart, + root, run-script, search, set, set-script, shrinkwrap, star, + stars, start, stop, team, test, token, uninstall, unpublish, + unstar, update, version, view, whoami + +Specify configs in the ini-formatted file: + /some/config/file/.npmrc +or on the command line via: npm --key=value + +More configuration info: npm help config +Configuration fields: npm help 7 config + +npm@7.99.9999 {BASEDIR} + +` + +exports[`test/lib/utils/npm-usage.js TAP with long > must match snapshot 1`] = ` + +Usage: npm + +npm install install all the dependencies in your project +npm install add the dependency to your project +npm test run this project's tests +npm run run the script named +npm -h quick help on +npm -l display usage info for all commands +npm help search for help on +npm help npm more involved overview + +All commands: + + access npm access public [] + npm access restricted [] + npm access grant [] + npm access revoke [] + npm access 2fa-required [] + npm access 2fa-not-required [] + npm access ls-packages [||] + npm access ls-collaborators [ []] + npm access edit [] + + adduser npm adduser [--registry=url] [--scope=@orgname] [--always-auth] + + aliases: login, add-user + + audit npm audit [--json] [--production] + npm audit fix [--force|--package-lock-only|--dry-run|--production|--only=(dev|prod)] + + bin npm bin [-g] + + bugs npm bugs [] + + alias: issues + + cache npm cache add + npm cache add + npm cache add + npm cache add + npm cache add @ + npm cache clean + npm cache verify + + ci npm ci + + aliases: clean-install, ic, install-clean, isntall-clean + + completion source <(npm completion) + + config npm config set = [= ...] + npm config get [ [ ...]] + npm config delete [ ...] + npm config list [--json] + npm config edit + npm set = [= ...] + npm get [ [ ...]] + + alias: c + + dedupe npm dedupe + + alias: ddp + + deprecate npm deprecate [@] + + diff npm diff [...] + npm diff --diff= [...] + npm diff --diff= [--diff=] [...] + npm diff --diff= [--diff=] [...] + npm diff [--diff-ignore-all-space] [--diff-name-only] [...] [...] + + dist-tag npm dist-tag add @ [] + npm dist-tag rm + npm dist-tag ls [] + + alias: dist-tags + + docs npm docs [ [ ...]] + + alias: home + + doctor npm doctor + + edit npm edit [/...] + + exec Run a command from a local or remote npm package. + + npm exec -- [@] [args...] + npm exec --package=[@] -- [args...] + npm exec -c ' [args...]' + npm exec --package=foo -c ' [args...]' + + npx [@] [args...] + npx -p [@] [args...] + npx -c ' [args...]' + npx -p [@] -c ' [args...]' + Run without --call or positional args to open interactive subshell + + + alias: x + common options: + --package= (may be specified multiple times) + -p is a shorthand for --package only when using npx executable + -c --call= (may not be mixed with positional arguments) + + explain npm explain + + alias: why + + explore npm explore [ -- ] + + find-dupes npm find-dupes + + fund npm fund + + common options: npm fund [--json] [--browser] [--unicode] [[<@scope>/] [--which=] + + get npm get [ ...] (See \`npm config\`) + + help npm help [] + + alias: hlep + + hook npm hook add [--type=] + npm hook ls [pkg] + npm hook rm + npm hook update + + init + npm init [--force|-f|--yes|-y|--scope] + npm init <@scope> (same as \`npx <@scope>/create\`) + npm init [<@scope>/] (same as \`npx [<@scope>/]create-\`) + + aliases: create, innit + + install npm install (with no args, in package dir) + npm install [<@scope>/] + npm install [<@scope>/]@ + npm install [<@scope>/]@ + npm install [<@scope>/]@ + npm install @npm: + npm install + npm install + npm install + npm install + npm install / + + aliases: i, in, ins, inst, insta, instal, isnt, isnta, isntal, add + common options: [--save-prod|--save-dev|--save-optional|--save-peer] [--save-exact] [--no-save] + + install-ci-test npm install-ci-test [args] + Same args as \`npm ci\` + + alias: cit + + install-test npm install-test [args] + Same args as \`npm install\` + + alias: it + + link npm link (in package dir) + npm link [<@scope>/][@] + + alias: ln + + ll npm ls [[<@scope>/] ...] + + alias: list + + login npm adduser [--registry=url] [--scope=@orgname] [--always-auth] + + aliases: login, add-user + + logout npm logout [--registry=] [--scope=<@scope>] + + ls npm ls [[<@scope>/] ...] + + alias: list + + org npm org set orgname username [developer | admin | owner] + npm org rm orgname username + npm org ls orgname [] + + outdated npm outdated [[<@scope>/] ...] + + owner npm owner add [<@scope>/] + npm owner rm [<@scope>/] + npm owner ls [<@scope>/] + + alias: author + + pack npm pack [[<@scope>/]...] [--dry-run] + + ping npm ping + ping registry + + prefix npm prefix [-g] + + profile npm profile disable-2fa + + + common options: npm profile get [] + + + prune npm prune [[<@scope>/]...] [--production] + + publish npm publish [] [--tag ] [--access ] [--dry-run] + + Publishes '.' if no argument supplied + Sets tag \`latest\` if no --tag specified + + rebuild npm rebuild [[<@scope>/][@] ...] + + alias: rb + + repo npm repo [ [ ...]] + + restart npm restart [-- ] + + root npm root [-g] + + run-script npm run-script [-- ] + + aliases: run, rum, urn + + search npm search [-l|--long] [--json] [--parseable] [--no-description] [search terms ...] + + aliases: s, se, find + + set npm set = [= ...] (See \`npm config\`) + + set-script npm set-script [