-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #966 from thefrontside/tm/test-more-node-versions
Run tests in CI for Node 16, 18, 20, 22
- Loading branch information
Showing
7 changed files
with
161 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,8 @@ | |
}, | ||
"lock": false, | ||
"tasks": { | ||
"test": "deno test --allow-run=deno", | ||
"test:node": "deno task build:npm 0.0.0 && node test/main/node.mjs hello world", | ||
"test": "deno test --allow-run=deno --allow-read --allow-env", | ||
"test:node": "deno run -A tasks/built-test.ts", | ||
"build:npm": "deno run -A tasks/build-npm.ts", | ||
"bench": "deno run -A tasks/bench.ts", | ||
"build:jsr": "deno run -A tasks/build-jsr.ts" | ||
|
@@ -42,5 +42,11 @@ | |
"dom", | ||
"dom.iterable" | ||
] | ||
}, | ||
"imports": { | ||
"@std/testing": "jsr:@std/testing@1", | ||
"ts-expect": "npm:[email protected]", | ||
"@std/expect": "jsr:@std/expect@1", | ||
"tinyexec": "npm:[email protected]" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { build, emptyDir } from "jsr:@deno/[email protected]"; | ||
|
||
const outDir = "./build/test"; | ||
|
||
await emptyDir(outDir); | ||
|
||
const entryPoints = [ | ||
"./lib/mod.ts", | ||
]; | ||
for await (const entry of Deno.readDir("test")) { | ||
if (entry.isFile) { | ||
entryPoints.push(`./test/${entry.name}`); | ||
} | ||
} | ||
|
||
await build({ | ||
entryPoints, | ||
outDir, | ||
shims: { | ||
deno: true, | ||
}, | ||
typeCheck: false, | ||
compilerOptions: { | ||
lib: ["ESNext", "DOM"], | ||
target: "ES2020", | ||
sourceMap: true, | ||
}, | ||
importMap: "deno.json", | ||
package: { | ||
// package.json properties | ||
name: "effection-tests", | ||
version: "0.0.0", | ||
sideEffects: false, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,166 +1,99 @@ | ||
import { $await, describe, expect, it, useCommand } from "./suite.ts"; | ||
import { type Operation, resource, run, sleep, spawn } from "../mod.ts"; | ||
import { describe, expect, it, x } from "./suite.ts"; | ||
import { each, run, type Stream } from "../mod.ts"; | ||
|
||
function* until(stream: Stream<string, void>, text: string) { | ||
for (const line of yield* each(stream)) { | ||
if (line.includes(text)) { | ||
return; | ||
} | ||
yield* each.next(); | ||
} | ||
} | ||
|
||
describe("main", () => { | ||
it("gracefully shuts down on SIGINT", async () => { | ||
await run(function* () { | ||
let daemon = yield* useCommand("deno", { | ||
stdout: "piped", | ||
args: ["run", "test/main/ok.daemon.ts"], | ||
}); | ||
let stdout = yield* buffer(daemon.stdout); | ||
yield* detect(stdout, "started"); | ||
let proc = yield* x("deno", ["run", "test/main/ok.daemon.ts"]); | ||
|
||
daemon.kill("SIGINT"); | ||
yield* until(proc.lines, "started"); | ||
|
||
let status = yield* $await(daemon.status); | ||
const { exitCode, stdout } = yield* proc.kill("SIGINT"); | ||
|
||
expect(status.code).toBe(130); | ||
expect(stdout).toContain("gracefully stopped"); | ||
|
||
yield* detect(stdout, "gracefully stopped"); | ||
expect(exitCode).toBe(130); | ||
}); | ||
}); | ||
|
||
it("gracefully shuts down on SIGTERM", async () => { | ||
await run(function* () { | ||
let daemon = yield* useCommand("deno", { | ||
stdout: "piped", | ||
args: ["run", "test/main/ok.daemon.ts"], | ||
}); | ||
let stdout = yield* buffer(daemon.stdout); | ||
yield* detect(stdout, "started"); | ||
let proc = yield* x("deno", ["run", "test/main/ok.daemon.ts"]); | ||
|
||
daemon.kill("SIGTERM"); | ||
yield* until(proc.lines, "started"); | ||
|
||
let status = yield* $await(daemon.status); | ||
const { exitCode, stdout } = yield* proc.kill("SIGTERM"); | ||
|
||
expect(status.code).toBe(143); | ||
expect(stdout).toContain("gracefully stopped"); | ||
|
||
yield* detect(stdout, "gracefully stopped"); | ||
expect(exitCode).toBe(143); | ||
}); | ||
}); | ||
|
||
it("exits gracefully on explicit exit()", async () => { | ||
await run(function* () { | ||
let cmd = yield* useCommand("deno", { | ||
stdout: "piped", | ||
args: ["run", "test/main/ok.exit.ts"], | ||
}); | ||
|
||
let stdout = yield* buffer(cmd.stdout); | ||
let proc = yield* x("deno", ["run", "test/main/ok.exit.ts"]); | ||
|
||
yield* detect(stdout, "goodbye.\nOk, computer."); | ||
yield* until(proc.lines, "goodbye."); | ||
yield* until(proc.lines, "Ok, computer."); | ||
}); | ||
}); | ||
|
||
it("exits gracefully with 0 on implicit exit", async () => { | ||
await run(function* () { | ||
let cmd = yield* useCommand("deno", { | ||
stdout: "piped", | ||
args: ["run", "test/main/ok.implicit.ts"], | ||
}); | ||
let proc = yield* x("deno", ["run", "test/main/ok.implicit.ts"]); | ||
|
||
yield* until(proc.lines, "goodbye."); | ||
|
||
let stdout = yield* buffer(cmd.stdout); | ||
let status = yield* $await(cmd.status); | ||
const { exitCode } = yield* proc; | ||
|
||
yield* detect(stdout, "goodbye."); | ||
expect(status.code).toEqual(0); | ||
expect(exitCode).toEqual(0); | ||
}); | ||
}); | ||
|
||
it("exits gracefully on explicit exit failure exit()", async () => { | ||
await run(function* () { | ||
let cmd = yield* useCommand("deno", { | ||
stdout: "piped", | ||
stderr: "piped", | ||
args: ["run", "test/main/fail.exit.ts"], | ||
}); | ||
let stdout = yield* buffer(cmd.stdout); | ||
let stderr = yield* buffer(cmd.stderr); | ||
let status = yield* $await(cmd.status); | ||
|
||
yield* detect(stdout, "graceful goodbye"); | ||
yield* detect(stderr, "It all went horribly wrong"); | ||
expect(status.code).toEqual(23); | ||
let proc = yield* x("deno", ["run", "test/main/fail.exit.ts"]); | ||
|
||
const { stderr, exitCode, stdout } = yield* proc; | ||
|
||
expect(stdout).toContain("graceful goodbye"); | ||
expect(stderr).toContain("It all went horribly wrong"); | ||
expect(exitCode).toEqual(23); | ||
}); | ||
}); | ||
|
||
it("error exits gracefully on unexpected errors", async () => { | ||
await run(function* () { | ||
let cmd = yield* useCommand("deno", { | ||
stdout: "piped", | ||
stderr: "piped", | ||
args: ["run", "test/main/fail.unexpected.ts"], | ||
}); | ||
|
||
let stdout = yield* buffer(cmd.stdout); | ||
let stderr = yield* buffer(cmd.stderr); | ||
let status = yield* $await(cmd.status); | ||
|
||
yield* detect(stdout, "graceful goodbye"); | ||
yield* detect(stderr, "Error: moo"); | ||
expect(status.code).toEqual(1); | ||
let proc = yield* x("deno", ["run", "test/main/fail.unexpected.ts"]); | ||
|
||
const { stderr, stdout, exitCode } = yield* proc; | ||
|
||
expect(stdout).toContain("graceful goodbye"); | ||
expect(stderr).toContain("Error: moo"); | ||
expect(exitCode).toEqual(1); | ||
}); | ||
}); | ||
|
||
it("works even if suspend is the only operation", async () => { | ||
await run(function* () { | ||
let process = yield* useCommand("deno", { | ||
stdout: "piped", | ||
args: ["run", "test/main/just.suspend.ts"], | ||
}); | ||
let stdout = yield* buffer(process.stdout); | ||
yield* detect(stdout, "started"); | ||
|
||
process.kill("SIGINT"); | ||
let proc = yield* x("deno", ["run", "test/main/just.suspend.ts"]); | ||
|
||
let status = yield* $await(process.status); | ||
yield* until(proc.lines, "started"); | ||
|
||
expect(status.code).toBe(130); | ||
const { exitCode, stdout } = yield* proc.kill("SIGINT"); | ||
|
||
yield* detect(stdout, "gracefully stopped"); | ||
expect(exitCode).toBe(130); | ||
expect(stdout).toContain("gracefully stopped"); | ||
}); | ||
}); | ||
}); | ||
|
||
interface Buffer { | ||
content: string; | ||
} | ||
|
||
function buffer(stream: ReadableStream<Uint8Array>): Operation<Buffer> { | ||
return resource<{ content: string }>(function* (provide) { | ||
let buff = { content: " " }; | ||
yield* spawn(function* () { | ||
let decoder = new TextDecoder(); | ||
let reader = stream.getReader(); | ||
|
||
try { | ||
let next = yield* $await(reader.read()); | ||
while (!next.done) { | ||
buff.content += decoder.decode(next.value); | ||
next = yield* $await(reader.read()); | ||
} | ||
} finally { | ||
yield* $await(reader.cancel()); | ||
} | ||
}); | ||
|
||
yield* provide(buff); | ||
}); | ||
} | ||
|
||
function* detect( | ||
buffer: Buffer, | ||
text: string, | ||
options: { timeout: number } = { timeout: 1000 }, | ||
): Operation<void> { | ||
let start = new Date().getTime(); | ||
|
||
while ((new Date().getTime() - start) < options.timeout) { | ||
if (buffer.content.includes(text)) { | ||
return; | ||
} | ||
yield* sleep(10); | ||
} | ||
expect(buffer.content).toContain(text); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.