verify-cli-release.mjs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import assert from 'node:assert/strict';
  2. import { createHash } from 'node:crypto';
  3. import { createRequire } from 'node:module';
  4. import { execFileSync } from 'node:child_process';
  5. import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
  6. import { tmpdir } from 'node:os';
  7. import path from 'node:path';
  8. import { fileURLToPath } from 'node:url';
  9. import { versionFromTag } from './lib/cli-release.mjs';
  10. const root = fileURLToPath(new URL('../', import.meta.url));
  11. const version = versionFromTag(process.env.MCODE_RELEASE_TAG);
  12. if (!process.env.MCODE_RELEASE_ARCHIVE) throw new Error('MCODE_RELEASE_ARCHIVE is required.');
  13. if (!['linux', 'darwin'].includes(process.platform)) throw new Error('Package validation currently supports Linux and macOS.');
  14. const archive = path.resolve(process.env.MCODE_RELEASE_ARCHIVE);
  15. const sha256 = createHash('sha256').update(readFileSync(archive)).digest('hex');
  16. assert.equal(readFileSync(`${archive}.sha256`, 'utf8'), `${sha256} ${path.basename(archive)}\n`, 'Release archive checksum mismatch');
  17. const revision = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: root, encoding: 'utf8' }).trim();
  18. const temporary = mkdtempSync(path.join(tmpdir(), 'mcode-package-install-'));
  19. try {
  20. // No global install or real user profile is modified. Dependencies and the
  21. // generated npm launcher must resolve from this fresh installation alone.
  22. const prefix = path.join(temporary, 'install');
  23. const home = path.join(temporary, 'home');
  24. mkdirSync(home);
  25. const env = {
  26. ...process.env, HOME: home, USERPROFILE: home,
  27. MINIMAX_DATA_DIR: path.join(home, 'data'), MAVIS_DATA_DIR: path.join(home, 'data'),
  28. MCODE_DISABLE_TELEMETRY: '1',
  29. };
  30. for (const name of Object.keys(env)) {
  31. if (/^npm_config_/i.test(name)) delete env[name];
  32. }
  33. Object.assign(env, {
  34. npm_config_cache: path.join(temporary, 'npm-cache'),
  35. npm_config_userconfig: path.join(home, '.npmrc'),
  36. });
  37. delete env.NODE_PATH;
  38. delete env.NODE_OPTIONS;
  39. execFileSync('npm', ['install', '--global', '--prefix', prefix,
  40. '--registry=https://registry.npmjs.org/', '--include=optional', '--ignore-scripts=false',
  41. '--allow-scripts=better-sqlite3', '--no-audit', '--no-fund', archive],
  42. { cwd: home, env, stdio: 'inherit', timeout: 300000 });
  43. const installed = path.join(prefix, 'lib/node_modules/@minimax-ai/code');
  44. const release = JSON.parse(readFileSync(path.join(installed, 'release.json'), 'utf8'));
  45. assert.equal(release.version, version);
  46. assert.equal(release.tag, process.env.MCODE_RELEASE_TAG);
  47. assert.equal(release.revision, revision);
  48. const result = execFileSync(path.join(prefix, 'bin/mcode'), ['--version'], { cwd: home, env, encoding: 'utf8', timeout: 30000 });
  49. assert.equal(result.trim(), version);
  50. const require = createRequire(path.join(installed, 'package.json'));
  51. const Database = require('better-sqlite3');
  52. const db = new Database(':memory:');
  53. try { assert.equal(db.prepare('select 42 as value').get().value, 42); } finally { db.close(); }
  54. assert.match(execFileSync(require('@vscode/ripgrep').rgPath, ['--version'], { encoding: 'utf8' }), /ripgrep/);
  55. execFileSync(process.execPath, ['--test', 'test/smoke.test.mjs', 'test/byok.test.mjs'], {
  56. cwd: root, env: { ...env, MCODE_TEST_CLI: path.join(installed, 'cli.js') }, stdio: 'inherit', timeout: 240000,
  57. });
  58. if (process.env.MCODE_VERIFY_REPORT_DIR) {
  59. mkdirSync(process.env.MCODE_VERIFY_REPORT_DIR, { recursive: true });
  60. writeFileSync(path.join(process.env.MCODE_VERIFY_REPORT_DIR, 'package-install.json'), JSON.stringify({
  61. status: 'PASS', version, revision, sha256, platform: process.platform, arch: process.arch, node: process.version,
  62. }, null, 2) + '\n');
  63. }
  64. console.log(`Verified npm installation of ${path.basename(archive)} (${sha256}).`);
  65. } finally {
  66. rmSync(temporary, { recursive: true, force: true });
  67. }