|
| 1 | +import { describe, test } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | + |
| 4 | +import { parseCliArgs, cliUsage } from './index.mjs'; |
| 5 | + |
| 6 | +describe('pick-branch: CLI args', () => { |
| 7 | + test('defaults: spec=otel, dry-run on when not in Actions', () => { |
| 8 | + const result = parseCliArgs([], {}); |
| 9 | + assert.equal(result.spec, 'otel'); |
| 10 | + assert.equal(result.dryRun, true); |
| 11 | + assert.equal(result.help, false); |
| 12 | + assert.match(result.dryRunReason, /GITHUB_ACTIONS/); |
| 13 | + }); |
| 14 | + |
| 15 | + test('default dry-run is OFF when GITHUB_ACTIONS=true', () => { |
| 16 | + const result = parseCliArgs([], { GITHUB_ACTIONS: 'true' }); |
| 17 | + assert.equal(result.dryRun, false); |
| 18 | + assert.equal(result.dryRunReason, 'GITHUB_ACTIONS=true'); |
| 19 | + }); |
| 20 | + |
| 21 | + test('default dry-run is ON when GITHUB_ACTIONS is set to anything else', () => { |
| 22 | + assert.equal(parseCliArgs([], { GITHUB_ACTIONS: 'false' }).dryRun, true); |
| 23 | + assert.equal(parseCliArgs([], { GITHUB_ACTIONS: '' }).dryRun, true); |
| 24 | + }); |
| 25 | + |
| 26 | + test('--dry-run forces dry-run even under Actions', () => { |
| 27 | + const result = parseCliArgs(['--dry-run'], { GITHUB_ACTIONS: 'true' }); |
| 28 | + assert.equal(result.dryRun, true); |
| 29 | + assert.equal(result.dryRunReason, '--dry-run flag'); |
| 30 | + }); |
| 31 | + |
| 32 | + test('--no-dry-run forces writes even outside Actions', () => { |
| 33 | + const result = parseCliArgs(['--no-dry-run'], {}); |
| 34 | + assert.equal(result.dryRun, false); |
| 35 | + assert.equal(result.dryRunReason, '--no-dry-run flag'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('later --dry-run / --no-dry-run wins', () => { |
| 39 | + assert.equal(parseCliArgs(['--no-dry-run', '--dry-run'], {}).dryRun, true); |
| 40 | + assert.equal(parseCliArgs(['--dry-run', '--no-dry-run'], {}).dryRun, false); |
| 41 | + }); |
| 42 | + |
| 43 | + test('--spec=<id> selects a known spec', () => { |
| 44 | + assert.equal(parseCliArgs(['--spec=semconv'], {}).spec, 'semconv'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('--spec <id> (separate token) selects a known spec', () => { |
| 48 | + assert.equal(parseCliArgs(['--spec', 'semconv'], {}).spec, 'semconv'); |
| 49 | + }); |
| 50 | + |
| 51 | + test('-s <id> short form selects a known spec', () => { |
| 52 | + assert.equal(parseCliArgs(['-s', 'semconv'], {}).spec, 'semconv'); |
| 53 | + }); |
| 54 | + |
| 55 | + test('--help sets help flag without throwing', () => { |
| 56 | + const result = parseCliArgs(['--help'], {}); |
| 57 | + assert.equal(result.help, true); |
| 58 | + }); |
| 59 | + |
| 60 | + test('-h short form sets help flag', () => { |
| 61 | + assert.equal(parseCliArgs(['-h'], {}).help, true); |
| 62 | + }); |
| 63 | + |
| 64 | + test('unknown --spec value throws', () => { |
| 65 | + assert.throws(() => parseCliArgs(['--spec=bogus'], {}), /Unknown --spec/); |
| 66 | + }); |
| 67 | + |
| 68 | + test('unknown flag throws', () => { |
| 69 | + assert.throws(() => parseCliArgs(['--bogus'], {}), /Unknown argument/); |
| 70 | + }); |
| 71 | + |
| 72 | + test('--spec without value throws', () => { |
| 73 | + assert.throws(() => parseCliArgs(['--spec'], {}), /Missing value/); |
| 74 | + }); |
| 75 | + |
| 76 | + test('cliUsage mentions both specs and the dry-run flags', () => { |
| 77 | + const text = cliUsage(); |
| 78 | + assert.match(text, /otel\|semconv/); |
| 79 | + assert.match(text, /--dry-run/); |
| 80 | + assert.match(text, /--no-dry-run/); |
| 81 | + }); |
| 82 | +}); |
0 commit comments