diff options
author | isaacs <i@izs.me> | 2020-12-16 03:32:32 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-12-18 22:38:41 +0300 |
commit | a9b8bf2634c627fbb16ca3a6bb2c2f1058c3e586 (patch) | |
tree | 8e51be0a095d76b489a2af43f1d8cc781699be40 /test/lib/config.js | |
parent | a92d310b7e9e4c48b08f52785c2e3a6d52a82ad7 (diff) |
Support multiple set/get/deletes in npm configisaacs/config-set-multi
While digging into #2300, I realized it would be a lot easier if we
could do this:
npm config set email=me@example.com _auth=xxxx
and avoid the whole issue of what gets set first.
Also, why not let `npm config get foo bar baz` return just the keys
specified?
Also updates the docs, including the statement that `npm config set foo`
with no value sets it to `true`, when as far as I can tell, that has
never been the case.
PR-URL: https://github.com/npm/cli/pull/2362
Credit: @isaacs
Close: #2362
Reviewed-by: @nlf
Diffstat (limited to 'test/lib/config.js')
-rw-r--r-- | test/lib/config.js | 90 |
1 files changed, 89 insertions, 1 deletions
diff --git a/test/lib/config.js b/test/lib/config.js index 74cd530c6..1ccd823ac 100644 --- a/test/lib/config.js +++ b/test/lib/config.js @@ -212,6 +212,33 @@ t.test('config delete key', t => { }) }) +t.test('config delete multiple key', t => { + t.plan(6) + + const expect = [ + 'foo', + 'bar', + ] + + npm.config.delete = (key, where) => { + t.equal(key, expect.shift(), 'should delete expected keyword') + t.equal(where, 'user', 'should delete key from user config by default') + } + + npm.config.save = where => { + t.equal(where, 'user', 'should save user config post-delete') + } + + config(['delete', 'foo', 'bar'], (err) => { + t.ifError(err, 'npm config delete keys') + }) + + t.teardown(() => { + delete npm.config.delete + delete npm.config.save + }) +}) + t.test('config delete key --global', t => { t.plan(4) @@ -293,12 +320,43 @@ t.test('config set key=val', t => { }) }) +t.test('config set multiple keys', t => { + t.plan(11) + + const expect = [ + ['foo', 'bar'], + ['bar', 'baz'], + ['asdf', ''], + ] + const args = ['foo', 'bar', 'bar=baz', 'asdf'] + + npm.config.set = (key, val, where) => { + const [expectKey, expectVal] = expect.shift() + t.equal(key, expectKey, 'should set expected key to user config') + t.equal(val, expectVal, 'should set expected value to user config') + t.equal(where, 'user', 'should set key/val in user config by default') + } + + npm.config.save = where => { + t.equal(where, 'user', 'should save user config') + } + + config(['set', ...args], (err) => { + t.ifError(err, 'npm config set key') + }) + + t.teardown(() => { + delete npm.config.set + delete npm.config.save + }) +}) + t.test('config set key to empty value', t => { t.plan(5) npm.config.set = (key, val, where) => { t.equal(key, 'foo', 'should set expected key to user config') - t.equal(val, '', 'should set empty value to user config') + t.equal(val, '', 'should set "" to user config') t.equal(where, 'user', 'should set key/val in user config by default') } @@ -403,6 +461,36 @@ t.test('config get key', t => { }) }) +t.test('config get multiple keys', t => { + t.plan(4) + + const expect = [ + 'foo', + 'bar', + ] + + const npmConfigGet = npm.config.get + npm.config.get = (key) => { + t.equal(key, expect.shift(), 'should use expected key') + return 'asdf' + } + + npm.config.save = where => { + throw new Error('should not save') + } + + config(['get', 'foo', 'bar'], (err) => { + t.ifError(err, 'npm config get multiple keys') + t.equal(result, 'foo=asdf\nbar=asdf') + }) + + t.teardown(() => { + result = '' + npm.config.get = npmConfigGet + delete npm.config.save + }) +}) + t.test('config get private key', t => { config(['get', '//private-reg.npmjs.org/:_authThoken'], (err) => { t.match( |