chore(test): extract shared start server code (#1250)
Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
+7
-63
@@ -8,8 +8,8 @@ import {
|
|||||||
delay,
|
delay,
|
||||||
puppeteer,
|
puppeteer,
|
||||||
retry,
|
retry,
|
||||||
TextLineStream,
|
|
||||||
} from "./deps.ts";
|
} from "./deps.ts";
|
||||||
|
import { startFreshServer } from "./test_utils.ts";
|
||||||
|
|
||||||
type FileTree = {
|
type FileTree = {
|
||||||
type: "file";
|
type: "file";
|
||||||
@@ -112,29 +112,10 @@ Deno.test({
|
|||||||
});
|
});
|
||||||
|
|
||||||
await t.step("start up the server and access the root page", async () => {
|
await t.step("start up the server and access the root page", async () => {
|
||||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
const { serverProcess, lines } = await startFreshServer({
|
||||||
args: ["run", "-A", "--check", "main.ts"],
|
args: ["run", "-A", "--check", "main.ts"],
|
||||||
stdin: "null",
|
|
||||||
stdout: "piped",
|
|
||||||
stderr: "inherit",
|
|
||||||
cwd: tmpDirName,
|
cwd: tmpDirName,
|
||||||
}).spawn();
|
});
|
||||||
|
|
||||||
const lines = serverProcess.stdout
|
|
||||||
.pipeThrough(new TextDecoderStream())
|
|
||||||
.pipeThrough(new TextLineStream());
|
|
||||||
|
|
||||||
let started = false;
|
|
||||||
for await (const line of lines) {
|
|
||||||
console.log(line);
|
|
||||||
if (line.includes("Listening on http://")) {
|
|
||||||
started = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!started) {
|
|
||||||
throw new Error("Server didn't start up");
|
|
||||||
}
|
|
||||||
|
|
||||||
await delay(100);
|
await delay(100);
|
||||||
|
|
||||||
@@ -260,29 +241,10 @@ Deno.test({
|
|||||||
});
|
});
|
||||||
|
|
||||||
await t.step("start up the server and access the root page", async () => {
|
await t.step("start up the server and access the root page", async () => {
|
||||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
const { serverProcess, lines } = await startFreshServer({
|
||||||
args: ["run", "-A", "--check", "main.ts"],
|
args: ["run", "-A", "--check", "main.ts"],
|
||||||
stdin: "null",
|
|
||||||
stdout: "piped",
|
|
||||||
stderr: "inherit",
|
|
||||||
cwd: tmpDirName,
|
cwd: tmpDirName,
|
||||||
}).spawn();
|
});
|
||||||
|
|
||||||
const lines = serverProcess.stdout
|
|
||||||
.pipeThrough(new TextDecoderStream())
|
|
||||||
.pipeThrough(new TextLineStream());
|
|
||||||
|
|
||||||
let started = false;
|
|
||||||
for await (const line of lines) {
|
|
||||||
console.log(line);
|
|
||||||
if (line.includes("Listening on http://")) {
|
|
||||||
started = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!started) {
|
|
||||||
throw new Error("Server didn't start up");
|
|
||||||
}
|
|
||||||
|
|
||||||
await delay(100);
|
await delay(100);
|
||||||
|
|
||||||
@@ -456,28 +418,10 @@ Deno.test({
|
|||||||
});
|
});
|
||||||
|
|
||||||
await t.step("start up the server", async () => {
|
await t.step("start up the server", async () => {
|
||||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
const { serverProcess, lines } = await startFreshServer({
|
||||||
args: ["run", "-A", "--check", "subdirectory/subsubdirectory/dev.ts"],
|
args: ["run", "-A", "--check", "subdirectory/subsubdirectory/dev.ts"],
|
||||||
stdin: "null",
|
|
||||||
stdout: "piped",
|
|
||||||
stderr: "inherit",
|
|
||||||
cwd: tmpDirName,
|
cwd: tmpDirName,
|
||||||
}).spawn();
|
});
|
||||||
|
|
||||||
const lines = serverProcess.stdout
|
|
||||||
.pipeThrough(new TextDecoderStream())
|
|
||||||
.pipeThrough(new TextLineStream());
|
|
||||||
|
|
||||||
let started = false;
|
|
||||||
for await (const line of lines) {
|
|
||||||
console.log(line);
|
|
||||||
if (line.includes("Listening on http://")) {
|
|
||||||
started = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(started, "Server didn't start up");
|
|
||||||
|
|
||||||
await delay(100);
|
await delay(100);
|
||||||
|
|
||||||
|
|||||||
+10
-86
@@ -5,11 +5,11 @@ import {
|
|||||||
assertStringIncludes,
|
assertStringIncludes,
|
||||||
delay,
|
delay,
|
||||||
puppeteer,
|
puppeteer,
|
||||||
TextLineStream,
|
|
||||||
} from "./deps.ts";
|
} from "./deps.ts";
|
||||||
import manifest from "./fixture/fresh.gen.ts";
|
import manifest from "./fixture/fresh.gen.ts";
|
||||||
import options from "./fixture/options.ts";
|
import options from "./fixture/options.ts";
|
||||||
import { BUILD_ID } from "../src/server/build_id.ts";
|
import { BUILD_ID } from "../src/server/build_id.ts";
|
||||||
|
import { startFreshServer } from "./test_utils.ts";
|
||||||
|
|
||||||
const ctx = await ServerContext.fromManifest(manifest, options);
|
const ctx = await ServerContext.fromManifest(manifest, options);
|
||||||
const handler = ctx.handler();
|
const handler = ctx.handler();
|
||||||
@@ -606,7 +606,7 @@ Deno.test("experimental Deno.serve", {
|
|||||||
ignore: Deno.build.os === "windows", // TODO: Deno.serve hang on Windows?
|
ignore: Deno.build.os === "windows", // TODO: Deno.serve hang on Windows?
|
||||||
}, async (t) => {
|
}, async (t) => {
|
||||||
// Preparation
|
// Preparation
|
||||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
const { serverProcess, lines } = await startFreshServer({
|
||||||
args: [
|
args: [
|
||||||
"run",
|
"run",
|
||||||
"-A",
|
"-A",
|
||||||
@@ -614,25 +614,7 @@ Deno.test("experimental Deno.serve", {
|
|||||||
"./tests/fixture/main.ts",
|
"./tests/fixture/main.ts",
|
||||||
"--experimental-deno-serve",
|
"--experimental-deno-serve",
|
||||||
],
|
],
|
||||||
stdout: "piped",
|
});
|
||||||
stderr: "inherit",
|
|
||||||
}).spawn();
|
|
||||||
|
|
||||||
const decoder = new TextDecoderStream();
|
|
||||||
const lines = serverProcess.stdout
|
|
||||||
.pipeThrough(decoder)
|
|
||||||
.pipeThrough(new TextLineStream());
|
|
||||||
|
|
||||||
let started = false;
|
|
||||||
for await (const line of lines) {
|
|
||||||
if (line.includes("Listening on http://")) {
|
|
||||||
started = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!started) {
|
|
||||||
throw new Error("Server didn't start up");
|
|
||||||
}
|
|
||||||
|
|
||||||
await delay(100);
|
await delay(100);
|
||||||
|
|
||||||
@@ -676,27 +658,9 @@ Deno.test("jsx pragma works", {
|
|||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
}, async (t) => {
|
}, async (t) => {
|
||||||
// Preparation
|
// Preparation
|
||||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
const { serverProcess, lines } = await startFreshServer({
|
||||||
args: ["run", "-A", "./tests/fixture_jsx_pragma/main.ts"],
|
args: ["run", "-A", "./tests/fixture_jsx_pragma/main.ts"],
|
||||||
stdout: "piped",
|
});
|
||||||
stderr: "inherit",
|
|
||||||
}).spawn();
|
|
||||||
|
|
||||||
const decoder = new TextDecoderStream();
|
|
||||||
const lines = serverProcess.stdout
|
|
||||||
.pipeThrough(decoder)
|
|
||||||
.pipeThrough(new TextLineStream());
|
|
||||||
|
|
||||||
let started = false;
|
|
||||||
for await (const line of lines) {
|
|
||||||
if (line.includes("Listening on http://")) {
|
|
||||||
started = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!started) {
|
|
||||||
throw new Error("Server didn't start up");
|
|
||||||
}
|
|
||||||
|
|
||||||
await delay(100);
|
await delay(100);
|
||||||
|
|
||||||
@@ -730,27 +694,9 @@ Deno.test("preloading javascript files", {
|
|||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
}, async () => {
|
}, async () => {
|
||||||
// Preparation
|
// Preparation
|
||||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
const { serverProcess, lines } = await startFreshServer({
|
||||||
args: ["run", "-A", "./tests/fixture/main.ts"],
|
args: ["run", "-A", "./tests/fixture/main.ts"],
|
||||||
stdout: "piped",
|
});
|
||||||
stderr: "inherit",
|
|
||||||
}).spawn();
|
|
||||||
|
|
||||||
const decoder = new TextDecoderStream();
|
|
||||||
const lines = serverProcess.stdout
|
|
||||||
.pipeThrough(decoder)
|
|
||||||
.pipeThrough(new TextLineStream());
|
|
||||||
|
|
||||||
let started = false;
|
|
||||||
for await (const line of lines) {
|
|
||||||
if (line.includes("Listening on http://")) {
|
|
||||||
started = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!started) {
|
|
||||||
throw new Error("Server didn't start up");
|
|
||||||
}
|
|
||||||
|
|
||||||
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
|
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
@@ -798,32 +744,10 @@ Deno.test("PORT environment variable", {
|
|||||||
}, async () => {
|
}, async () => {
|
||||||
const PORT = "8765";
|
const PORT = "8765";
|
||||||
// Preparation
|
// Preparation
|
||||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
const { serverProcess, lines } = await startFreshServer({
|
||||||
args: [
|
args: ["run", "-A", "./tests/fixture/main.ts"],
|
||||||
"run",
|
|
||||||
"-A",
|
|
||||||
"./tests/fixture/main.ts",
|
|
||||||
],
|
|
||||||
stdout: "piped",
|
|
||||||
stderr: "inherit",
|
|
||||||
env: { PORT },
|
env: { PORT },
|
||||||
}).spawn();
|
});
|
||||||
|
|
||||||
const decoder = new TextDecoderStream();
|
|
||||||
const lines = serverProcess.stdout
|
|
||||||
.pipeThrough(decoder)
|
|
||||||
.pipeThrough(new TextLineStream());
|
|
||||||
|
|
||||||
let started = false;
|
|
||||||
for await (const line of lines) {
|
|
||||||
if (line.includes("Listening on http://") && line.includes(PORT)) {
|
|
||||||
started = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!started) {
|
|
||||||
throw new Error("Server didn't start up");
|
|
||||||
}
|
|
||||||
|
|
||||||
await delay(100);
|
await delay(100);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { TextLineStream } from "./deps.ts";
|
||||||
|
|
||||||
|
export async function startFreshServer(options: Deno.CommandOptions) {
|
||||||
|
const serverProcess = new Deno.Command(Deno.execPath(), {
|
||||||
|
...options,
|
||||||
|
stdin: "null",
|
||||||
|
stdout: "piped",
|
||||||
|
stderr: "inherit",
|
||||||
|
}).spawn();
|
||||||
|
|
||||||
|
const decoder = new TextDecoderStream();
|
||||||
|
const lines: ReadableStream<string> = serverProcess.stdout
|
||||||
|
.pipeThrough(decoder)
|
||||||
|
.pipeThrough(new TextLineStream());
|
||||||
|
|
||||||
|
let started = false;
|
||||||
|
for await (const line of lines) {
|
||||||
|
if (line.includes("Listening on http://")) {
|
||||||
|
started = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!started) {
|
||||||
|
throw new Error("Server didn't start up");
|
||||||
|
}
|
||||||
|
|
||||||
|
return { serverProcess, lines };
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user