dsherret / dax

Cross-platform shell tools for Deno and Node.js inspired by zx.
MIT License
1.02k stars 34 forks source link

fix: empty strings were ignored #195

Closed Ryooooooga closed 9 months ago

Ryooooooga commented 10 months ago

deno eval '' should succeed and output nothing.

await $`deno eval ''`;
// error: the following required arguments were not provided:
// ...

test

#!/bin/sh
# ./test.sh: Output the number of arguments ($#) and the value of each argument
printf '$#: %d\n' $#

for i in "$@"; do
  printf '  [%s]\n' "$i"
done
import $ from "https://deno.land/x/dax/mod.ts";
import $2 from "./mod.ts"; // this branch

Deno.env.set("X", "yyy      zzzz");

console.log("master:");
await $`./test.sh`.printCommand();
await $`./test.sh A 'B' "C"`.printCommand();
await $`./test.sh A "" ""`.printCommand();
await $`./test.sh $X"$X"'$X'`.printCommand();

console.log("\n\nPR:");
await $2`./test.sh`.printCommand();
await $2`./test.sh A 'B' "C"`.printCommand();
await $2`./test.sh A '' ""`.printCommand();
await $2`./test.sh $X"$X"'$X'`.printCommand();

console.log("\n\nBash:");

await runBash(`./test.sh`);
await runBash(`./test.sh A 'B' "C"`);
await runBash(`./test.sh A '' ""`);
await runBash(`./test.sh $X"$X"'$X'`);

async function runBash(cmd: string) {
  console.log(`> bash -c ${cmd}`);
  await new Deno.Command("bash", {
    args: ["-c", cmd],
    stdout: "inherit",
  }).spawn();
}
> ./test.sh
$#: 0
> ./test.sh A 'B' "C"
$#: 3
  [A]
  [B]
  [C]
> ./test.sh A "" ""
$#: 1
  [A]
> ./test.sh $X"$X"'$X'
$#: 2
  [yyy]
  [zzzzyyy zzzz$X]

PR:
> ./test.sh
$#: 0
> ./test.sh A 'B' "C"
$#: 3
  [A]
  [B]
  [C]
> ./test.sh A '' ""
$#: 3
  [A]
  []
  []
> ./test.sh $X"$X"'$X'
$#: 2
  [yyy]
  [zzzzyyy      zzzz$X]

Bash:
> bash -c ./test.sh
> bash -c ./test.sh A 'B' "C"
$#: 0
> bash -c ./test.sh A '' ""
$#: 3
  [A]
  [B]
  [C]
> bash -c ./test.sh $X"$X"'$X'
$#: 3
  [A]
  []
  []
$#: 2
  [yyy]
  [zzzzyyy      zzzz$X]