Skip to content

Commit

Permalink
feat: basic redirect support (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 26, 2024
1 parent 34e8ed2 commit da97144
Show file tree
Hide file tree
Showing 6 changed files with 3,425 additions and 3,212 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"tasks": {
"test": "deno test -A",
"wasmbuild": "deno run -A https://deno.land/x/wasmbuild@0.15.4/main.ts --sync --out ./src/lib"
"wasmbuild": "deno run -A https://deno.land/x/wasmbuild@0.15.6/main.ts --sync --out ./src/lib"
},
"fmt": {
"proseWrap": "preserve",
Expand Down
56 changes: 56 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,62 @@ Deno.test("command .lines()", async () => {
assertEquals(result, ["1", "2"]);
});

Deno.test("redirects", async () => {
await withTempDir(async (tempDir) => {
// absolute
const tempFile = tempDir.join("temp_file.txt");
await $`echo 1 > ${tempFile}`;
assertEquals(tempFile.readTextSync(), "1\n");
tempFile.removeSync();

// relative
await $`echo 2 > ./temp_file.txt`;
assertEquals(tempFile.readTextSync(), "2\n");

// changing directories then relative
await $`mkdir sub_dir && cd sub_dir && echo 3 > ./temp_file.txt`;
assertEquals(tempDir.join("sub_dir/temp_file.txt").readTextSync(), "3\n");

// stderr
await $`deno eval 'console.log(2); console.error(5);' 2> ./temp_file.txt`;
assertEquals(tempFile.readTextSync(), "5\n");

// append
await $`deno eval 'console.error(1);' 2> ./temp_file.txt && echo 2 >> ./temp_file.txt && echo 3 >> ./temp_file.txt`;
assertEquals(tempFile.readTextSync(), "1\n2\n3\n");

// /dev/null
assertEquals(await $`echo 1 > /dev/null`.text(), "");
assertEquals(await $`deno eval 'console.error(1); console.log(2)' 2> /dev/null`.text(), "2");

// not supported fd
{
const result = await $`echo 1 3> file.txt`.noThrow().stderr("piped");
assertEquals(result.code, 1);
assertEquals(result.stderr, "only redirecting to stdout (1) and stderr (2) is supported\n");
}

// multiple words
{
const result = await $`echo 1 > $var`.env("var", "testing this").noThrow().stderr("piped");
assertEquals(result.code, 1);
assertEquals(
result.stderr,
'redirect path must be 1 argument, but found 2 (testing this). Did you mean to quote it (ex. "testing this")?\n',
);
}

// piping to a directory
{
const dir = tempDir.join("dir");
dir.mkdirSync();
const result = await $`echo 1 > ${dir}`.noThrow().stderr("piped");
assertEquals(result.code, 1);
assert(result.stderr.startsWith("failed opening file for redirect"));
}
});
});

Deno.test("shebang support", async (t) => {
await withTempDir(async (dir) => {
const steps: Promise<boolean>[] = [];
Expand Down
Loading

0 comments on commit da97144

Please sign in to comment.