| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,90 @@ |
| +import { mkdtempSync, rmSync, writeFileSync } from 'fs' |
| +import { tmpdir } from 'os' |
| +import { join } from 'path' |
| + |
| +import { getPkgManager as getCreateNextAppPkgManager } from '../../packages/create-next-app/helpers/get-pkg-manager' |
| +import { getPkgManager as getCodemodPkgManager } from '../../packages/next-codemod/lib/handle-package' |
| +import { getPkgManager as getNextPkgManager } from '../../packages/next/src/lib/helpers/get-pkg-manager' |
| + |
| +const originalUserAgent = process.env.npm_config_user_agent |
| +const temporaryDirectories: string[] = [] |
| + |
| +function projectWithLockfiles(...lockfiles: string[]) { |
| + const directory = mkdtempSync(join(tmpdir(), 'next-pkg-manager-')) |
| + temporaryDirectories.push(directory) |
| + for (const lockfile of lockfiles) { |
| + writeFileSync(join(directory, lockfile), '') |
| + } |
| + return directory |
| +} |
| + |
| +afterEach(() => { |
| + if (originalUserAgent === undefined) { |
| + delete process.env.npm_config_user_agent |
| + } else { |
| + process.env.npm_config_user_agent = originalUserAgent |
| + } |
| + |
| + for (const directory of temporaryDirectories.splice(0)) { |
| + rmSync(directory, { recursive: true, force: true }) |
| + } |
| +}) |
| + |
| +describe('package manager detection', () => { |
| + test.each([ |
| + ['yarn', 'package-lock.json'], |
| + ['pnpm', 'package-lock.json'], |
| + ['bun', 'package-lock.json'], |
| + ['npm', 'yarn.lock'], |
| + ] as const)( |
| + 'the codemod prefers a %s user agent over a conflicting lockfile', |
| + (packageManager, conflictingLockfile) => { |
| + process.env.npm_config_user_agent = `${packageManager}/1.2.3 node/v22` |
| + const project = projectWithLockfiles(conflictingLockfile) |
| + |
| + expect(getCodemodPkgManager(project)).toBe(packageManager) |
| + } |
| + ) |
| + |
| + test('the codemod uses the shared package manager order when lockfiles coexist', () => { |
| + delete process.env.npm_config_user_agent |
| + const project = projectWithLockfiles( |
| + 'package-lock.json', |
| + 'bun.lock', |
| + 'pnpm-lock.yaml', |
| + 'yarn.lock' |
| + ) |
| + |
| + expect(getCodemodPkgManager(project)).toBe('yarn') |
| + }) |
| + |
| + test.each([ |
| + ['yarn', 'package-lock.json'], |
| + ['pnpm', 'yarn.lock'], |
| + ['npm', 'yarn.lock'], |
| + ] as const)( |
| + 'Next.js prefers a %s user agent over a conflicting lockfile', |
| + (packageManager, conflictingLockfile) => { |
| + process.env.npm_config_user_agent = `${packageManager}/1.2.3 node/v22` |
| + const project = projectWithLockfiles(conflictingLockfile) |
| + |
| + expect(getNextPkgManager(project)).toBe(packageManager) |
| + } |
| + ) |
| + |
| + test('Next.js falls back to lockfile detection for a bun user agent', () => { |
| + process.env.npm_config_user_agent = 'bun/1.2.3 node/v22' |
| + const project = projectWithLockfiles('pnpm-lock.yaml') |
| + |
| + expect(getNextPkgManager(project)).toBe('pnpm') |
| + }) |
| + |
| + test.each(['yarn', 'pnpm', 'bun', 'npm'] as const)( |
| + 'create-next-app recognizes a %s user agent', |
| + (packageManager) => { |
| + process.env.npm_config_user_agent = `${packageManager}/1.2.3 node/v22` |
| + |
| + expect(getCreateNextAppPkgManager()).toBe(packageManager) |
| + } |
| + ) |
| +}) |
|
|