File size: 3,309 Bytes
b2cad4f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | diff --git a/test/unit/package-manager-detection.test.ts b/test/unit/package-manager-detection.test.ts
new file mode 100644
index 0000000000..e175f4d09d
--- /dev/null
+++ b/test/unit/package-manager-detection.test.ts
@@ -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)
+ }
+ )
+})
|