|
| 1 | +import { execFileSync } from 'node:child_process'; |
| 2 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { dirname, join } from 'node:path'; |
| 5 | + |
| 6 | +import { expect, test } from 'vitest'; |
| 7 | + |
| 8 | +import { brandVite } from '../brand-vite.ts'; |
| 9 | +import { alignVendoredVitestDependencies } from '../vendored-vitest.mjs'; |
| 10 | + |
| 11 | +const upstreamSources = { |
| 12 | + 'constants.ts': 'export const VERSION = version as string\n', |
| 13 | + 'cli.ts': [ |
| 14 | + "import { VERSION } from './constants'", |
| 15 | + "cac('vite')", |
| 16 | + 'cli.version(VERSION)', |
| 17 | + 'colors.green(', |
| 18 | + " `${colors.bold('VITE')} v${VERSION}`,", |
| 19 | + ' )', |
| 20 | + '', |
| 21 | + ].join('\n'), |
| 22 | + 'build.ts': [ |
| 23 | + ' logger.info(', |
| 24 | + ' colors.cyan(', |
| 25 | + ' `vite v${VERSION} ${colors.green(', |
| 26 | + ' `building ${environment.name} environment for ${environment.config.mode}...`,', |
| 27 | + ' )}`,', |
| 28 | + ' ),', |
| 29 | + ' )', |
| 30 | + '`[vite]: Rolldown failed`', |
| 31 | + '', |
| 32 | + ].join('\n'), |
| 33 | + 'logger.ts': "prefix = '[vite]'\n", |
| 34 | + 'plugins/reporter.ts': [ |
| 35 | + "import path from 'node:path'", |
| 36 | + ' logInfo: shouldLogInfo ? (msg) => env.logger.info(msg) : undefined,', |
| 37 | + '', |
| 38 | + ].join('\n'), |
| 39 | + 'config.ts': [ |
| 40 | + ' !process.env.VITE_CONFIG_NATIVE_IGNORE_WARNING &&', |
| 41 | + ' createNativeConfigCompatPlugin(nativeIncompatibilities),', |
| 42 | + '', |
| 43 | + ].join('\n'), |
| 44 | +}; |
| 45 | + |
| 46 | +function write(file: string, source: string) { |
| 47 | + mkdirSync(dirname(file), { recursive: true }); |
| 48 | + writeFileSync(file, source); |
| 49 | +} |
| 50 | + |
| 51 | +test('branding preserves aligned dependencies and unrelated changes across repeated builds', () => { |
| 52 | + const root = mkdtempSync(join(tmpdir(), 'vp-brand-vite-')); |
| 53 | + const viteDir = join(root, 'vite'); |
| 54 | + const nodeDir = join(viteDir, 'packages/vite/src/node'); |
| 55 | + const git = (...args: string[]) => execFileSync('git', args, { cwd: viteDir, stdio: 'pipe' }); |
| 56 | + try { |
| 57 | + for (const [file, source] of Object.entries(upstreamSources)) { |
| 58 | + write(join(nodeDir, file), source); |
| 59 | + } |
| 60 | + const manifest = join(viteDir, 'packages/vite/package.json'); |
| 61 | + const lockfile = join(viteDir, 'pnpm-lock.yaml'); |
| 62 | + const unrelatedSource = join(nodeDir, 'server.ts'); |
| 63 | + write(manifest, '{"devDependencies":{"@vitest/utils":"4.1.10"}}\n'); |
| 64 | + write(lockfile, '# upstream lockfile\n'); |
| 65 | + write(unrelatedSource, '// upstream source\n'); |
| 66 | + git('init'); |
| 67 | + git('config', 'core.autocrlf', 'false'); |
| 68 | + git('config', 'core.hooksPath', join(root, 'no-hooks')); |
| 69 | + git('add', '.'); |
| 70 | + git( |
| 71 | + '-c', |
| 72 | + 'user.name=Test', |
| 73 | + '-c', |
| 74 | + 'user.email=test@example.com', |
| 75 | + '-c', |
| 76 | + 'commit.gpgsign=false', |
| 77 | + 'commit', |
| 78 | + '-m', |
| 79 | + 'upstream fixture', |
| 80 | + ); |
| 81 | + |
| 82 | + mkdirSync(join(root, 'rolldown/packages'), { recursive: true }); |
| 83 | + alignVendoredVitestDependencies(root, '5.0.0'); |
| 84 | + const alignedManifest = readFileSync(manifest, 'utf8'); |
| 85 | + expect(JSON.parse(alignedManifest).devDependencies['@vitest/utils']).toBe('5.0.0'); |
| 86 | + write(lockfile, '# local lockfile change\n'); |
| 87 | + write(unrelatedSource, '// unrelated local source change\n'); |
| 88 | + |
| 89 | + let brandedSources: string[] | undefined; |
| 90 | + for (let run = 0; run < 2; run++) { |
| 91 | + brandVite(root); |
| 92 | + |
| 93 | + expect(readFileSync(manifest, 'utf8')).toBe(alignedManifest); |
| 94 | + expect(readFileSync(lockfile, 'utf8')).toBe('# local lockfile change\n'); |
| 95 | + expect(readFileSync(unrelatedSource, 'utf8')).toBe('// unrelated local source change\n'); |
| 96 | + expect(readFileSync(join(nodeDir, 'cli.ts'), 'utf8')).toContain("cac('vp')"); |
| 97 | + expect(readFileSync(join(nodeDir, 'build.ts'), 'utf8')).not.toContain('logger.info('); |
| 98 | + const sources = Object.entries(upstreamSources).map(([file, upstream]) => { |
| 99 | + const source = readFileSync(join(nodeDir, file), 'utf8'); |
| 100 | + expect(source).not.toBe(upstream); |
| 101 | + return source; |
| 102 | + }); |
| 103 | + if (brandedSources) { |
| 104 | + expect(sources).toEqual(brandedSources); |
| 105 | + } |
| 106 | + brandedSources = sources; |
| 107 | + } |
| 108 | + } finally { |
| 109 | + rmSync(root, { recursive: true, force: true }); |
| 110 | + } |
| 111 | +}); |
0 commit comments