Files
pnpm/config/commands/test/managingAuthSettings.test.ts
Zoltan Kochan 3033bee430 refactor(config): split Config interface into settings + runtime context (#11197)
* refactor(config): split Config interface into settings + runtime context

Create ConfigContext for runtime state (hooks, finders, workspace graph,
CLI metadata) and keep Config for user-facing settings only. Functions
use Pick<Config, ...> & Pick<ConfigContext, ...> to express which fields
they need from each interface.

getConfig() now returns { config, context, warnings }. The CLI wrapper
returns { config, context } and spreads both when calling command
handlers (to be refactored to separate params in follow-up PRs).

Closes #11195

* fix: address review feedback

- Initialize cliOptions on pnpmConfig so context.cliOptions is never undefined
- Move rootProjectManifestDir assignment before ignoreLocalSettings guard
- Add allProjectsGraph to INTERNAL_CONFIG_KEYS

* refactor: remove INTERNAL_CONFIG_KEYS from configToRecord

configToRecord now accepts Config and ConfigContext separately, so
context fields are never in scope. Only auth-related Config fields
(authConfig, authInfos, sslConfigs) need filtering.

* refactor: eliminate INTERNAL_CONFIG_KEYS from configToRecord

configToRecord now receives the clean Config object and explicitlySetKeys
separately (via opts.config and opts.context), so context fields are
never in scope. main.ts passes the original split objects alongside
the spread for command handlers that need them.

* fix: spelling

* fix: import sorting

* fix: --config.xxx nconf overrides conflicting with --config CLI flag

When `pnpm add` registers `config: Boolean`, nopt captures
--config.xxx=yyy as the --config flag value instead of treating it
as a nconf-style config override. Fix by extracting --config.xxx args
before nopt parsing and re-parsing them separately.

Also rename the split config/context properties on the command opts
object to _config/_context to avoid clashing with the --config CLI option.
2026-04-04 23:44:25 +02:00

261 lines
6.7 KiB
TypeScript

import path from 'node:path'
import { config } from '@pnpm/config.commands'
import { tempDir } from '@pnpm/prepare'
import { createConfigCommandOpts } from './utils/index.js'
import { type ConfigFilesData, readConfigFiles, writeConfigFiles } from './utils/index.js'
describe.each(
[
'_auth',
'_authToken',
'_password',
'username',
'registry',
'//registry.npmjs.org/:_authToken',
]
)('auth settings are written to the rc file directly', (key) => {
describe('global (without --json)', () => {
it(`should set ${key}`, async () => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')
const initConfig = {
globalRc: {},
globalYaml: undefined,
localRc: undefined,
localYaml: undefined,
} satisfies ConfigFilesData
writeConfigFiles(configDir, tmp, initConfig)
await config.handler(createConfigCommandOpts({
dir: tmp,
cliOptions: {},
configDir,
global: true,
authConfig: {},
}), ['set', `${key}=123`])
expect(readConfigFiles(configDir, tmp)).toEqual({
...initConfig,
globalRc: { [key]: '123' },
})
})
it(`should delete ${key}`, async () => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')
const initConfig = {
globalRc: { [key]: 'some-value' },
globalYaml: undefined,
localRc: undefined,
localYaml: undefined,
} satisfies ConfigFilesData
writeConfigFiles(configDir, tmp, initConfig)
await config.handler(createConfigCommandOpts({
dir: tmp,
cliOptions: {},
configDir,
global: true,
authConfig: {},
}), ['delete', key])
expect(readConfigFiles(configDir, tmp)).toEqual({
...initConfig,
globalRc: {},
})
})
})
describe('global (with --json)', () => {
it(`should set ${key}`, async () => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')
const initConfig = {
globalRc: {},
globalYaml: undefined,
localRc: undefined,
localYaml: undefined,
} satisfies ConfigFilesData
writeConfigFiles(configDir, tmp, initConfig)
await config.handler(createConfigCommandOpts({
json: true,
dir: tmp,
cliOptions: {},
configDir,
global: true,
authConfig: {},
}), ['set', key, '"123"'])
expect(readConfigFiles(configDir, tmp)).toEqual({
...initConfig,
globalRc: { [key]: '123' },
})
})
it(`should delete ${key}`, async () => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')
const initConfig = {
globalRc: { [key]: 'some-value' },
globalYaml: undefined,
localRc: undefined,
localYaml: undefined,
} satisfies ConfigFilesData
writeConfigFiles(configDir, tmp, initConfig)
await config.handler(createConfigCommandOpts({
json: true,
dir: tmp,
cliOptions: {},
configDir,
global: true,
authConfig: {},
}), ['delete', key])
expect(readConfigFiles(configDir, tmp)).toEqual({
...initConfig,
globalRc: {},
})
})
})
})
describe.each(
[
'@foo:registry',
]
)('scoped auth settings are written to the rc file directly', (key) => {
it(`should set ${key} globally`, async () => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')
const initConfig = {
globalRc: {},
globalYaml: undefined,
localRc: undefined,
localYaml: undefined,
} satisfies ConfigFilesData
writeConfigFiles(configDir, tmp, initConfig)
await config.handler(createConfigCommandOpts({
dir: tmp,
cliOptions: {},
configDir,
global: true,
authConfig: {},
}), ['set', `${key}=https://registry.example.com/`])
expect(readConfigFiles(configDir, tmp)).toEqual({
...initConfig,
globalRc: { [key]: 'https://registry.example.com/' },
})
})
it(`should delete ${key} globally`, async () => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')
const initConfig = {
globalRc: { [key]: 'https://registry.example.com/' },
globalYaml: undefined,
localRc: undefined,
localYaml: undefined,
} satisfies ConfigFilesData
writeConfigFiles(configDir, tmp, initConfig)
await config.handler(createConfigCommandOpts({
dir: tmp,
cliOptions: {},
configDir,
global: true,
authConfig: {},
}), ['delete', key])
expect(readConfigFiles(configDir, tmp)).toEqual({
...initConfig,
globalRc: {},
})
})
})
describe.each(
[
'_auth',
'_authToken',
'_password',
'username',
'registry',
'@foo:registry',
'//registry.npmjs.org/:_authToken',
]
)('non-string values should be rejected', (key) => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')
it(`${key} should reject a non-string value`, async () => {
await expect(config.handler(createConfigCommandOpts({
json: true,
dir: tmp,
cliOptions: {},
configDir,
global: true,
authConfig: {},
}), ['set', key, '{}'])).rejects.toMatchObject({
code: 'ERR_PNPM_CONFIG_SET_AUTH_NON_STRING',
})
})
})
describe.each(
[
'._auth',
"['_auth']",
]
)('%p is handled as an auth setting', (propertyPath) => {
it('should set _auth', async () => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')
const initConfig = {
globalRc: {},
globalYaml: undefined,
localRc: undefined,
localYaml: undefined,
} satisfies ConfigFilesData
writeConfigFiles(configDir, tmp, initConfig)
await config.handler(createConfigCommandOpts({
dir: tmp,
cliOptions: {},
configDir,
global: true,
authConfig: {},
}), ['set', propertyPath, '123'])
expect(readConfigFiles(configDir, tmp)).toEqual({
...initConfig,
globalRc: { _auth: '123' },
})
})
it('should delete _auth', async () => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')
const initConfig = {
globalRc: { _auth: 'some-value' },
globalYaml: undefined,
localRc: undefined,
localYaml: undefined,
} satisfies ConfigFilesData
writeConfigFiles(configDir, tmp, initConfig)
await config.handler(createConfigCommandOpts({
dir: tmp,
cliOptions: {},
configDir,
global: true,
authConfig: {},
}), ['delete', propertyPath])
expect(readConfigFiles(configDir, tmp)).toEqual({
...initConfig,
globalRc: {},
})
})
})