Files
pnpm/pnpm11/engine/runtime/commands/test/runtime.test.ts
T
Zoltan Kochan 23996e9504 feat(pacquet): support global runtime install (#12814)
`pnpm runtime set <name> <version> -g` now installs the runtime
(Node/Deno/Bun) into the global packages directory and links its binary
into the global bin directory, matching the TypeScript pnpm CLI. It
previously errored with a "not supported yet" stub.

The `<name>@runtime:<version>` selector is routed through the existing
global-add pipeline. The wrinkle: pacquet's manifest writer folds a
`runtime:<version>` dependency into `engines.runtime` on save, so a
globally-installed runtime lands under `engines.runtime` with an empty
`dependencies` map. The global scanner and bin-linker read `dependencies`,
so without a fix they would link no binary. The global crate now reifies
`engines.runtime` / `devEngines.runtime` back into dependencies when
reading a group manifest — the same conversion the package manifest
reader already applies — so an installed runtime is treated as the direct
dependency it is. `list -g`, `remove -g`, and `update -g` therefore see
it too.

The TypeScript pnpm CLI already ships this feature, so this brings pacquet
to parity and needs no TypeScript change; pacquet crates are unpublished,
so no changeset is required.
2026-07-05 22:33:06 +02:00

188 lines
5.6 KiB
TypeScript

import { beforeEach, expect, jest, test } from '@jest/globals'
import { PnpmError } from '@pnpm/error'
const mockRunPnpmCli = jest.fn()
jest.unstable_mockModule('@pnpm/exec.pnpm-cli-runner', () => ({
runPnpmCli: mockRunPnpmCli,
}))
const { runtime } = await import('@pnpm/engine.runtime.commands')
beforeEach(() => {
mockRunPnpmCli.mockClear()
})
test('runtime set calls pnpm add with the correct arguments globally', async () => {
await runtime.handler({
bin: '/usr/local/bin',
cacheDir: '/tmp/cache',
dir: '/tmp/project',
global: true,
pnpmHomeDir: '/tmp/pnpm-home',
storeDir: '/tmp/store',
}, ['set', 'node', '22'])
expect(mockRunPnpmCli).toHaveBeenCalledWith(
['add', 'node@runtime:22', '--save-dev', '--global', '--global-bin-dir', '/usr/local/bin', '--store-dir', '/tmp/store', '--cache-dir', '/tmp/cache'],
{ cwd: '/tmp/pnpm-home' }
)
})
test('runtime set defaults to --save-dev so the runtime lands in devEngines', async () => {
await runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: false,
pnpmHomeDir: '/tmp/pnpm-home',
}, ['set', 'node', '22'])
expect(mockRunPnpmCli).toHaveBeenCalledWith(
['add', 'node@runtime:22', '--save-dev', '--ignore-workspace-root-check'],
{ cwd: '/tmp/project' }
)
})
test('runtime set with --save-prod saves the runtime under engines', async () => {
await runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: false,
pnpmHomeDir: '/tmp/pnpm-home',
saveProd: true,
}, ['set', 'node', '22'])
expect(mockRunPnpmCli).toHaveBeenCalledWith(
['add', 'node@runtime:22', '--save-prod', '--ignore-workspace-root-check'],
{ cwd: '/tmp/project' }
)
})
test('runtime set with --save-dev keeps the runtime under devEngines (matches the default)', async () => {
await runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: false,
pnpmHomeDir: '/tmp/pnpm-home',
saveDev: true,
}, ['set', 'node', '22'])
expect(mockRunPnpmCli).toHaveBeenCalledWith(
['add', 'node@runtime:22', '--save-dev', '--ignore-workspace-root-check'],
{ cwd: '/tmp/project' }
)
})
test('runtime set with both --save-dev and --save-prod prefers --save-dev (matches getSaveType precedence)', async () => {
await runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: false,
pnpmHomeDir: '/tmp/pnpm-home',
saveDev: true,
saveProd: true,
}, ['set', 'node', '22'])
expect(mockRunPnpmCli).toHaveBeenCalledWith(
['add', 'node@runtime:22', '--save-dev', '--ignore-workspace-root-check'],
{ cwd: '/tmp/project' }
)
})
test('runtime set without version spec', async () => {
await runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: true,
pnpmHomeDir: '/tmp/pnpm-home',
}, ['set', 'node'])
expect(mockRunPnpmCli).toHaveBeenCalledWith(
['add', 'node@runtime:', '--save-dev', '--global', '--global-bin-dir', '/usr/local/bin'],
{ cwd: '/tmp/pnpm-home' }
)
})
test('runtime set works with deno', async () => {
await runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: true,
pnpmHomeDir: '/tmp/pnpm-home',
}, ['set', 'deno', '2'])
expect(mockRunPnpmCli).toHaveBeenCalledWith(
['add', 'deno@runtime:2', '--save-dev', '--global', '--global-bin-dir', '/usr/local/bin'],
{ cwd: '/tmp/pnpm-home' }
)
})
test('fail if no subcommand is given', async () => {
await expect(
runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: true,
pnpmHomeDir: '/tmp/pnpm-home',
}, [])
).rejects.toEqual(new PnpmError('RUNTIME_NO_SUBCOMMAND', 'Please specify the subcommand'))
expect(mockRunPnpmCli).not.toHaveBeenCalled()
})
test('fail if unknown subcommand is given', async () => {
await expect(
runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: true,
pnpmHomeDir: '/tmp/pnpm-home',
}, ['foo'])
).rejects.toEqual(new PnpmError('RUNTIME_UNKNOWN_SUBCOMMAND', 'Unknown subcommand: foo'))
expect(mockRunPnpmCli).not.toHaveBeenCalled()
})
test('fail if runtime name is missing', async () => {
await expect(
runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: true,
pnpmHomeDir: '/tmp/pnpm-home',
}, ['set'])
).rejects.toEqual(new PnpmError('MISSING_RUNTIME_NAME', '"pnpm runtime set <name> <version>" requires a runtime name (e.g. node, deno, bun)'))
expect(mockRunPnpmCli).not.toHaveBeenCalled()
})
test.each([
['an unknown runtime', 'python'],
['a comma-separated package list', 'node,is-positive'],
['a relative local path', './evil'],
['a file: local path', 'file:./evil'],
])('fail if the runtime name is %s', async (_description, runtimeName) => {
await expect(
runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: true,
pnpmHomeDir: '/tmp/pnpm-home',
}, ['set', runtimeName, '22'])
).rejects.toEqual(new PnpmError('INVALID_RUNTIME_NAME', `"${runtimeName}" is not a supported runtime. Supported runtimes are: node, deno, bun`))
expect(mockRunPnpmCli).not.toHaveBeenCalled()
})
test('fail if the version contains a comma', async () => {
await expect(
runtime.handler({
bin: '/usr/local/bin',
dir: '/tmp/project',
global: true,
pnpmHomeDir: '/tmp/pnpm-home',
}, ['set', 'node', '22,is-positive'])
).rejects.toEqual(new PnpmError('INVALID_RUNTIME_VERSION', 'Invalid runtime version "22,is-positive": a version cannot contain a comma'))
expect(mockRunPnpmCli).not.toHaveBeenCalled()
})