Skip to content

Commit

Permalink
Error if providing an unsupported redirect.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 28, 2024
1 parent 8ae893b commit 3fbd36e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
15 changes: 15 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,14 @@ Deno.test("output redirects", async () => {
assertEquals(result.code, 1);
assert(result.stderr.startsWith("failed opening file for redirect"));
}

{
assertThrows(
() => $`echo 1 > ${new TextEncoder()}`,
Error,
"Failed resolving expression in command. Unsupported object provided to output redirect.",
);
}
});
});

Expand All @@ -1158,6 +1166,13 @@ Deno.test("input redirects", async () => {
const text = await $`cat - < test.txt`.text();
assertEquals(text, "Hi!");
});
{
assertThrows(
() => $`cat - < ${new TextEncoder()} && echo ${"test"}`,
Error,
"Failed resolving expression 1/2 in command. Unsupported object provided to input redirect.",
);
}
});

Deno.test("input redirects with readable", async () => {
Expand Down
15 changes: 10 additions & 5 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1277,8 +1277,10 @@ function templateInner(
},
});
});
} else {
} else if (typeof expr === "string" || expr instanceof PathRef) {
text += templateLiteralExprToString(expr, escape);
} else {
throw new Error("Unsupported object provided to input redirect.");
}
} else if (inputOrOutputRedirect === ">") {
if (expr instanceof WritableStream) {
Expand All @@ -1294,16 +1296,19 @@ function templateInner(
}
return stream;
});
} else {
} else if (typeof expr === "string" || expr instanceof PathRef) {
text += templateLiteralExprToString(expr, escape);
} else {
throw new Error("Unsupported object provided to output redirect.");
}
} else {
text += templateLiteralExprToString(expr, escape);
}
} catch (err) {
throw new Error(`Failed resolving expression ${i + 1}/${exprs.length}.`, {
cause: err,
});
const startMessage = exprs.length === 1
? "Failed resolving expression in command."
: `Failed resolving expression ${i + 1}/${exprs.length} in command.`;
throw new Error(`${startMessage} ${err?.message ?? err}`);
}
}
}
Expand Down

0 comments on commit 3fbd36e

Please sign in to comment.