import { spawn } from '@malept/cross-spawn-promise'; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi, } from 'vitest'; import { findUp } from '../src/find-up'; import { resolvePackageManager, spawnPackageManager, } from '../src/package-manager'; import { pathToFileURL } from 'node:url'; import path from 'node:path'; vi.mock('@malept/cross-spawn-promise'); vi.mock('../src/find-up', () => ({ findUp: vi.fn(), })); describe('package-manager', () => { beforeAll(() => { const originalUa = process.env.npm_config_user_agent; return () => { process.env.npm_config_user_agent = originalUa; }; }); describe('resolvePackageManager', () => { describe('npm_config_user_agent', () => { it.each([ { ua: 'yarn/1.22.22 npm/? node/v22.13.0 darwin arm64', pm: 'yarn', version: '1.22.22', }, { ua: 'pnpm/10.0.0 npm/? node/v20.11.1 darwin arm64', pm: 'pnpm', version: '10.0.0', }, { ua: 'npm/10.9.2 node/v22.13.0 darwin arm64 workspaces/false', pm: 'npm', version: '10.9.2', }, ])('with $ua', async ({ ua, pm, version }) => { process.env.npm_config_user_agent = ua; await expect(resolvePackageManager()).resolves.toHaveProperty( 'executable', pm, ); await expect(resolvePackageManager()).resolves.toHaveProperty( 'version', version, ); }); it('should return yarn if npm_config_user_agent=yarn', async () => { process.env.npm_config_user_agent = 'yarn/1.22.22 npm/? node/v22.13.0 darwin arm64'; await expect(resolvePackageManager()).resolves.toHaveProperty( 'executable', 'yarn', ); await expect(resolvePackageManager()).resolves.toHaveProperty( 'version', '1.22.22', ); }); it('should return pnpm if npm_config_user_agent=pnpm', async () => { process.env.npm_config_user_agent = 'pnpm/10.0.0 npm/? node/v20.11.1 darwin arm64'; await expect(resolvePackageManager()).resolves.toHaveProperty( 'executable', 'pnpm', ); }); it('should return npm if npm_config_user_agent=npm', async () => { process.env.npm_config_user_agent = 'npm/10.9.2 node/v22.13.0 darwin arm64 workspaces/false'; await expect(resolvePackageManager()).resolves.toHaveProperty( 'executable', 'npm', ); }); }); it('should use the package manager for the nearest ancestor lockfile if detected', async () => { delete process.env.npm_config_user_agent; vi.mocked(findUp).mockResolvedValue('/Users/foo/bar/yarn.lock'); vi.mocked(spawn).mockResolvedValue('1.22.22'); await expect(resolvePackageManager()).resolves.toHaveProperty( 'executable', 'yarn', ); }); it('should explain when the lockfile package manager cannot be run', async () => { delete process.env.npm_config_user_agent; vi.mocked(findUp).mockResolvedValue('/Users/foo/bar/yarn.lock'); vi.mocked(spawn).mockRejectedValue(new Error('spawn yarn ENOENT')); await expect(resolvePackageManager()).rejects.toThrow( /Found yarn\.lock, but "yarn" could not be run: spawn yarn ENOENT/, ); }); it('should fall back to npm if no other strategy worked', async () => { delete process.env.npm_config_user_agent; vi.mocked(findUp).mockResolvedValue(undefined); vi.mocked(spawn).mockResolvedValue('9.99.99'); await expect(resolvePackageManager()).resolves.toHaveProperty( 'executable', 'npm', ); await expect(resolvePackageManager()).resolves.toHaveProperty( 'version', '9.99.99', ); }); describe('cwd option', () => { beforeEach(() => { delete process.env.npm_config_user_agent; }); afterEach(() => { vi.mocked(findUp).mockResolvedValue(undefined); }); it('should prefer the lockfile over the user agent with preferLockfile', async () => { process.env.npm_config_user_agent = 'npm/10.9.2 node/v22.13.0 darwin arm64 workspaces/false'; // Platform-native paths, since the real `findUp` returns resolved ones const cwd = path.resolve('/some/project'); vi.mocked(findUp).mockResolvedValue(path.join(cwd, 'yarn.lock')); vi.mocked(spawn).mockResolvedValue('1.22.22'); const pm = await resolvePackageManager(undefined, { cwd, preferLockfile: true, }); expect(pm.executable).toBe('yarn'); expect(pm.version).toBe('1.22.22'); expect(spawn).toHaveBeenCalledWith( 'yarn', ['--version'], expect.objectContaining({ cwd }), ); }); it('should ignore a stray ancestor lockfile with preferLockfile', async () => { process.env.npm_config_user_agent = 'npm/10.9.2 node/v22.13.0 darwin arm64 workspaces/false'; // No package.json next to this lockfile, so it is not a project root const home = path.resolve('/nonexistent-forge-spec-home'); vi.mocked(findUp).mockResolvedValue(path.join(home, 'yarn.lock')); await expect( resolvePackageManager(undefined, { cwd: path.join(home, 'code', 'app'), preferLockfile: true, }), ).resolves.toHaveProperty('executable', 'npm'); }); it('should still use the user agent with preferLockfile when there is no lockfile', async () => { process.env.npm_config_user_agent = 'pnpm/10.0.0 npm/? node/v20.11.1 darwin arm64'; vi.mocked(findUp).mockResolvedValue(undefined); await expect( resolvePackageManager(undefined, { preferLockfile: true }), ).resolves.toHaveProperty('executable', 'pnpm'); }); it('should search for a lockfile starting from the given directory', async () => { vi.mocked(findUp).mockResolvedValue('/some/project/pnpm-lock.yaml'); vi.mocked(spawn).mockResolvedValue('10.0.0'); await expect( resolvePackageManager(undefined, { cwd: '/some/project' }), ).resolves.toHaveProperty('executable', 'pnpm'); expect(findUp).toHaveBeenCalledWith(expect.any(Array), { cwd: '/some/project', }); }); }); describe('with an explicit package manager passed in', () => { beforeEach(() => { vi.resetModules(); delete process.env.npm_config_user_agent; }); const packageManagerPath = pathToFileURL( path.resolve(import.meta.dirname, '../src/package-manager'), ).toString(); it('should accept a string with only the package manager name', async () => { const { resolvePackageManager } = await import(packageManagerPath); const first = await resolvePackageManager('pnpm'); expect(first.executable).toBe('pnpm'); expect(first.version).toBe('latest'); }); it('should accept the @latest tag', async () => { const { resolvePackageManager } = await import(packageManagerPath); const first = await resolvePackageManager('yarn@latest'); expect(first.executable).toBe('yarn'); expect(first.version).toBe('latest'); }); it('should accept a full version', async () => { const { resolvePackageManager } = await import(packageManagerPath); const first = await resolvePackageManager('yarn@1.22.22'); expect(first.executable).toBe('yarn'); expect(first.version).toBe('1.22.22'); }); it('should accept a major.minor version', async () => { const { resolvePackageManager } = await import(packageManagerPath); const first = await resolvePackageManager('yarn@1.22'); expect(first.executable).toBe('yarn'); expect(first.version).toBe('1.22'); }); it('should accept a major version', async () => { const { resolvePackageManager } = await import(packageManagerPath); const first = await resolvePackageManager('yarn@1'); expect(first.executable).toBe('yarn'); expect(first.version).toBe('1'); }); it('should cache explicit argument and ignore later env / lockfile', async () => { const { resolvePackageManager } = await import(packageManagerPath); const first = await resolvePackageManager('pnpm@10.0.0'); expect(first.executable).toBe('pnpm'); expect(first.version).toBe('10.0.0'); process.env.npm_config_user_agent = 'yarn/1.22.22 npm/? node/v22.13.0 darwin arm64'; vi.mocked(spawn).mockResolvedValue('9.9.9'); const second = await resolvePackageManager(); expect(second.executable).toBe('pnpm'); expect(second.version).toBe('10.0.0'); }); it('should fallback to npm and cache when explicit argument unsupported', async () => { const { resolvePackageManager } = await import(packageManagerPath); vi.mocked(spawn).mockResolvedValue('9.99.99'); const result = await resolvePackageManager('good coffee'); expect(result.executable).toBe('npm'); expect(result.version).toBe('9.99.99'); const again = await resolvePackageManager(); expect(again.executable).toBe('npm'); expect(again.version).toBe('9.99.99'); }); }); }); describe('spawnPackageManager', () => { afterEach(() => { vi.unstubAllEnvs(); }); it('should trim the output', async () => { vi.mocked(spawn).mockResolvedValue(' foo \n'); const result = await spawnPackageManager({ executable: 'npm', install: 'install', dev: '--save-dev', exact: '--save-exact', }); expect(result).toBe('foo'); }); it('should strip leaked npm allow-scripts config from the child env', async () => { vi.stubEnv('npm_config_allow_scripts', 'foo'); vi.stubEnv('NPM_CONFIG_STRICT_ALLOW_SCRIPTS', 'true'); vi.stubEnv('npm_config_dangerously_allow_all_scripts', 'true'); vi.stubEnv('FORGE_UNRELATED_VAR', 'keep'); vi.mocked(spawn).mockResolvedValue(''); await spawnPackageManager( { executable: 'npm', install: 'install', dev: '--save-dev', exact: '--save-exact', }, ['install'], { cwd: 'mydir', env: { FORGE_CALLER_VAR: 'caller' } }, ); const opts = vi.mocked(spawn).mock.lastCall?.[2]; expect(opts).toHaveProperty('cwd', 'mydir'); expect(opts?.env).toMatchObject({ FORGE_UNRELATED_VAR: 'keep', FORGE_CALLER_VAR: 'caller', }); expect(opts?.env).not.toHaveProperty('npm_config_allow_scripts'); expect(opts?.env).not.toHaveProperty('NPM_CONFIG_STRICT_ALLOW_SCRIPTS'); expect(opts?.env).not.toHaveProperty( 'npm_config_dangerously_allow_all_scripts', ); }); }); });