summaryrefslogtreecommitdiff
path: root/cli/tests
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tests')
-rw-r--r--cli/tests/unit/README.md4
-rw-r--r--cli/tests/unit/net_test.ts17
-rw-r--r--cli/tests/unit/test_util.ts47
3 files changed, 52 insertions, 16 deletions
diff --git a/cli/tests/unit/README.md b/cli/tests/unit/README.md
index 0d9e17554..af31c08fc 100644
--- a/cli/tests/unit/README.md
+++ b/cli/tests/unit/README.md
@@ -30,10 +30,10 @@ There are two ways to run `unit_test_runner.ts`:
```sh
# Run all tests.
-target/debug/deno test --allow-all --unstable --location=http://js-unit-tests/foo/bar cli/tests/unit/
+cargo run --bin deno -- test --allow-all --unstable --location=http://js-unit-tests/foo/bar cli/tests/unit/
# Run a specific test module
-target/debug/deno test --allow-all --unstable --location=http://js-unit-tests/foo/bar cli/tests/unit/files_test.ts
+cargo run --bin deno -- test --allow-all --unstable --location=http://js-unit-tests/foo/bar cli/tests/unit/files_test.ts
```
### Http server
diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts
index 8d004f424..b4a21578e 100644
--- a/cli/tests/unit/net_test.ts
+++ b/cli/tests/unit/net_test.ts
@@ -8,6 +8,7 @@ import {
deferred,
delay,
execCode,
+ execCode2,
} from "./test_util.ts";
import { join } from "../../../test_util/std/path/mod.ts";
@@ -853,25 +854,23 @@ Deno.test(
Deno.test(
{ permissions: { read: true, run: true, net: true } },
async function netListenUnrefAndRef() {
- const p = execCode(`
+ const p = execCode2(`
async function main() {
const listener = Deno.listen({ port: 3500 });
listener.unref();
listener.ref(); // This restores 'ref' state of listener
+ console.log("started");
await listener.accept();
console.log("accepted")
}
main();
`);
- // TODO(kt3k): This is racy. Find a correct way to
- // wait for the server to be ready
- setTimeout(async () => {
- const conn = await Deno.connect({ port: 3500 });
- conn.close();
- }, 200);
- const [statusCode, output] = await p;
+ await p.waitStdoutText("started");
+ const conn = await Deno.connect({ port: 3500 });
+ conn.close();
+ const [statusCode, output] = await p.finished();
assertEquals(statusCode, 0);
- assertEquals(output.trim(), "accepted");
+ assertEquals(output.trim(), "started\naccepted");
},
);
diff --git a/cli/tests/unit/test_util.ts b/cli/tests/unit/test_util.ts
index 9a1b13038..ce945c7db 100644
--- a/cli/tests/unit/test_util.ts
+++ b/cli/tests/unit/test_util.ts
@@ -29,16 +29,53 @@ export function pathToAbsoluteFileUrl(path: string): URL {
return new URL(`file://${Deno.build.os === "windows" ? "/" : ""}${path}`);
}
-const decoder = new TextDecoder();
+export function execCode(code: string): Promise<readonly [number, string]> {
+ return execCode2(code).finished();
+}
-export async function execCode(code: string): Promise<[number, string]> {
- const output = await new Deno.Command(Deno.execPath(), {
+export function execCode2(code: string) {
+ const command = new Deno.Command(Deno.execPath(), {
args: [
"eval",
"--unstable",
"--no-check",
code,
],
- }).output();
- return [output.code, decoder.decode(output.stdout)];
+ stdout: "piped",
+ stderr: "inherit",
+ });
+
+ const child = command.spawn();
+ const stdout = child.stdout.pipeThrough(new TextDecoderStream()).getReader();
+ let output = "";
+
+ return {
+ async waitStdoutText(text: string) {
+ while (true) {
+ const readData = await stdout.read();
+ if (readData.value) {
+ output += readData.value;
+ if (output.includes(text)) {
+ return;
+ }
+ }
+ if (readData.done) {
+ throw new Error(`Did not find text '${text}' in stdout.`);
+ }
+ }
+ },
+ async finished() {
+ while (true) {
+ const readData = await stdout.read();
+ if (readData.value) {
+ output += readData.value;
+ }
+ if (readData.done) {
+ break;
+ }
+ }
+ const status = await child.status;
+ return [status.code, output] as const;
+ },
+ };
}