install-sh-prune.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * install.sh version-prune tests (issue #1074).
  3. *
  4. * The standalone installer keeps each release in its own `versions/<v>` dir and
  5. * — before this fix — never removed the old ones, so they piled up (~50 MB of
  6. * vendored Node runtime each) across upgrades. `install.sh` now prunes every
  7. * `versions/*` dir except the one it just installed.
  8. *
  9. * Rather than duplicate the shell (which would drift from the shipped script),
  10. * these tests extract the REAL prune block from `install.sh` — between its
  11. * `CODEGRAPH_PRUNE_OLD_VERSIONS` markers — and run it under `sh` against a temp
  12. * fixture, with `$INSTALL_DIR` / `$dest` injected. No network, no download.
  13. *
  14. * POSIX only: the block is `/bin/sh`. Windows installs overwrite a single dir in
  15. * place (install.ps1) and never reach this code, so there's nothing to prune.
  16. */
  17. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  18. import { spawnSync } from 'child_process';
  19. import * as fs from 'fs';
  20. import * as os from 'os';
  21. import * as path from 'path';
  22. const INSTALL_SH = path.join(__dirname, '..', 'install.sh');
  23. const START = '# >>> CODEGRAPH_PRUNE_OLD_VERSIONS';
  24. const END = '# <<< CODEGRAPH_PRUNE_OLD_VERSIONS';
  25. /** Pull the exact prune block out of the shipped install.sh (no duplication). */
  26. function extractPruneBlock(): string {
  27. const lines = fs.readFileSync(INSTALL_SH, 'utf8').split('\n');
  28. const i = lines.findIndex((l) => l.trim() === START);
  29. const j = lines.findIndex((l) => l.trim() === END);
  30. if (i < 0 || j < 0 || j <= i) {
  31. throw new Error('CODEGRAPH_PRUNE_OLD_VERSIONS markers not found in install.sh');
  32. }
  33. return lines.slice(i + 1, j).join('\n');
  34. }
  35. /** Single-quote a path for safe interpolation into the sh script. */
  36. function shq(s: string): string {
  37. return `'${s.replace(/'/g, `'\\''`)}'`;
  38. }
  39. /** Run the real prune block with INSTALL_DIR/dest set, return code + stdout. */
  40. function runPrune(installDir: string, dest: string): { code: number; stdout: string } {
  41. const script = `set -eu\nINSTALL_DIR=${shq(installDir)}\ndest=${shq(dest)}\n${extractPruneBlock()}\n`;
  42. const r = spawnSync('sh', ['-c', script], { encoding: 'utf8' });
  43. return { code: r.status ?? -1, stdout: r.stdout ?? '' };
  44. }
  45. /** Create a versions/<v>/bin dir with a dummy launcher, like a real bundle. */
  46. function seedVersion(installDir: string, version: string): string {
  47. const dir = path.join(installDir, 'versions', version);
  48. fs.mkdirSync(path.join(dir, 'bin'), { recursive: true });
  49. fs.writeFileSync(path.join(dir, 'bin', 'codegraph'), '#!/bin/sh\n');
  50. return dir;
  51. }
  52. describe.skipIf(process.platform === 'win32')('install.sh version prune (#1074)', () => {
  53. let installDir: string;
  54. beforeEach(() => {
  55. installDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-prune-'));
  56. });
  57. afterEach(() => {
  58. fs.rmSync(installDir, { recursive: true, force: true });
  59. });
  60. it('removes older version dirs and keeps only the just-installed one', () => {
  61. seedVersion(installDir, 'v1.1.2');
  62. seedVersion(installDir, 'v1.1.3');
  63. const dest = seedVersion(installDir, 'v1.1.4');
  64. fs.symlinkSync(dest, path.join(installDir, 'current'));
  65. const { code, stdout } = runPrune(installDir, dest);
  66. expect(code).toBe(0);
  67. const remaining = fs.readdirSync(path.join(installDir, 'versions')).sort();
  68. expect(remaining).toEqual(['v1.1.4']);
  69. expect(stdout).toContain('Removed 2 older version(s)');
  70. // The `current` symlink (outside versions/) is never globbed → untouched.
  71. expect(fs.existsSync(path.join(installDir, 'current'))).toBe(true);
  72. expect(fs.realpathSync(path.join(installDir, 'current'))).toBe(fs.realpathSync(dest));
  73. });
  74. it('is a silent no-op when the just-installed version is the only one', () => {
  75. const dest = seedVersion(installDir, 'v1.1.4');
  76. const { code, stdout } = runPrune(installDir, dest);
  77. expect(code).toBe(0);
  78. expect(fs.readdirSync(path.join(installDir, 'versions'))).toEqual(['v1.1.4']);
  79. expect(stdout).not.toContain('Removed');
  80. });
  81. it('does not error when there is no versions/ dir yet', () => {
  82. const dest = path.join(installDir, 'versions', 'v1.1.4'); // never created
  83. const { code, stdout } = runPrune(installDir, dest);
  84. expect(code).toBe(0);
  85. expect(stdout).not.toContain('Removed');
  86. });
  87. it('reports the count when several older versions are present', () => {
  88. for (const v of ['v1.0.0', 'v1.1.0', 'v1.1.1', 'v1.1.2', 'v1.1.3']) seedVersion(installDir, v);
  89. const dest = seedVersion(installDir, 'v1.1.4');
  90. const { code, stdout } = runPrune(installDir, dest);
  91. expect(code).toBe(0);
  92. expect(fs.readdirSync(path.join(installDir, 'versions'))).toEqual(['v1.1.4']);
  93. expect(stdout).toContain('Removed 5 older version(s)');
  94. });
  95. });