Files
pnpm/packages/plugin-commands-setup/test/setup.test.ts
Zoltan Kochan a8f016ca59 feat: store config deps and package manager integrities in pnpm-lock.env.yaml (#10912)
## Summary

Store config dependency and package manager integrity info in a separate `pnpm-lock.env.yaml` lockfile instead of inlining it in `pnpm-workspace.yaml`. The workspace manifest now contains only clean version specifiers for `configDependencies`, while the resolved versions, integrity hashes, and tarball URLs are recorded in the new env lockfile.

### Key changes

- **New `pnpm-lock.env.yaml` lockfile**: Uses the standard lockfile format (`importers`, `packages`, `snapshots`) to store resolved config dependencies and package manager dependencies with integrity hashes and tarball URLs.
- **Automatic migration**: Projects using the old inline-hash format in `pnpm-workspace.yaml` are automatically migrated on install.
- **Global Virtual Store (GVS) for version switching**: When switching pnpm versions via the `packageManager` field, pnpm is installed to the global virtual store (`$STORE_DIR/links/`) instead of `globalPkgDir`, reusing the content-addressable store for deduplication.
- **Self-update uses headless install**: `pnpm self-update` performs frozen headless installs using integrity hashes from the env lockfile, then links bins to `PNPM_HOME`.
- **`packageManagerDependencies`**: The env lockfile also stores resolved `packageManagerDependencies` during version switching and self-update.
- **`@pnpm/exe` support**: Replicates `@pnpm/exe`'s postinstall script (linking platform-specific binaries) since install scripts are disabled.
- **`pnpm setup` refactored**: Uses `pnpm add -g` instead of copying the CLI binary directly.
- **Extracted `toLockfileResolution`** to `@pnpm/lockfile.utils` and **deduplicated `iteratePkgMeta`** into `@pnpm/calc-dep-state`.
- **Removed unused `@pnpm/tools.path` package**.
2026-03-11 00:39:37 +01:00

86 lines
2.7 KiB
TypeScript

import { PnpmError } from '@pnpm/error'
import { jest } from '@jest/globals'
import type { PathExtenderReport } from '@pnpm/os.env.path-extender'
jest.unstable_mockModule('@pnpm/os.env.path-extender', () => ({
addDirToEnvPath: jest.fn(),
}))
const actualFs = await import('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/plugin-commands-setup')
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')
})