|
| 1 | +/** |
| 2 | + * Tests for the `codegraph version` affordances. |
| 3 | + * |
| 4 | + * The version should be reachable however a user reaches for it — the bare |
| 5 | + * `version` subcommand, lowercase `-v`, single-dash `-version`, plus |
| 6 | + * commander's stock `--version` / `-V`. All of them print the exact |
| 7 | + * package.json version and nothing else. |
| 8 | + * |
| 9 | + * Exercised end-to-end against the built binary (same approach as |
| 10 | + * status-json.test.ts) so the spellings survive future CLI refactors. |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, it, expect } from 'vitest'; |
| 14 | +import { execFileSync } from 'child_process'; |
| 15 | +import * as fs from 'fs'; |
| 16 | +import * as os from 'os'; |
| 17 | +import * as path from 'path'; |
| 18 | + |
| 19 | +const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js'); |
| 20 | +const PKG_VERSION = JSON.parse( |
| 21 | + fs.readFileSync(path.resolve(__dirname, '../package.json'), 'utf-8'), |
| 22 | +).version as string; |
| 23 | + |
| 24 | +function run(args: string[]): string { |
| 25 | + return execFileSync(process.execPath, [BIN, ...args], { |
| 26 | + encoding: 'utf-8', |
| 27 | + // Skip the daemon and the wasm-flag re-exec so the command resolves in a |
| 28 | + // single fast process (no graph work happens for a version print anyway). |
| 29 | + env: { ...process.env, CODEGRAPH_NO_DAEMON: '1', CODEGRAPH_WASM_RELAUNCHED: '1' }, |
| 30 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 31 | + }).trim(); |
| 32 | +} |
| 33 | + |
| 34 | +describe('codegraph version affordances', () => { |
| 35 | + for (const spelling of ['version', '-v', '-version', '--version', '-V']) { |
| 36 | + it(`\`codegraph ${spelling}\` prints exactly the package version`, () => { |
| 37 | + expect(run([spelling])).toBe(PKG_VERSION); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + it('lists the `version` subcommand in --help', () => { |
| 42 | + expect(run(['--help'])).toContain('version'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('`codegraph help` prints usage and the command list', () => { |
| 46 | + const out = run(['help']); |
| 47 | + expect(out).toContain('Usage: codegraph'); |
| 48 | + expect(out).toContain('Commands:'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('a trailing `-v` is still the subcommand\'s --verbose, not the version intercept', () => { |
| 52 | + // A fresh temp dir outside any indexed project: `index -v` parses `-v` as |
| 53 | + // the index command's --verbose, then short-circuits at "not initialized" |
| 54 | + // and exits non-zero. The point is it must NOT print the bare version, |
| 55 | + // which would mean the top-level intercept swallowed a subcommand flag. |
| 56 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-version-test-')); |
| 57 | + let combined = ''; |
| 58 | + try { |
| 59 | + combined = execFileSync(process.execPath, [BIN, 'index', '-v', tempDir], { |
| 60 | + encoding: 'utf-8', |
| 61 | + env: { ...process.env, CODEGRAPH_NO_DAEMON: '1', CODEGRAPH_WASM_RELAUNCHED: '1' }, |
| 62 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 63 | + }); |
| 64 | + } catch (err: unknown) { |
| 65 | + const e = err as { stdout?: string; stderr?: string }; |
| 66 | + combined = `${e.stdout ?? ''}${e.stderr ?? ''}`; |
| 67 | + } finally { |
| 68 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 69 | + } |
| 70 | + expect(combined.trim()).not.toBe(PKG_VERSION); |
| 71 | + expect(combined).toContain('not initialized'); |
| 72 | + }); |
| 73 | +}); |
0 commit comments