Skip to content

Commit 5814e10

Browse files
committed
test: cover utf8 file pipe regressions
1 parent c08352c commit 5814e10

4 files changed

Lines changed: 43 additions & 6 deletions

File tree

.changeset/dry-worms-mix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"just-bash": patch
3+
---
4+
5+
Add regression coverage for UTF-8 piped file input.

examples/custom-command/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ This demonstrates how custom commands can:
4646
Use `defineCommand` from just-bash:
4747

4848
```typescript
49-
import { defineCommand } from "just-bash";
49+
import { decodeBytesToUtf8, defineCommand } from "just-bash";
5050

5151
const myCommand = defineCommand("mycommand", async (args, ctx) => {
5252
// args: command arguments (string[])
53-
// ctx: CommandContext with fs, cwd, env, stdin, exec
54-
53+
// ctx.stdin is a ByteString. Decode it before text operations.
54+
const input = decodeBytesToUtf8(ctx.stdin);
55+
5556
return {
56-
stdout: "output here\n",
57+
stdout: input.toUpperCase(),
5758
stderr: "",
5859
exitCode: 0,
5960
};
@@ -75,6 +76,6 @@ Your command receives a context object with:
7576
- `fs` - Virtual filesystem interface
7677
- `cwd` - Current working directory
7778
- `env` - Environment variables
78-
- `stdin` - Standard input (from pipes)
79+
- `stdin` - Standard input from pipes as a `ByteString`; use
80+
`decodeBytesToUtf8(ctx.stdin)` before text parsing or string operations
7981
- `exec` - Function to run subcommands
80-

packages/just-bash/src/commands/jq/jq.utf8-stdin.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,18 @@ describe("jq reads UTF-8 from stdin", () => {
1313
expect(result.exitCode).toBe(0);
1414
expect(result.stdout).toBe("한글 / café / 漢字\n");
1515
});
16+
17+
it("preserves multibyte string values when file input is redirected", async () => {
18+
const env = new Bash({
19+
files: {
20+
"/places.json": JSON.stringify({ name: "Florida — Miami" }),
21+
},
22+
});
23+
24+
const result = await env.exec("jq '.name' /places.json > /out.json");
25+
expect(result.exitCode).toBe(0);
26+
27+
const out = await env.fs.readFile("/out.json", "utf8");
28+
expect(out).toBe('"Florida — Miami"\n');
29+
});
1630
});

packages/just-bash/src/custom-commands.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ describe("custom-commands", () => {
129129
expect(result.exitCode).toBe(0);
130130
});
131131

132+
it("custom command decodes multibyte stdin from a file pipe", async () => {
133+
const capture = defineCommand("capture", async (_args, ctx) => ({
134+
stdout: decodeBytesToUtf8(ctx.stdin),
135+
stderr: "",
136+
exitCode: 0,
137+
}));
138+
139+
const bash = new Bash({
140+
customCommands: [capture],
141+
files: { "/place.json": '{"name":"Florida — Miami"}' },
142+
});
143+
const result = await bash.exec("cat /place.json | capture");
144+
145+
expect(result.stdout).toBe('{"name":"Florida — Miami"}');
146+
expect(result.exitCode).toBe(0);
147+
});
148+
132149
it("custom command can read files via ctx.fs", async () => {
133150
const reader = defineCommand("reader", async (args, ctx) => {
134151
const content = await ctx.fs.readFile(args[0]);

0 commit comments

Comments
 (0)