Skip to content

Commit eed9a58

Browse files
committed
More commands
1 parent 9b77cdb commit eed9a58

33 files changed

+3295
-375
lines changed

AGENTS.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ Usage: pnpm dev:exec '<bash script>'
88
cat script.sh | pnpm dev:exec
99
```
1010

11+
**Important**: When using `pnpm dev:exec` with command-line arguments, pnpm converts single quotes to double quotes, which causes shell variables like `$?` to be expanded by the host shell before reaching BashEnv. To test scripts containing `$?` or other shell variables, use stdin instead:
12+
13+
```bash
14+
# WRONG - $? gets expanded by host shell to 0:
15+
pnpm dev:exec 'false; echo $?'
16+
17+
# CORRECT - use stdin to avoid shell expansion:
18+
echo 'false; echo $?' | pnpm dev:exec
19+
```
20+
1121
- Install packages via pnpm rather than editing package.json directly
1222
- Bias towards making new test files that are roughly logically grouped rather than letting test files gets too large. Try to stay below 300 lines. Prefer making a new file when you want to add a `describe()`
1323
- Prefer asserting the full STDOUT/STDERR output rather than using to.contain or to.not.contain
@@ -24,3 +34,11 @@ Usage: pnpm dev:exec '<bash script>'
2434
- Always make sure to build before using dist
2535
- Biome rules often have the same name as eslint rules (if you are lookinf for one)
2636
- Error / show usage on unknown flags in commands and built-ins (unless real bash also ignores)
37+
- Dependencies that use wasm are not allowed. Binary npm packages are fine
38+
39+
## Commands
40+
41+
- Must have usage statement
42+
- Must error on unknown options (unless bash ignores them)
43+
- Must have extensive unit tests collocated with the command
44+
- Should have comparison tests if there is doubt about behavior

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"vitest": "^4.0.16"
4242
},
4343
"dependencies": {
44+
"diff": "^8.0.2",
4445
"minimatch": "^10.1.1",
4546
"sprintf-js": "^1.1.3"
4647
}

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BashEnv.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ export class BashEnv {
7676
lastExitCode: 0,
7777
options: {
7878
errexit: false,
79+
pipefail: false,
7980
},
8081
inCondition: false,
82+
loopDepth: 0,
8183
};
8284

8385
// Create essential directories for VirtualFs (only for default layout)

src/commands/base64/base64.test.ts

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import { describe, expect, it } from "vitest";
2+
import { BashEnv } from "../../BashEnv.js";
3+
4+
describe("base64", () => {
5+
describe("encoding", () => {
6+
it("should encode string from stdin", async () => {
7+
const env = new BashEnv();
8+
const result = await env.exec('echo -n "hello" | base64');
9+
expect(result.stdout).toBe("aGVsbG8=\n");
10+
expect(result.stderr).toBe("");
11+
expect(result.exitCode).toBe(0);
12+
});
13+
14+
it("should encode string with newline", async () => {
15+
const env = new BashEnv();
16+
const result = await env.exec('echo "hello" | base64');
17+
expect(result.stdout).toBe("aGVsbG8K\n");
18+
expect(result.stderr).toBe("");
19+
expect(result.exitCode).toBe(0);
20+
});
21+
22+
it("should encode file contents", async () => {
23+
const env = new BashEnv({
24+
files: { "/test.txt": "hello" },
25+
});
26+
const result = await env.exec("base64 /test.txt");
27+
expect(result.stdout).toBe("aGVsbG8=\n");
28+
expect(result.stderr).toBe("");
29+
expect(result.exitCode).toBe(0);
30+
});
31+
32+
it("should wrap lines at 76 characters by default", async () => {
33+
const env = new BashEnv();
34+
// Long input that will produce > 76 chars of base64
35+
const result = await env.exec(
36+
'echo -n "This is a very long string that will definitely produce more than 76 characters of base64 output" | base64',
37+
);
38+
const lines = result.stdout.split("\n").filter((l) => l);
39+
expect(lines[0].length).toBeLessThanOrEqual(76);
40+
expect(result.exitCode).toBe(0);
41+
});
42+
43+
it("should not wrap with -w 0", async () => {
44+
const env = new BashEnv();
45+
const result = await env.exec(
46+
'echo -n "This is a very long string that will definitely produce more than 76 characters of base64 output" | base64 -w 0',
47+
);
48+
// Should be single line (no newline wrapping, just trailing newline)
49+
expect(result.stdout).not.toContain("\n\n");
50+
expect(result.exitCode).toBe(0);
51+
});
52+
53+
it("should wrap at custom width", async () => {
54+
const env = new BashEnv();
55+
const result = await env.exec(
56+
'echo -n "hello world test" | base64 -w 10',
57+
);
58+
const lines = result.stdout.split("\n").filter((l) => l);
59+
for (const line of lines.slice(0, -1)) {
60+
expect(line.length).toBe(10);
61+
}
62+
expect(result.exitCode).toBe(0);
63+
});
64+
});
65+
66+
describe("decoding", () => {
67+
it("should decode base64 with -d", async () => {
68+
const env = new BashEnv();
69+
const result = await env.exec('echo "aGVsbG8=" | base64 -d');
70+
expect(result.stdout).toBe("hello");
71+
expect(result.stderr).toBe("");
72+
expect(result.exitCode).toBe(0);
73+
});
74+
75+
it("should decode base64 with --decode", async () => {
76+
const env = new BashEnv();
77+
const result = await env.exec('echo "aGVsbG8=" | base64 --decode');
78+
expect(result.stdout).toBe("hello");
79+
expect(result.stderr).toBe("");
80+
expect(result.exitCode).toBe(0);
81+
});
82+
83+
it("should decode base64 from file", async () => {
84+
const env = new BashEnv({
85+
files: { "/encoded.txt": "aGVsbG8gd29ybGQ=" },
86+
});
87+
const result = await env.exec("base64 -d /encoded.txt");
88+
expect(result.stdout).toBe("hello world");
89+
expect(result.stderr).toBe("");
90+
expect(result.exitCode).toBe(0);
91+
});
92+
93+
it("should ignore whitespace when decoding", async () => {
94+
const env = new BashEnv();
95+
const result = await env.exec('echo "aGVs\nbG8=" | base64 -d');
96+
expect(result.stdout).toBe("hello");
97+
expect(result.exitCode).toBe(0);
98+
});
99+
100+
it("should roundtrip encode then decode", async () => {
101+
const env = new BashEnv();
102+
const result = await env.exec(
103+
'echo -n "test data 123" | base64 | base64 -d',
104+
);
105+
expect(result.stdout).toBe("test data 123");
106+
expect(result.exitCode).toBe(0);
107+
});
108+
});
109+
110+
describe("error handling", () => {
111+
it("should error on missing file", async () => {
112+
const env = new BashEnv();
113+
const result = await env.exec("base64 /nonexistent.txt");
114+
expect(result.stdout).toBe("");
115+
expect(result.stderr).toBe(
116+
"base64: /nonexistent.txt: No such file or directory\n",
117+
);
118+
expect(result.exitCode).toBe(1);
119+
});
120+
121+
it("should error on unknown option", async () => {
122+
const env = new BashEnv();
123+
const result = await env.exec("base64 --unknown");
124+
expect(result.stderr).toContain("unrecognized option");
125+
expect(result.exitCode).toBe(1);
126+
});
127+
128+
it("should error on unknown short option", async () => {
129+
const env = new BashEnv();
130+
const result = await env.exec("base64 -z");
131+
expect(result.stderr).toContain("invalid option");
132+
expect(result.exitCode).toBe(1);
133+
});
134+
});
135+
136+
describe("help", () => {
137+
it("should show help with --help", async () => {
138+
const env = new BashEnv();
139+
const result = await env.exec("base64 --help");
140+
expect(result.stdout).toContain("base64");
141+
expect(result.stdout).toContain("decode");
142+
expect(result.exitCode).toBe(0);
143+
});
144+
});
145+
146+
describe("stdin placeholder", () => {
147+
it("should read from stdin with -", async () => {
148+
const env = new BashEnv();
149+
const result = await env.exec('echo -n "test" | base64 -');
150+
expect(result.stdout).toBe("dGVzdA==\n");
151+
expect(result.exitCode).toBe(0);
152+
});
153+
});
154+
});

src/commands/base64/base64.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* base64 - Encode or decode base64
3+
*/
4+
5+
import type { Command, CommandContext, ExecResult } from "../../types.js";
6+
import { hasHelpFlag, showHelp, unknownOption } from "../help.js";
7+
8+
const base64Help = {
9+
name: "base64",
10+
summary: "base64 encode/decode data and print to standard output",
11+
usage: "base64 [OPTION]... [FILE]",
12+
options: [
13+
"-d, --decode decode data",
14+
"-w, --wrap=COLS wrap encoded lines after COLS character (default 76, 0 to disable)",
15+
" --help display this help and exit",
16+
],
17+
};
18+
19+
export const base64Command: Command = {
20+
name: "base64",
21+
22+
async execute(args: string[], ctx: CommandContext): Promise<ExecResult> {
23+
if (hasHelpFlag(args)) {
24+
return showHelp(base64Help);
25+
}
26+
27+
let decode = false;
28+
let wrapCols = 76;
29+
const files: string[] = [];
30+
31+
for (let i = 0; i < args.length; i++) {
32+
const arg = args[i];
33+
if (arg === "-d" || arg === "--decode") {
34+
decode = true;
35+
} else if (arg === "-w" || arg === "--wrap") {
36+
wrapCols = Number.parseInt(args[++i] ?? "76", 10) || 0;
37+
} else if (arg.startsWith("--wrap=")) {
38+
wrapCols = Number.parseInt(arg.slice(7), 10) || 0;
39+
} else if (arg.startsWith("-w")) {
40+
wrapCols = Number.parseInt(arg.slice(2), 10) || 0;
41+
} else if (arg === "-") {
42+
files.push("-");
43+
} else if (arg.startsWith("--")) {
44+
return unknownOption("base64", arg);
45+
} else if (arg.startsWith("-")) {
46+
for (const c of arg.slice(1)) {
47+
if (c === "d") decode = true;
48+
else return unknownOption("base64", `-${c}`);
49+
}
50+
} else {
51+
files.push(arg);
52+
}
53+
}
54+
55+
let input: string;
56+
if (files.length === 0 || (files.length === 1 && files[0] === "-")) {
57+
input = ctx.stdin;
58+
} else {
59+
try {
60+
const filePath = ctx.fs.resolvePath(ctx.cwd, files[0]);
61+
input = await ctx.fs.readFile(filePath);
62+
} catch {
63+
return {
64+
stdout: "",
65+
stderr: `base64: ${files[0]}: No such file or directory\n`,
66+
exitCode: 1,
67+
};
68+
}
69+
}
70+
71+
try {
72+
if (decode) {
73+
const cleaned = input.replace(/\s/g, "");
74+
const decoded = Buffer.from(cleaned, "base64").toString("utf-8");
75+
return { stdout: decoded, stderr: "", exitCode: 0 };
76+
}
77+
let encoded = Buffer.from(input).toString("base64");
78+
if (wrapCols > 0) {
79+
const lines: string[] = [];
80+
for (let i = 0; i < encoded.length; i += wrapCols) {
81+
lines.push(encoded.slice(i, i + wrapCols));
82+
}
83+
encoded = lines.join("\n") + (encoded.length > 0 ? "\n" : "");
84+
}
85+
return { stdout: encoded, stderr: "", exitCode: 0 };
86+
} catch {
87+
return { stdout: "", stderr: "base64: invalid input\n", exitCode: 1 };
88+
}
89+
},
90+
};

0 commit comments

Comments
 (0)