From 7c89e74469520f80a0922987c761ebd808d8608c Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 15 Mar 2021 16:59:33 -0700 Subject: update lib/*.js to use new config structures --- test/lib/completion.js | 13 ++++++----- test/lib/config.js | 59 +++++++++++++++++++++++++++++--------------------- test/lib/npm.js | 18 +++++++++------ test/lib/publish.js | 23 +++++++++++++++----- 4 files changed, 70 insertions(+), 43 deletions(-) (limited to 'test') diff --git a/test/lib/completion.js b/test/lib/completion.js index 708f13825..c6ef901a7 100644 --- a/test/lib/completion.js +++ b/test/lib/completion.js @@ -63,11 +63,14 @@ const cmdList = { plumbing: [], } +// only include a subset so that the snapshots aren't huge and +// don't change when we add/remove config definitions. +const definitions = require('../../lib/utils/config/definitions.js') const config = { - types: { - global: Boolean, - browser: [null, Boolean, String], - registry: [null, String], + definitions: { + global: definitions.global, + browser: definitions.browser, + registry: definitions.registry, }, shorthands: { reg: ['--registry'], @@ -80,7 +83,7 @@ const deref = (cmd) => { const Completion = requireInject('../../lib/completion.js', { '../../lib/utils/cmd-list.js': cmdList, - '../../lib/utils/config.js': config, + '../../lib/utils/config/index.js': config, '../../lib/utils/deref-command.js': deref, '../../lib/utils/is-windows-shell.js': false, }) diff --git a/test/lib/config.js b/test/lib/config.js index 3aeb29f8d..14cd81617 100644 --- a/test/lib/config.js +++ b/test/lib/config.js @@ -1,4 +1,5 @@ const t = require('tap') + const requireInject = require('require-inject') const { EventEmitter } = require('events') @@ -22,12 +23,21 @@ const redactCwd = (path) => { t.cleanSnapshot = (str) => redactCwd(str) let result = '' -const types = { - 'init-author-name': String, - 'init-version': String, - 'init.author.name': String, - 'init.version': String, -} + +const configDefs = require('../../lib/utils/config') +const definitions = Object.entries(configDefs.definitions) + .filter(([key, def]) => { + return [ + 'init-author-name', + 'init.author.name', + 'init-version', + 'init.version', + ].includes(key) + }).reduce((defs, [key, def]) => { + defs[key] = def + return defs + }, {}) + const defaults = { 'init-author-name': '', 'init-version': '1.0.0', @@ -35,7 +45,7 @@ const defaults = { 'init.version': '1.0.0', } -const flatOptions = { +const cliConfig = { editor: 'vi', json: false, long: false, @@ -43,7 +53,6 @@ const flatOptions = { } const npm = { - flatOptions, log: { info: () => null, enableProgress: () => null, @@ -53,10 +62,10 @@ const npm = { data: new Map(Object.entries({ default: { data: defaults, source: 'default values' }, global: { data: {}, source: '/etc/npmrc' }, - cli: { data: flatOptions, source: 'command line options' }, + cli: { data: cliConfig, source: 'command line options' }, })), get (key) { - return flatOptions[key] + return cliConfig[key] }, validate () { return true @@ -70,7 +79,7 @@ const npm = { const usageUtil = () => 'usage instructions' const mocks = { - '../../lib/utils/config.js': { defaults, types }, + '../../lib/utils/config/index.js': { defaults, definitions }, '../../lib/utils/usage.js': usageUtil, } @@ -110,13 +119,13 @@ t.test('config list overrides', t => { }, source: '~/.npmrc', }) - flatOptions['init.author.name'] = 'Bar' + cliConfig['init.author.name'] = 'Bar' npm.config.find = () => 'cli' result = '' t.teardown(() => { result = '' npm.config.data.delete('user') - delete flatOptions['init.author.name'] + delete cliConfig['init.author.name'] delete npm.config.find }) @@ -129,12 +138,12 @@ t.test('config list overrides', t => { t.test('config list --long', t => { t.plan(2) - npm.config.find = key => key in flatOptions ? 'cli' : 'default' - flatOptions.long = true + npm.config.find = key => key in cliConfig ? 'cli' : 'default' + cliConfig.long = true result = '' t.teardown(() => { delete npm.config.find - flatOptions.long = false + cliConfig.long = false result = '' }) @@ -147,7 +156,7 @@ t.test('config list --long', t => { t.test('config list --json', t => { t.plan(2) - flatOptions.json = true + cliConfig.json = true result = '' npm.config.list = [{ '//private-reg.npmjs.org/:_authThoken': 'f00ba1', @@ -158,7 +167,7 @@ t.test('config list --json', t => { t.teardown(() => { delete npm.config.list - flatOptions.json = false + cliConfig.json = false npm.config.get = npmConfigGet result = '' }) @@ -246,13 +255,13 @@ t.test('config delete key --global', t => { t.equal(where, 'global', 'should save global config post-delete') } - flatOptions.global = true + cliConfig.global = true config.exec(['delete', 'foo'], (err) => { t.ifError(err, 'npm config delete key --global') }) t.teardown(() => { - flatOptions.global = false + cliConfig.global = false delete npm.config.delete delete npm.config.save }) @@ -401,13 +410,13 @@ t.test('config set key --global', t => { t.equal(where, 'global', 'should save global config') } - flatOptions.global = true + cliConfig.global = true config.exec(['set', 'foo', 'bar'], (err) => { t.ifError(err, 'npm config set key --global') }) t.teardown(() => { - flatOptions.global = false + cliConfig.global = false delete npm.config.set delete npm.config.save }) @@ -555,7 +564,7 @@ sign-git-commit=true` t.test('config edit --global', t => { t.plan(6) - flatOptions.global = true + cliConfig.global = true const npmrc = 'init.author.name=Foo' npm.config.data.set('global', { source: '/etc/npmrc', @@ -595,7 +604,7 @@ t.test('config edit --global', t => { }) t.teardown(() => { - flatOptions.global = false + cliConfig.global = false npm.config.data.delete('user') delete npm.config.save }) @@ -612,7 +621,7 @@ t.test('completion', t => { testComp(['npm', 'config'], ['get', 'set', 'delete', 'ls', 'rm', 'edit', 'list']) testComp(['npm', 'config', 'set', 'foo'], []) - const possibleConfigKeys = [...Object.keys(types)] + const possibleConfigKeys = [...Object.keys(definitions)] testComp(['npm', 'config', 'get'], possibleConfigKeys) testComp(['npm', 'config', 'set'], possibleConfigKeys) testComp(['npm', 'config', 'delete'], possibleConfigKeys) diff --git a/test/lib/npm.js b/test/lib/npm.js index 87cbea8f2..dfb3cca64 100644 --- a/test/lib/npm.js +++ b/test/lib/npm.js @@ -42,7 +42,7 @@ const npmlog = require('npmlog') const npmPath = resolve(__dirname, '..', '..') const Config = require('@npmcli/config') -const { types, defaults, shorthands } = require('../../lib/utils/config.js') +const { definitions, shorthands } = require('../../lib/utils/config') const freshConfig = (opts = {}) => { for (const env of Object.keys(process.env).filter(e => /^npm_/.test(e))) delete process.env[env] @@ -50,8 +50,7 @@ const freshConfig = (opts = {}) => { process.env.npm_config_cache = CACHE npm.config = new Config({ - types, - defaults, + definitions, shorthands, npmPath, log: npmlog, @@ -160,7 +159,7 @@ t.test('npm.load', t => { npm.load(third) t.equal(thirdCalled, true, 'third callbback got called') t.match(logs, [ - ['timing', 'npm:load', /Completed in [0-9]+ms/], + ['timing', 'npm:load', /Completed in [0-9.]+ms/], ]) logs.length = 0 @@ -288,6 +287,11 @@ t.test('npm.load', t => { t.equal(npm.config.get('scope'), '@foo', 'added the @ sign to scope') t.match(logs.filter(l => l[0] !== 'timing' || !/^config:/.test(l[1])), [ + [ + 'timing', + 'npm:load:whichnode', + /Completed in [0-9.]+ms/, + ], [ 'verbose', 'node symlink', @@ -296,7 +300,7 @@ t.test('npm.load', t => { [ 'timing', 'npm:load', - /Completed in [0-9]+ms/, + /Completed in [0-9.]+ms/, ], ]) logs.length = 0 @@ -328,12 +332,12 @@ t.test('npm.load', t => { [ 'timing', 'command:config', - /Completed in [0-9]+ms/, + /Completed in [0-9.]+ms/, ], [ 'timing', 'command:get', - /Completed in [0-9]+ms/, + /Completed in [0-9.]+ms/, ], ]) t.same(consoleLogs, [['scope=@foo\n\u2010not-a-dash=undefined']]) diff --git a/test/lib/publish.js b/test/lib/publish.js index d1a1cd630..f61377b54 100644 --- a/test/lib/publish.js +++ b/test/lib/publish.js @@ -11,7 +11,11 @@ const log = require('npmlog') log.level = 'silent' // mock config -const {defaults} = require('../../lib/utils/config.js') +const {definitions} = require('../../lib/utils/config') +const defaults = Object.entries(definitions).reduce((defaults, [key, def]) => { + defaults[key] = def.default + return defaults +}, {}) const config = defaults @@ -488,11 +492,12 @@ t.test('able to publish after if encountered multiple configs', t => { }, null, 2), }) - const configList = [ - { ...defaults, tag }, - { ...defaults, registry: `https://other.registry`, tag: 'some-tag' }, - defaults, - ] + const configList = [defaults] + configList.unshift(Object.assign(Object.create(configList[0]), { + registry: `https://other.registry`, + tag: 'some-tag', + })) + configList.unshift(Object.assign(Object.create(configList[0]), { tag })) const Publish = requireInject('../../lib/publish.js', { libnpmpublish: { @@ -502,7 +507,13 @@ t.test('able to publish after if encountered multiple configs', t => { }, }) const publish = new Publish({ + // what would be flattened by the configList created above + flatOptions: { + defaultTag: 'better-tag', + registry: 'https://other.registry', + }, config: { + get: key => configList[0][key], list: configList, getCredentialsByURI: (uri) => { t.same(uri, registry, 'gets credentials for expected registry') -- cgit v1.2.3