Files
pnpm/engine/pm/commands/test/setup/setup.test.ts
Zoltan Kochan 7a304b17c4 refactor: rename directories and unify command packages per domain (#10993)
- Rename `installing/core` → `installing/deps-installer` and `installing/headless` → `installing/deps-restorer` for clearer naming
- Rename all `plugin-commands-*` directories to use `-commands` suffix convention
- Merge multiple command packages per domain into a single `commands/` directory (one commands package per domain rule):
  - `building/{build-commands,policy-commands}` → `building/commands`
  - `deps/compliance/{audit-commands,licenses-commands,sbom-commands}` → `deps/compliance/commands`
  - `deps/inspection/{listing-commands,outdated-commands}` → `deps/inspection/commands`
  - `store/{store-commands,inspecting-commands}` → `store/commands`
  - `releasing/{publish-commands,deploy-commands}` → `releasing/commands`
  - `cli/{completion-commands,doctor-commands}` → `cli/commands`
  - `engine/pm/{self-updater-commands,setup-commands}` → `engine/pm/commands`
  - `engine/runtime/{runtime-commands,env-commands}` → `engine/runtime/commands`
  - `cache/cache-commands` → `cache/commands`
- Fix relative paths in merged test files (pnpmBin, __typings__ references)
- Update jest config to ignore `utils/` dirs at any nesting depth under `test/`
- Fix stale package names in changeset files
2026-03-17 17:42:20 +01:00

86 lines
2.7 KiB
TypeScript

import { jest } from '@jest/globals'
import { PnpmError } from '@pnpm/error'
import type { PathExtenderReport } from '@pnpm/os.env.path-extender'
jest.unstable_mockModule('@pnpm/os.env.path-extender', () => ({
addDirToEnvPath: jest.fn(),
}))
const actualFs = await import('node:fs')
jest.unstable_mockModule('fs', () => {
return {
...actualFs,
promises: {
...actualFs.promises,
readFile: jest.fn(),
writeFile: jest.fn(),
},
}
})
const { addDirToEnvPath } = await import('@pnpm/os.env.path-extender')
const { setup } = await import('@pnpm/engine.pm.commands')
test('setup makes no changes', async () => {
jest.mocked(addDirToEnvPath).mockReturnValue(Promise.resolve<PathExtenderReport>({
oldSettings: 'PNPM_HOME=dir',
newSettings: 'PNPM_HOME=dir',
}))
const output = await setup.handler({ pnpmHomeDir: '' })
expect(output).toBe('No changes to the environment were made. Everything is already up to date.')
})
test('setup makes changes on POSIX', async () => {
jest.mocked(addDirToEnvPath).mockReturnValue(Promise.resolve<PathExtenderReport>({
configFile: {
changeType: 'created',
path: '~/.bashrc',
},
oldSettings: 'export PNPM_HOME=dir1',
newSettings: 'export PNPM_HOME=dir2',
}))
const output = await setup.handler({ pnpmHomeDir: '' })
expect(output).toBe(`Created ~/.bashrc
Next configuration changes were made:
export PNPM_HOME=dir2
To start using pnpm, run:
source ~/.bashrc
`)
})
test('setup makes changes on Windows', async () => {
jest.mocked(addDirToEnvPath).mockReturnValue(Promise.resolve<PathExtenderReport>({
oldSettings: 'export PNPM_HOME=dir1',
newSettings: 'export PNPM_HOME=dir2',
}))
const output = await setup.handler({ pnpmHomeDir: '' })
expect(output).toBe(`Next configuration changes were made:
export PNPM_HOME=dir2
Setup complete. Open a new terminal to start using pnpm.`)
})
test('hint is added to ERR_PNPM_BAD_ENV_FOUND error object', async () => {
jest.mocked(addDirToEnvPath).mockReturnValue(Promise.reject(new PnpmError('BAD_ENV_FOUND', '')))
let err!: PnpmError
try {
await setup.handler({ pnpmHomeDir: '' })
} catch (_err: any) { // eslint-disable-line
err = _err
}
expect(err?.hint).toBe('If you want to override the existing env variable, use the --force option')
})
test('hint is added to ERR_PNPM_BAD_SHELL_SECTION error object', async () => {
jest.mocked(addDirToEnvPath).mockReturnValue(Promise.reject(new PnpmError('BAD_SHELL_SECTION', '')))
let err!: PnpmError
try {
await setup.handler({ pnpmHomeDir: '' })
} catch (_err: any) { // eslint-disable-line
err = _err
}
expect(err?.hint).toBe('If you want to override the existing configuration section, use the --force option')
})