|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +import { PackageManager, type WorkspaceInfo } from '../../types/index.js'; |
| 8 | + |
| 9 | +// Mock with file: protocol paths (simulating VP_OVERRIDE_PACKAGES in CI) |
| 10 | +vi.mock('../../utils/constants.js', async (importOriginal) => { |
| 11 | + const mod = await importOriginal<typeof import('../../utils/constants.js')>(); |
| 12 | + return { |
| 13 | + ...mod, |
| 14 | + VITE_PLUS_VERSION: 'file:/tmp/tgz/vite-plus-0.0.0.tgz', |
| 15 | + VITE_PLUS_OVERRIDE_PACKAGES: { |
| 16 | + vite: 'file:/tmp/tgz/voidzero-dev-vite-plus-core-0.0.0.tgz', |
| 17 | + vitest: 'file:/tmp/tgz/voidzero-dev-vite-plus-test-0.0.0.tgz', |
| 18 | + '@voidzero-dev/vite-plus-core': 'file:/tmp/tgz/voidzero-dev-vite-plus-core-0.0.0.tgz', |
| 19 | + '@voidzero-dev/vite-plus-test': 'file:/tmp/tgz/voidzero-dev-vite-plus-test-0.0.0.tgz', |
| 20 | + }, |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +const { rewriteMonorepo } = await import('../migrator.js'); |
| 25 | + |
| 26 | +function makeWorkspaceInfo(rootDir: string, packageManager: PackageManager): WorkspaceInfo { |
| 27 | + return { |
| 28 | + rootDir, |
| 29 | + isMonorepo: false, |
| 30 | + monorepoScope: '', |
| 31 | + workspacePatterns: [], |
| 32 | + parentDirs: [], |
| 33 | + packageManager, |
| 34 | + packageManagerVersion: '10.33.0', |
| 35 | + downloadPackageManager: { |
| 36 | + name: 'pnpm', |
| 37 | + installDir: '/tmp', |
| 38 | + binPrefix: '/tmp/bin', |
| 39 | + packageName: 'pnpm', |
| 40 | + version: '10.33.0', |
| 41 | + }, |
| 42 | + packages: [], |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +function readJson(filePath: string): Record<string, unknown> { |
| 47 | + return JSON.parse(fs.readFileSync(filePath, 'utf8')); |
| 48 | +} |
| 49 | + |
| 50 | +describe('rewriteMonorepo bun catalog with file: protocol', () => { |
| 51 | + let tmpDir: string; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-test-bun-file-')); |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('uses file: paths directly in overrides instead of catalog:', () => { |
| 62 | + fs.writeFileSync( |
| 63 | + path.join(tmpDir, 'package.json'), |
| 64 | + JSON.stringify({ |
| 65 | + name: 'bun-monorepo', |
| 66 | + workspaces: ['packages/*'], |
| 67 | + devDependencies: { vite: '^7.0.0' }, |
| 68 | + packageManager: 'bun@1.3.11', |
| 69 | + }), |
| 70 | + ); |
| 71 | + rewriteMonorepo(makeWorkspaceInfo(tmpDir, PackageManager.bun), true); |
| 72 | + |
| 73 | + const pkg = readJson(path.join(tmpDir, 'package.json')); |
| 74 | + // catalog should not contain file: entries |
| 75 | + const catalog = (pkg.catalog ?? {}) as Record<string, string>; |
| 76 | + expect(catalog.vite).toBeUndefined(); |
| 77 | + expect(catalog.vitest).toBeUndefined(); |
| 78 | + // overrides should use file: paths directly, not catalog: |
| 79 | + const overrides = pkg.overrides as Record<string, string>; |
| 80 | + expect(overrides.vite).toBe('file:/tmp/tgz/voidzero-dev-vite-plus-core-0.0.0.tgz'); |
| 81 | + expect(overrides.vitest).toBe('file:/tmp/tgz/voidzero-dev-vite-plus-test-0.0.0.tgz'); |
| 82 | + expect(overrides['@voidzero-dev/vite-plus-core']).toBe( |
| 83 | + 'file:/tmp/tgz/voidzero-dev-vite-plus-core-0.0.0.tgz', |
| 84 | + ); |
| 85 | + expect(overrides['@voidzero-dev/vite-plus-test']).toBe( |
| 86 | + 'file:/tmp/tgz/voidzero-dev-vite-plus-test-0.0.0.tgz', |
| 87 | + ); |
| 88 | + }); |
| 89 | +}); |
0 commit comments