|
| 1 | +# Comparison Tests |
| 2 | + |
| 3 | +Comparison tests validate that just-bash produces the same output as real bash. They use a **fixture-based system** that records bash outputs once and replays them during tests, eliminating platform-specific differences. |
| 4 | + |
| 5 | +## How It Works |
| 6 | + |
| 7 | +1. **Fixtures** are JSON files containing recorded bash outputs (`src/comparison-tests/fixtures/*.fixtures.json`) |
| 8 | +2. **Tests** run commands in just-bash and compare against the recorded fixtures |
| 9 | +3. **Record mode** runs real bash and saves outputs to fixtures |
| 10 | + |
| 11 | +## Running Tests |
| 12 | + |
| 13 | +```bash |
| 14 | +# Run all comparison tests (uses fixtures, no real bash needed) |
| 15 | +pnpm test:comparison |
| 16 | + |
| 17 | +# Run a specific test file |
| 18 | +pnpm test:run src/comparison-tests/ls.comparison.test.ts |
| 19 | + |
| 20 | +# Re-record fixtures (runs real bash, skips locked fixtures) |
| 21 | +pnpm test:comparison:record |
| 22 | +# Or: RECORD_FIXTURES=1 pnpm test:comparison |
| 23 | + |
| 24 | +# Force re-record ALL fixtures including locked ones |
| 25 | +RECORD_FIXTURES=force pnpm test:comparison |
| 26 | +``` |
| 27 | + |
| 28 | +## Adding New Tests |
| 29 | + |
| 30 | +### 1. Add the test case |
| 31 | + |
| 32 | +```typescript |
| 33 | +// src/comparison-tests/mycommand.comparison.test.ts |
| 34 | +import { afterEach, beforeEach, describe, it } from "vitest"; |
| 35 | +import { |
| 36 | + cleanupTestDir, |
| 37 | + compareOutputs, |
| 38 | + createTestDir, |
| 39 | + setupFiles, |
| 40 | +} from "./test-helpers.js"; |
| 41 | + |
| 42 | +describe("mycommand - Real Bash Comparison", () => { |
| 43 | + let testDir: string; |
| 44 | + |
| 45 | + beforeEach(async () => { |
| 46 | + testDir = await createTestDir(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(async () => { |
| 50 | + await cleanupTestDir(testDir); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should do something", async () => { |
| 54 | + const env = await setupFiles(testDir, { |
| 55 | + "input.txt": "hello world\n", |
| 56 | + }); |
| 57 | + await compareOutputs(env, testDir, "mycommand input.txt"); |
| 58 | + }); |
| 59 | +}); |
| 60 | +``` |
| 61 | + |
| 62 | +### 2. Record the fixture |
| 63 | + |
| 64 | +```bash |
| 65 | +RECORD_FIXTURES=1 pnpm test:run src/comparison-tests/mycommand.comparison.test.ts |
| 66 | +``` |
| 67 | + |
| 68 | +This creates `src/comparison-tests/fixtures/mycommand.comparison.fixtures.json`. |
| 69 | + |
| 70 | +### 3. Commit both the test and fixture file |
| 71 | + |
| 72 | +## Updating Fixtures |
| 73 | + |
| 74 | +When bash behavior changes or you need to update expected outputs: |
| 75 | + |
| 76 | +```bash |
| 77 | +# Re-record specific test file |
| 78 | +RECORD_FIXTURES=1 pnpm test:run src/comparison-tests/ls.comparison.test.ts |
| 79 | + |
| 80 | +# Re-record all fixtures |
| 81 | +pnpm test:comparison:record |
| 82 | +``` |
| 83 | + |
| 84 | +## Handling Platform Differences |
| 85 | + |
| 86 | +The fixture system solves platform differences (macOS vs Linux): |
| 87 | + |
| 88 | +1. **Record once** on any platform |
| 89 | +2. **Manually adjust** the fixture to match desired behavior (usually Linux) |
| 90 | +3. **Lock the fixture** to prevent accidental overwriting |
| 91 | +4. Tests then pass on all platforms |
| 92 | + |
| 93 | +Example: `ls -R` outputs differently on macOS vs Linux: |
| 94 | +- macOS: `dir\nfile.txt\n...` |
| 95 | +- Linux: `.:\ndir\nfile.txt\n...` (includes ".:" header) |
| 96 | + |
| 97 | +We record on macOS, then edit the fixture to use Linux behavior since our implementation follows Linux. |
| 98 | + |
| 99 | +## Locked Fixtures |
| 100 | + |
| 101 | +Fixtures that have been manually adjusted for platform-specific behavior should be marked as **locked** to prevent accidental overwriting when re-recording: |
| 102 | + |
| 103 | +```json |
| 104 | +{ |
| 105 | + "fixture_id": { |
| 106 | + "command": "ls -R", |
| 107 | + "files": { ... }, |
| 108 | + "stdout": ".:\ndir\nfile.txt\n...", |
| 109 | + "stderr": "", |
| 110 | + "exitCode": 0, |
| 111 | + "locked": true |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +When recording: |
| 117 | +- `RECORD_FIXTURES=1` skips locked fixtures and reports them |
| 118 | +- `RECORD_FIXTURES=force` overwrites all fixtures including locked ones |
| 119 | + |
| 120 | +Currently locked fixtures: |
| 121 | +- `ls -R` - Uses Linux-style output with ".:" header |
| 122 | +- `cat -n` with multiple files - Uses continuous line numbering (Linux behavior) |
| 123 | + |
| 124 | +## API Reference |
| 125 | + |
| 126 | +### `setupFiles(testDir, files)` |
| 127 | + |
| 128 | +Sets up test files in both real filesystem and BashEnv. |
| 129 | + |
| 130 | +```typescript |
| 131 | +const env = await setupFiles(testDir, { |
| 132 | + "file.txt": "content", |
| 133 | + "dir/nested.txt": "nested content", |
| 134 | +}); |
| 135 | +``` |
| 136 | + |
| 137 | +### `compareOutputs(env, testDir, command, options?)` |
| 138 | + |
| 139 | +Compares just-bash output against recorded fixture. |
| 140 | + |
| 141 | +```typescript |
| 142 | +// Basic usage |
| 143 | +await compareOutputs(env, testDir, "cat file.txt"); |
| 144 | + |
| 145 | +// With options |
| 146 | +await compareOutputs(env, testDir, "wc -l file.txt", { |
| 147 | + normalizeWhitespace: true, // For BSD/GNU whitespace differences |
| 148 | + compareExitCode: false, // Skip exit code comparison |
| 149 | +}); |
| 150 | +``` |
| 151 | + |
| 152 | +### `runRealBash(command, cwd)` |
| 153 | + |
| 154 | +Runs a command in real bash (for tests that need direct bash access). |
| 155 | + |
| 156 | +```typescript |
| 157 | +const result = await runRealBash("echo hello", testDir); |
| 158 | +// result: { stdout, stderr, exitCode } |
| 159 | +``` |
| 160 | + |
| 161 | +## Fixture File Format |
| 162 | + |
| 163 | +```json |
| 164 | +{ |
| 165 | + "fixture_id_hash": { |
| 166 | + "command": "ls -la", |
| 167 | + "files": { |
| 168 | + "file.txt": "content" |
| 169 | + }, |
| 170 | + "stdout": "file.txt\n", |
| 171 | + "stderr": "", |
| 172 | + "exitCode": 0 |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +The fixture ID is a hash of (command + files), ensuring each unique test case has its own fixture entry. |
| 178 | + |
| 179 | +## Best Practices |
| 180 | + |
| 181 | +1. **Keep tests focused** - One behavior per test |
| 182 | +2. **Use meaningful file content** - Makes debugging easier |
| 183 | +3. **Test edge cases** - Empty files, special characters, etc. |
| 184 | +4. **Use `normalizeWhitespace`** for commands with platform-specific formatting (wc, column widths) |
| 185 | +5. **Commit fixtures** - They're part of the test suite |
| 186 | +6. **Re-record when needed** - If you change test files/commands, re-record the fixtures |
0 commit comments