Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setUp and tearDown to simplify tests. #108

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ test(function t2() {
/** A more complicated test that runs a subprocess. */
test(async function catSmoke() {
const p = run({
args: ["deno", "examples/cat.ts", "README.md"]
args: ["deno", "examples/cat.ts", "README.md"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're fixing this, can we cat a smaller file? I've noticed this is flooding the log

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The piped stops it flooding the log.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

stdout: "piped"
});
const s = await p.status();
assertEqual(s.code, 0);
Expand Down
90 changes: 44 additions & 46 deletions http/file_server_test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
import { readFile } from "deno";
import { readFile, run } from "deno";

import { test, assert, assertEqual } from "../testing/mod.ts";
import { test, assert, assertEqual, setUp, tearDown } from "../testing/mod.ts";
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";

// Promise to completeResolve when all tests completes
let completeResolve;
export const completePromise = new Promise(res => (completeResolve = res));
let completedTestCount = 0;

function maybeCompleteTests() {
completedTestCount++;
// Change this when adding more tests
if (completedTestCount === 3) {
completeResolve();
}
}

export function runTests(serverReadyPromise: Promise<any>) {
test(async function serveFile() {
await serverReadyPromise;
const res = await fetch("http://localhost:4500/azure-pipelines.yml");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEqual(res.headers.get("content-type"), "text/yaml; charset=utf-8");
const downloadedFile = await res.text();
const localFile = new TextDecoder().decode(
await readFile("./azure-pipelines.yml")
);
assertEqual(downloadedFile, localFile);
maybeCompleteTests();
let fileServer;
setUp(async function startFileServer() {
fileServer = run({
args: ["deno", "--allow-net", "http/file_server.ts", ".", "--cors"],
stdout: "piped"
});
// Once fileServer is ready it will write to its stdout.
const r = new TextProtoReader(new BufReader(fileServer.stdout));
const [s, err] = await r.readLine();
assert(err == null);
assert(s.includes("server listening"));
});
tearDown(function killFileServer() {
fileServer.close();
fileServer.stdout.close();
});

test(async function serveDirectory() {
await serverReadyPromise;
const res = await fetch("http://localhost:4500/");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
const page = await res.text();
assert(page.includes("azure-pipelines.yml"));
maybeCompleteTests();
});
test(async function serveFile() {
const res = await fetch("http://localhost:4500/azure-pipelines.yml");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEqual(res.headers.get("content-type"), "text/yaml; charset=utf-8");
const downloadedFile = await res.text();
const localFile = new TextDecoder().decode(
await readFile("./azure-pipelines.yml")
);
assertEqual(downloadedFile, localFile);
});

test(async function serveFallback() {
await serverReadyPromise;
const res = await fetch("http://localhost:4500/badfile.txt");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEqual(res.status, 404);
maybeCompleteTests();
});
}
test(async function serveDirectory() {
const res = await fetch("http://localhost:4500/");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
const page = await res.text();
assert(page.includes("azure-pipelines.yml"));
});

test(async function serveFallback() {
const res = await fetch("http://localhost:4500/badfile.txt");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEqual(res.status, 404);
});
15 changes: 3 additions & 12 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,11 @@ import "fs/path/resolve_test.ts";
import "fs/path/zero_length_strings_test.ts";
import "io/bufio_test.ts";
import "http/http_test.ts";
import "http/file_server_test.ts";
import "log/test.ts";
import "media_types/test.ts";
import "testing/test.ts";
import "textproto/test.ts";
import "ws/ioutil_test.ts";
import "ws/sha1_test.ts";
import "ws/test.ts";

import { runTests, completePromise } from "http/file_server_test.ts";

const fileServer = run({
args: ["deno", "--allow-net", "http/file_server.ts", ".", "--cors"]
});

runTests(new Promise(res => setTimeout(res, 5000)));
(async () => {
await completePromise;
fileServer.close();
})();
20 changes: 20 additions & 0 deletions testing/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { args } from "deno";
import { runTests, cancelTests, setFilter } from "./mod.ts";
import { parse } from "../flags/mod.ts";
import { resolve } from "../fs/path.ts";

cancelTests();

const a = parse(args);
const filter = a.grep || ".*";
// TODO support globs and recursive.
const test_files = a._.slice(1).map(p => resolve(p));

setFilter(filter);

(async () => {
for (let fn of test_files) {
await import(fn);
}
await runTests();
})();
26 changes: 22 additions & 4 deletions testing/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,20 @@ export const exitOnFail = true;

let filterRegExp: RegExp | null;
const tests: TestDefinition[] = [];
const setUps: TestFunction[] = [];
const tearDowns: TestFunction[] = [];

let filtered = 0;
const ignored = 0;
const measured = 0;

export function setUp(t: TestFunction): void {
setUps.push(t);
}
export function tearDown(t: TestFunction): void {
tearDowns.push(t);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just fix timer for starting fileServer in this PR and leave out the setUP and tearDown stuff?

Because modifying testing will effect all of deno, this needs to be considered very carefully. I'm not sure I agree with setting a global setUp/tearDown.

Please just manually do the setUP and tearDown for each of the fileServer tests with the new initialization. After that's landed let's discuss these testing changes (in gitter if you have time)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I agree with setting a global setUp/tearDown.

I would prefer this was per-file, but am not sure on the trick to make this work.

I also want to improve this across all of deno / have it considered carefully.

I could tease it apart:

  • timing of fileserver
  • main.ts for testing
  • setUp + tearDown (to discuss)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timing of fileserver

Let's do just this in this PR.

main.ts for testing

Yeah. I think we should get rid of the "automatic" run behavior that we currently have
https://github.com/denoland/deno_std/blob/b99d7d3e0f61216db7b5a5ad2bc254f8ac97ff4d/testing/mod.ts#L166
and rather require users to call runTests() explicitly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hayd I'm very interested in having your improvements here. I do think they should be split up into different PRs tho. For simplicity, I'm going to close this one.


// Must be called before any test() that needs to be filtered.
export function setFilter(s: string): void {
filterRegExp = new RegExp(s, "i");
Expand Down Expand Up @@ -117,13 +126,16 @@ function green_ok() {
return FG_GREEN + "ok" + RESET;
}

async function runTests() {
export async function runTests() {
let passed = 0;
let failed = 0;

for (let s of setUps) {
await s();
}
console.log("running", tests.length, "tests");
for (let i = 0; i < tests.length; i++) {
const { fn, name } = tests[i];
for (let t of tests) {
const { fn, name } = t;
let result = green_ok();
// See https://github.com/denoland/deno/pull/1452
// about this usage of groupCollapsed
Expand All @@ -144,6 +156,9 @@ async function runTests() {
}
}
}
for (let t of tearDowns) {
await t();
}

// Attempting to match the output of Rust's test runner.
const result = failed > 0 ? red_failed() : green_ok();
Expand All @@ -163,4 +178,7 @@ async function runTests() {
}
}

setTimeout(runTests, 0);
const t = setTimeout(runTests, 0);
export function cancelTests() {
clearTimeout(t);
}
2 changes: 1 addition & 1 deletion io/ioutil.ts → ws/ioutil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { BufReader } from "./bufio.ts";
import { BufReader } from "../io/bufio.ts";

/* Read big endian 16bit short from BufReader */
export async function readShort(buf: BufReader): Promise<number> {
Expand Down
2 changes: 1 addition & 1 deletion io/ioutil_test.ts → ws/ioutil_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Reader, ReadResult } from "deno";
import { assertEqual, test } from "../testing/mod.ts";
import { readInt, readLong, readShort, sliceLongToBytes } from "./ioutil.ts";
import { BufReader } from "./bufio.ts";
import { BufReader } from "../io/bufio.ts";

class BinaryReader implements Reader {
index = 0;
Expand Down
4 changes: 2 additions & 2 deletions ws/mod.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { Buffer, Writer, Conn } from "deno";
import { ServerRequest } from "../http/http.ts";
import { ServerRequest } from "../http/mod.ts";
import { BufReader, BufWriter } from "../io/bufio.ts";
import { readLong, readShort, sliceLongToBytes } from "../io/ioutil.ts";
import { readLong, readShort, sliceLongToBytes } from "./ioutil.ts";
import { Sha1 } from "./sha1.ts";

export const OpCodeContinue = 0x0;
Expand Down
2 changes: 1 addition & 1 deletion ws/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
readFrame,
unmask
} from "./mod.ts";
import { serve } from "../http/http.ts";
import { serve } from "../http/mod.ts";

test(async function testReadUnmaskedTextFrame() {
// unmasked single text frame with payload "Hello"
Expand Down