Skip to content

Commit

Permalink
fixed a test
Browse files Browse the repository at this point in the history
  • Loading branch information
j50n committed Jun 6, 2023
1 parent 37ddd7e commit 4303764
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions tests/runnable/stderr-handling.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExitCodeError, run, runnable, toLines } from "../../mod3.ts";
import { assertEquals } from "../deps/asserts.ts";
import { assert, assertEquals, fail } from "../deps/asserts.ts";
import { gray } from "../deps/colors.ts";

Deno.test({
Expand Down Expand Up @@ -53,23 +53,27 @@ export class TestError extends Error {
Deno.test({
name: "I can throw custom errors using data from stderr.",
async fn() {
const result = await run(
{
fnStderr: async (input: AsyncIterable<string[]>): Promise<string[]> => {
/* This supresses print of the stderr data. */
return await runnable(input).flatten().collect();
},
fnError: (error?: Error, stderrData?: string[]) => {
if (error != null && error instanceof ExitCodeError) {
throw new TestError("Something went wrong.", stderrData!, {
cause: error,
});
}
const result: string[] = [];
try {
const output = run(
{
fnStderr: async (
input: AsyncIterable<string[]>,
): Promise<string[]> => {
/* This supresses print of the stderr data. */
return await runnable(input).flatten().collect();
},
fnError: (error?: Error, stderrData?: string[]) => {
if (error != null && error instanceof ExitCodeError) {
throw new TestError("Something went wrong.", stderrData!, {
cause: error,
});
}
},
},
},
"bash",
"-c",
`
"bash",
"-c",
`
set -e
echo "excelsior" 1>&2
Expand All @@ -80,12 +84,32 @@ Deno.test({
exit 7
`,
)
.transform(toLines)
.flatten()
.collect();
)
.transform(toLines)
.flatten();

for await (const line of output) {
result.push(line);
}

fail("The iterable should error out and never reach this line.");
} catch (e) {
assert(e instanceof TestError, "I should see the error I threw.");
assert(
e.message === "Something went wrong.",
"It's the right error message.",
);
assertEquals(
e.data,
["excelsior"],
"I captured the data from stderr to the error.",
);
assert(
e.cause instanceof ExitCodeError,
"The cause is passed along too.",
);
}

//assertEquals(stderr, ["excelsior"], "I can get lines from stderr.");
assertEquals(result, ["A", "B", "C"], "I can get lines from a process.");
},
});

0 comments on commit 4303764

Please sign in to comment.