summaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authorhaturau <135221985+haturatu@users.noreply.github.com>2024-11-20 01:20:47 +0900
committerGitHub <noreply@github.com>2024-11-20 01:20:47 +0900
commit85719a67e59c7aa45bead26e4942d7df8b1b42d4 (patch)
treeface0aecaac53e93ce2f23b53c48859bcf1a36ec /tests/unit
parent67697bc2e4a62a9670699fd18ad0dd8efc5bd955 (diff)
parent186b52731c6bb326c4d32905c5e732d082e83465 (diff)
Merge branch 'denoland:main' into main
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/command_test.ts18
-rw-r--r--tests/unit/console_test.ts9
-rw-r--r--tests/unit/fetch_test.ts27
-rw-r--r--tests/unit/globals_test.ts1
-rw-r--r--tests/unit/ops_test.ts2
-rw-r--r--tests/unit/serve_test.ts57
-rw-r--r--tests/unit/signal_test.ts32
-rw-r--r--tests/unit/stat_test.ts18
-rw-r--r--tests/unit/streams_test.ts15
-rw-r--r--tests/unit/websocket_test.ts3
10 files changed, 153 insertions, 29 deletions
diff --git a/tests/unit/command_test.ts b/tests/unit/command_test.ts
index 51bbdd860..8345548f8 100644
--- a/tests/unit/command_test.ts
+++ b/tests/unit/command_test.ts
@@ -14,27 +14,31 @@ Deno.test(
const enc = new TextEncoder();
const cwd = await Deno.makeTempDir({ prefix: "deno_command_test" });
+ const exitCodeFileLock = "deno_was_here.lock";
const exitCodeFile = "deno_was_here";
const programFile = "poll_exit.ts";
const program = `
+const file = await Deno.open("${exitCodeFileLock}", { write: true, create: true });
async function tryExit() {
+ await file.lock(true);
try {
const code = parseInt(await Deno.readTextFile("${exitCodeFile}"));
Deno.exit(code);
} catch {
// Retry if we got here before deno wrote the file.
setTimeout(tryExit, 0.01);
+ } finally {
+ await file.unlock();
}
}
tryExit();
`;
-
Deno.writeFileSync(`${cwd}/${programFile}`, enc.encode(program));
const command = new Deno.Command(Deno.execPath(), {
cwd,
- args: ["run", "--allow-read", programFile],
+ args: ["run", "-RW", programFile],
stdout: "inherit",
stderr: "inherit",
});
@@ -43,12 +47,18 @@ tryExit();
// Write the expected exit code *after* starting deno.
// This is how we verify that `Child` is actually asynchronous.
const code = 84;
- Deno.writeFileSync(`${cwd}/${exitCodeFile}`, enc.encode(`${code}`));
+ await using file = await Deno.open(`${cwd}/${exitCodeFileLock}`, {
+ write: true,
+ create: true,
+ });
+ await file.lock(true);
+ Deno.writeFileSync(`${cwd}/${exitCodeFile}`, enc.encode(`${code}`));
+ await file.unlock();
const status = await child.status;
await Deno.remove(cwd, { recursive: true });
- assertEquals(status.success, false);
assertEquals(status.code, code);
+ assertEquals(status.success, false);
assertEquals(status.signal, null);
},
);
diff --git a/tests/unit/console_test.ts b/tests/unit/console_test.ts
index 878d17ae3..06f5dd7e6 100644
--- a/tests/unit/console_test.ts
+++ b/tests/unit/console_test.ts
@@ -1162,7 +1162,7 @@ Deno.test(function consoleTestWithIntegerFormatSpecifier() {
assertEquals(stringify("%i"), "%i");
assertEquals(stringify("%i", 42.0), "42");
assertEquals(stringify("%i", 42), "42");
- assertEquals(stringify("%i", "42"), "NaN");
+ assertEquals(stringify("%i", "42"), "42");
assertEquals(stringify("%i", 1.5), "1");
assertEquals(stringify("%i", -0.5), "0");
assertEquals(stringify("%i", ""), "NaN");
@@ -1172,7 +1172,7 @@ Deno.test(function consoleTestWithIntegerFormatSpecifier() {
assertEquals(stringify("%d", 12345678901234567890123), "1");
assertEquals(
stringify("%i", 12345678901234567890123n),
- "12345678901234567890123n",
+ "1.2345678901234568e+22",
);
});
@@ -1180,13 +1180,13 @@ Deno.test(function consoleTestWithFloatFormatSpecifier() {
assertEquals(stringify("%f"), "%f");
assertEquals(stringify("%f", 42.0), "42");
assertEquals(stringify("%f", 42), "42");
- assertEquals(stringify("%f", "42"), "NaN");
+ assertEquals(stringify("%f", "42"), "42");
assertEquals(stringify("%f", 1.5), "1.5");
assertEquals(stringify("%f", -0.5), "-0.5");
assertEquals(stringify("%f", Math.PI), "3.141592653589793");
assertEquals(stringify("%f", ""), "NaN");
assertEquals(stringify("%f", Symbol("foo")), "NaN");
- assertEquals(stringify("%f", 5n), "NaN");
+ assertEquals(stringify("%f", 5n), "5");
assertEquals(stringify("%f %f", 42, 43), "42 43");
assertEquals(stringify("%f %f", 42), "42 %f");
});
@@ -1227,6 +1227,7 @@ Deno.test(function consoleParseCssColor() {
assertEquals(parseCssColor("inherit"), null);
assertEquals(parseCssColor("black"), [0, 0, 0]);
assertEquals(parseCssColor("darkmagenta"), [139, 0, 139]);
+ assertEquals(parseCssColor("darkMaGenta"), [139, 0, 139]);
assertEquals(parseCssColor("slateblue"), [106, 90, 205]);
assertEquals(parseCssColor("#ffaa00"), [255, 170, 0]);
assertEquals(parseCssColor("#ffAA00"), [255, 170, 0]);
diff --git a/tests/unit/fetch_test.ts b/tests/unit/fetch_test.ts
index 3ae96746a..6d3fd8cc1 100644
--- a/tests/unit/fetch_test.ts
+++ b/tests/unit/fetch_test.ts
@@ -2119,3 +2119,30 @@ Deno.test(
await server;
},
);
+
+Deno.test("fetch async iterable", async () => {
+ const iterable = (async function* () {
+ yield new Uint8Array([1, 2, 3, 4, 5]);
+ yield new Uint8Array([6, 7, 8, 9, 10]);
+ })();
+ const res = new Response(iterable);
+ const actual = await res.bytes();
+ const expected = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
+ assertEquals(actual, expected);
+});
+
+Deno.test("fetch iterable", async () => {
+ const iterable = (function* () {
+ yield new Uint8Array([1, 2, 3, 4, 5]);
+ yield new Uint8Array([6, 7, 8, 9, 10]);
+ })();
+ const res = new Response(iterable);
+ const actual = await res.bytes();
+ const expected = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
+ assertEquals(actual, expected);
+});
+
+Deno.test("fetch string object", async () => {
+ const res = new Response(Object("hello"));
+ assertEquals(await res.text(), "hello");
+});
diff --git a/tests/unit/globals_test.ts b/tests/unit/globals_test.ts
index 45a045835..6de228e1c 100644
--- a/tests/unit/globals_test.ts
+++ b/tests/unit/globals_test.ts
@@ -1,4 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+// deno-lint-ignore-file no-node-globals
import {
assert,
diff --git a/tests/unit/ops_test.ts b/tests/unit/ops_test.ts
index 4ba7c5ce3..6de55f8b6 100644
--- a/tests/unit/ops_test.ts
+++ b/tests/unit/ops_test.ts
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-const EXPECTED_OP_COUNT = 11;
+const EXPECTED_OP_COUNT = 12;
Deno.test(function checkExposedOps() {
// @ts-ignore TS doesn't allow to index with symbol
diff --git a/tests/unit/serve_test.ts b/tests/unit/serve_test.ts
index 439d71d55..7d8c6ca06 100644
--- a/tests/unit/serve_test.ts
+++ b/tests/unit/serve_test.ts
@@ -4270,3 +4270,60 @@ Deno.test({
assertEquals(hostname, "0.0.0.0");
await server.shutdown();
});
+
+Deno.test({
+ name: "AbortSignal aborted when request is cancelled",
+}, async () => {
+ const { promise, resolve } = Promise.withResolvers<void>();
+
+ let cancelled = false;
+
+ const server = Deno.serve({
+ hostname: "0.0.0.0",
+ port: servePort,
+ onListen: () => resolve(),
+ }, async (request) => {
+ request.signal.addEventListener("abort", () => cancelled = true);
+ assert(!request.signal.aborted);
+ await new Promise((resolve) => setTimeout(resolve, 3000)); // abort during waiting
+ assert(request.signal.aborted);
+ return new Response("Ok");
+ });
+
+ await promise;
+ await fetch(`http://localhost:${servePort}/`, {
+ signal: AbortSignal.timeout(1000),
+ }).catch(() => {});
+
+ await server.shutdown();
+
+ assert(cancelled);
+});
+
+Deno.test({
+ name: "AbortSignal event aborted when request is cancelled",
+}, async () => {
+ const { promise, resolve } = Promise.withResolvers<void>();
+
+ const server = Deno.serve({
+ hostname: "0.0.0.0",
+ port: servePort,
+ onListen: () => resolve(),
+ }, async (request) => {
+ const { promise: promiseAbort, resolve: resolveAbort } = Promise
+ .withResolvers<void>();
+ request.signal.addEventListener("abort", () => resolveAbort());
+ assert(!request.signal.aborted);
+
+ await promiseAbort;
+
+ return new Response("Ok");
+ });
+
+ await promise;
+ await fetch(`http://localhost:${servePort}/`, {
+ signal: AbortSignal.timeout(100),
+ }).catch(() => {});
+
+ await server.shutdown();
+});
diff --git a/tests/unit/signal_test.ts b/tests/unit/signal_test.ts
index 65b5ba78e..8923aa75b 100644
--- a/tests/unit/signal_test.ts
+++ b/tests/unit/signal_test.ts
@@ -5,101 +5,101 @@ Deno.test(
{ ignore: Deno.build.os !== "windows" },
function signalsNotImplemented() {
const msg =
- "Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK).";
+ "Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK), but got ";
assertThrows(
() => {
Deno.addSignalListener("SIGALRM", () => {});
},
Error,
- msg,
+ msg + "SIGALRM",
);
assertThrows(
() => {
Deno.addSignalListener("SIGCHLD", () => {});
},
Error,
- msg,
+ msg + "SIGCHLD",
);
assertThrows(
() => {
Deno.addSignalListener("SIGHUP", () => {});
},
Error,
- msg,
+ msg + "SIGHUP",
);
assertThrows(
() => {
Deno.addSignalListener("SIGIO", () => {});
},
Error,
- msg,
+ msg + "SIGIO",
);
assertThrows(
() => {
Deno.addSignalListener("SIGPIPE", () => {});
},
Error,
- msg,
+ msg + "SIGPIPE",
);
assertThrows(
() => {
Deno.addSignalListener("SIGQUIT", () => {});
},
Error,
- msg,
+ msg + "SIGQUIT",
);
assertThrows(
() => {
Deno.addSignalListener("SIGTERM", () => {});
},
Error,
- msg,
+ msg + "SIGTERM",
);
assertThrows(
() => {
Deno.addSignalListener("SIGUSR1", () => {});
},
Error,
- msg,
+ msg + "SIGUSR1",
);
assertThrows(
() => {
Deno.addSignalListener("SIGUSR2", () => {});
},
Error,
- msg,
+ msg + "SIGUSR2",
);
assertThrows(
() => {
Deno.addSignalListener("SIGWINCH", () => {});
},
Error,
- msg,
+ msg + "SIGWINCH",
);
assertThrows(
() => Deno.addSignalListener("SIGKILL", () => {}),
Error,
- msg,
+ msg + "SIGKILL",
);
assertThrows(
() => Deno.addSignalListener("SIGSTOP", () => {}),
Error,
- msg,
+ msg + "SIGSTOP",
);
assertThrows(
() => Deno.addSignalListener("SIGILL", () => {}),
Error,
- msg,
+ msg + "SIGILL",
);
assertThrows(
() => Deno.addSignalListener("SIGFPE", () => {}),
Error,
- msg,
+ msg + "SIGFPE",
);
assertThrows(
() => Deno.addSignalListener("SIGSEGV", () => {}),
Error,
- msg,
+ msg + "SIGSEGV",
);
},
);
diff --git a/tests/unit/stat_test.ts b/tests/unit/stat_test.ts
index 59831a069..0609035b4 100644
--- a/tests/unit/stat_test.ts
+++ b/tests/unit/stat_test.ts
@@ -31,6 +31,13 @@ Deno.test(
assert(
tempInfo.birthtime === null || now - tempInfo.birthtime.valueOf() < 1000,
);
+ assert(tempInfo.ctime !== null && now - tempInfo.ctime.valueOf() < 1000);
+ const mode = tempInfo.mode! & 0o777;
+ if (Deno.build.os === "windows") {
+ assertEquals(mode, 0o666);
+ } else {
+ assertEquals(mode, 0o600);
+ }
const readmeInfoByUrl = Deno.statSync(pathToAbsoluteFileUrl("README.md"));
assert(readmeInfoByUrl.isFile);
@@ -65,6 +72,10 @@ Deno.test(
tempInfoByUrl.birthtime === null ||
now - tempInfoByUrl.birthtime.valueOf() < 1000,
);
+ assert(
+ tempInfoByUrl.ctime !== null &&
+ now - tempInfoByUrl.ctime.valueOf() < 1000,
+ );
Deno.removeSync(tempFile, { recursive: true });
Deno.removeSync(tempFileForUrl, { recursive: true });
@@ -171,6 +182,7 @@ Deno.test(
assert(
tempInfo.birthtime === null || now - tempInfo.birthtime.valueOf() < 1000,
);
+ assert(tempInfo.ctime !== null && now - tempInfo.ctime.valueOf() < 1000);
const tempFileForUrl = await Deno.makeTempFile();
const tempInfoByUrl = await Deno.stat(
@@ -191,7 +203,10 @@ Deno.test(
tempInfoByUrl.birthtime === null ||
now - tempInfoByUrl.birthtime.valueOf() < 1000,
);
-
+ assert(
+ tempInfoByUrl.ctime !== null &&
+ now - tempInfoByUrl.ctime.valueOf() < 1000,
+ );
Deno.removeSync(tempFile, { recursive: true });
Deno.removeSync(tempFileForUrl, { recursive: true });
},
@@ -271,7 +286,6 @@ Deno.test(
const s = Deno.statSync(filename);
assert(s.dev !== 0);
assert(s.ino === null);
- assert(s.mode === null);
assert(s.nlink === null);
assert(s.uid === null);
assert(s.gid === null);
diff --git a/tests/unit/streams_test.ts b/tests/unit/streams_test.ts
index b866fa7d5..73f9a6095 100644
--- a/tests/unit/streams_test.ts
+++ b/tests/unit/streams_test.ts
@@ -1,5 +1,10 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-import { assertEquals, assertRejects, fail } from "./test_util.ts";
+import {
+ assertEquals,
+ assertRejects,
+ assertThrows,
+ fail,
+} from "./test_util.ts";
const {
core,
@@ -533,3 +538,11 @@ Deno.test(async function decompressionStreamInvalidGzipStillReported() {
"corrupt gzip stream does not have a matching checksum",
);
});
+
+Deno.test(function readableStreamFromWithStringThrows() {
+ assertThrows(
+ () => ReadableStream.from("string"),
+ TypeError,
+ "Failed to execute 'ReadableStream.from': Argument 1 can not be converted to async iterable.",
+ );
+});
diff --git a/tests/unit/websocket_test.ts b/tests/unit/websocket_test.ts
index 7db876b17..3aafe8da2 100644
--- a/tests/unit/websocket_test.ts
+++ b/tests/unit/websocket_test.ts
@@ -453,7 +453,8 @@ Deno.test("invalid server", async () => {
const { promise, resolve } = Promise.withResolvers<void>();
const ws = new WebSocket("ws://localhost:2121");
let err = false;
- ws.onerror = () => {
+ ws.onerror = (e) => {
+ assert("error" in e);
err = true;
};
ws.onclose = () => {