|
| 1 | +const { execSync } = require("node:child_process"); |
| 2 | + |
| 3 | +function runCommand(command) { |
| 4 | + return execSync(command, { encoding: "utf-8" }).trim(); |
| 5 | +} |
| 6 | + |
| 7 | +function normalizeOutput(output) { |
| 8 | + return output.replaceAll(/\s+/g, " ").trim(); |
| 9 | +} |
| 10 | + |
| 11 | +function test(name, systemCommand, nodeCommand) { |
| 12 | + try { |
| 13 | + const expected = normalizeOutput(runCommand(systemCommand)); |
| 14 | + const actual = normalizeOutput(runCommand(nodeCommand)); |
| 15 | + if (expected === actual) { |
| 16 | + console.log(`✅ ${name} passed`); |
| 17 | + } else { |
| 18 | + console.error(`❌ ${name} failed`); |
| 19 | + console.error(`Expected: "${expected}"`); |
| 20 | + console.error(`Actual: "${actual}"`); |
| 21 | + } |
| 22 | + } catch (error) { |
| 23 | + console.error(`❌ ${name} failed with error: ${error.message}`); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/* CAT TESTS */ |
| 28 | +test( |
| 29 | + "cat single file", |
| 30 | + "cat implement-shell-tools/cat/sample-files/1.txt", |
| 31 | + "node implement-shell-tools/cat/cat.js implement-shell-tools/cat/sample-files/1.txt", |
| 32 | +); |
| 33 | + |
| 34 | +test( |
| 35 | + "cat -n single file", |
| 36 | + "cat -n implement-shell-tools/cat/sample-files/1.txt", |
| 37 | + "node implement-shell-tools/cat/cat.js -n implement-shell-tools/cat/sample-files/1.txt", |
| 38 | +); |
| 39 | + |
| 40 | +test( |
| 41 | + "cat multiple files", |
| 42 | + "cat implement-shell-tools/cat/sample-files/*.txt", |
| 43 | + "node implement-shell-tools/cat/cat.js implement-shell-tools/cat/sample-files/*.txt", |
| 44 | +); |
| 45 | + |
| 46 | +test( |
| 47 | + "cat -n multiple files", |
| 48 | + "cat -n implement-shell-tools/cat/sample-files/*.txt", |
| 49 | + "node implement-shell-tools/cat/cat.js -n implement-shell-tools/cat/sample-files/*.txt", |
| 50 | +); |
| 51 | + |
| 52 | +test( |
| 53 | + "cat -b file", |
| 54 | + "cat -b implement-shell-tools/cat/sample-files/3.txt", |
| 55 | + "node implement-shell-tools/cat/cat.js -b implement-shell-tools/cat/sample-files/3.txt", |
| 56 | +); |
| 57 | + |
| 58 | +/* LS TESTS */ |
| 59 | + |
| 60 | +test( |
| 61 | + "ls sample-files", |
| 62 | + "ls implement-shell-tools/ls/sample-files", |
| 63 | + "node implement-shell-tools/ls/ls.js implement-shell-tools/ls/sample-files", |
| 64 | +); |
| 65 | + |
| 66 | +/* WC TESTS */ |
| 67 | + |
| 68 | +test( |
| 69 | + "wc all files", |
| 70 | + "wc implement-shell-tools/wc/sample-files/*", |
| 71 | + "node implement-shell-tools/wc/wc.js implement-shell-tools/wc/sample-files/*", |
| 72 | +); |
| 73 | + |
| 74 | +test( |
| 75 | + "wc -l single file", |
| 76 | + "wc -l implement-shell-tools/wc/sample-files/3.txt", |
| 77 | + "node implement-shell-tools/wc/wc.js -l implement-shell-tools/wc/sample-files/3.txt", |
| 78 | +); |
| 79 | + |
| 80 | +test( |
| 81 | + "wc -w single file", |
| 82 | + "wc -w implement-shell-tools/wc/sample-files/3.txt", |
| 83 | + "node implement-shell-tools/wc/wc.js -w implement-shell-tools/wc/sample-files/3.txt", |
| 84 | +); |
| 85 | + |
| 86 | +test( |
| 87 | + "wc -c single file", |
| 88 | + "wc -c implement-shell-tools/wc/sample-files/3.txt", |
| 89 | + "node implement-shell-tools/wc/wc.js -c implement-shell-tools/wc/sample-files/3.txt", |
| 90 | +); |
| 91 | + |
| 92 | +test( |
| 93 | + "wc -l multiple files", |
| 94 | + "wc -l implement-shell-tools/wc/sample-files/*", |
| 95 | + "node implement-shell-tools/wc/wc.js -l implement-shell-tools/wc/sample-files/*", |
| 96 | +); |
0 commit comments