diff options
Diffstat (limited to 'test/lib/commands/config.js')
-rw-r--r-- | test/lib/commands/config.js | 99 |
1 files changed, 98 insertions, 1 deletions
diff --git a/test/lib/commands/config.js b/test/lib/commands/config.js index 61e47244e..c94e0df13 100644 --- a/test/lib/commands/config.js +++ b/test/lib/commands/config.js @@ -411,6 +411,102 @@ t.test('config edit - editor exits non-0', async t => { ) }) +t.test('config fix', (t) => { + t.test('no problems', async (t) => { + const home = t.testdir({ + '.npmrc': '', + }) + + const sandbox = new Sandbox(t, { home }) + await sandbox.run('config', ['fix']) + t.equal(sandbox.output, '', 'printed nothing') + }) + + t.test('repairs all configs by default', async (t) => { + const root = t.testdir({ + global: { + npmrc: '_authtoken=notatoken\n_authToken=afaketoken', + }, + home: { + '.npmrc': '_authtoken=thisisinvalid\n_auth=beef', + }, + }) + const registry = `//registry.npmjs.org/` + + const sandbox = new Sandbox(t, { + global: join(root, 'global'), + home: join(root, 'home'), + }) + await sandbox.run('config', ['fix']) + + // global config fixes + t.match(sandbox.output, '`_authtoken` deleted from global config', + 'output has deleted global _authtoken') + t.match(sandbox.output, `\`_authToken\` renamed to \`${registry}:_authToken\` in global config`, + 'output has renamed global _authToken') + t.not(sandbox.config.get('_authtoken', 'global'), '_authtoken is not set globally') + t.not(sandbox.config.get('_authToken', 'global'), '_authToken is not set globally') + t.equal(sandbox.config.get(`${registry}:_authToken`, 'global'), 'afaketoken', + 'global _authToken was scoped') + const globalConfig = await readFile(join(root, 'global', 'npmrc'), { encoding: 'utf8' }) + t.equal(globalConfig, `${registry}:_authToken=afaketoken\n`, 'global config was written') + + // user config fixes + t.match(sandbox.output, '`_authtoken` deleted from user config', + 'output has deleted user _authtoken') + t.match(sandbox.output, `\`_auth\` renamed to \`${registry}:_auth\` in user config`, + 'output has renamed user _auth') + t.not(sandbox.config.get('_authtoken', 'user'), '_authtoken is not set in user config') + t.not(sandbox.config.get('_auth'), '_auth is not set in user config') + t.equal(sandbox.config.get(`${registry}:_auth`, 'user'), 'beef', 'user _auth was scoped') + const userConfig = await readFile(join(root, 'home', '.npmrc'), { encoding: 'utf8' }) + t.equal(userConfig, `${registry}:_auth=beef\n`, 'user config was written') + }) + + t.test('repairs only the config specified by --location if asked', async (t) => { + const root = t.testdir({ + global: { + npmrc: '_authtoken=notatoken\n_authToken=afaketoken', + }, + home: { + '.npmrc': '_authtoken=thisisinvalid\n_auth=beef', + }, + }) + const registry = `//registry.npmjs.org/` + + const sandbox = new Sandbox(t, { + global: join(root, 'global'), + home: join(root, 'home'), + }) + await sandbox.run('config', ['fix', '--location=user']) + + // global config should be untouched + t.notMatch(sandbox.output, '`_authtoken` deleted from global', + 'output has deleted global _authtoken') + t.notMatch(sandbox.output, `\`_authToken\` renamed to \`${registry}:_authToken\` in global`, + 'output has renamed global _authToken') + t.equal(sandbox.config.get('_authtoken', 'global'), 'notatoken', 'global _authtoken untouched') + t.equal(sandbox.config.get('_authToken', 'global'), 'afaketoken', 'global _authToken untouched') + t.not(sandbox.config.get(`${registry}:_authToken`, 'global'), 'global _authToken not scoped') + const globalConfig = await readFile(join(root, 'global', 'npmrc'), { encoding: 'utf8' }) + t.equal(globalConfig, '_authtoken=notatoken\n_authToken=afaketoken', + 'global config was not written') + + // user config fixes + t.match(sandbox.output, '`_authtoken` deleted from user', + 'output has deleted user _authtoken') + t.match(sandbox.output, `\`_auth\` renamed to \`${registry}:_auth\` in user`, + 'output has renamed user _auth') + t.not(sandbox.config.get('_authtoken', 'user'), '_authtoken is not set in user config') + t.not(sandbox.config.get('_auth', 'user'), '_auth is not set in user config') + t.equal(sandbox.config.get(`${registry}:_auth`, 'user'), 'beef', 'user _auth was scoped') + const userConfig = await readFile(join(root, 'home', '.npmrc'), { encoding: 'utf8' }) + t.equal(userConfig, `${registry}:_auth=beef\n`, 'user config was written') + }) + + t.end() +}) + t.test('completion', async t => { const sandbox = new Sandbox(t) @@ -423,13 +519,14 @@ t.test('completion', async t => { sandbox.reset() } - await testComp([], ['get', 'set', 'delete', 'ls', 'rm', 'edit', 'list']) + await testComp([], ['get', 'set', 'delete', 'ls', 'rm', 'edit', 'fix', 'list']) await testComp(['set', 'foo'], []) await testComp(['get'], allKeys) await testComp(['set'], allKeys) await testComp(['delete'], allKeys) await testComp(['rm'], allKeys) await testComp(['edit'], []) + await testComp(['fix'], []) await testComp(['list'], []) await testComp(['ls'], []) |