refactor: use _util/asserts.ts for non-test code (#2857)

This commit is contained in:
Asher Gomez 2022-11-10 18:28:20 +11:00 committed by GitHub
parent e808ece9ed
commit 792f1334fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 65 additions and 74 deletions

View File

@ -109,3 +109,10 @@ accepted.
_For maintainers_:
To release a new version a tag in the form of `x.y.z` should be added.
### Internal Assertions
All internal non-test code, that is files that do not have `test` or `bench` in
the name, must use the assertion functions within `_utils/asserts.ts` and not
`testing/asserts.ts`. This is to create a separation of concerns between
internal and testing assertions.

View File

@ -1,6 +1,10 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
* All internal non-test code, that is files that do not have `test` or `bench` in the name, must use the assertion functions within `_utils/asserts.ts` and not `testing/asserts.ts`. This is to create a separation of concerns between internal and testing assertions.
*/
export class DenoStdInternalError extends Error {
constructor(message: string) {
super(message);
@ -14,3 +18,8 @@ export function assert(expr: unknown, msg = ""): asserts expr {
throw new DenoStdInternalError(msg);
}
}
/** Use this to assert unreachable code. */
export function unreachable(): never {
throw new DenoStdInternalError("unreachable");
}

View File

@ -1,5 +1,5 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert, DenoStdInternalError } from "./assert.ts";
import { assert, DenoStdInternalError, unreachable } from "./asserts.ts";
import { assertThrows } from "../testing/asserts.ts";
Deno.test({
@ -29,3 +29,15 @@ Deno.test({
);
},
});
Deno.test("assert unreachable", function () {
let didThrow = false;
try {
unreachable();
} catch (e) {
assert(e instanceof DenoStdInternalError);
assert(e.message === "unreachable");
didThrow = true;
}
assert(didThrow);
});

View File

@ -93,7 +93,7 @@
import { MultiReader } from "../io/readers.ts";
import { Buffer, PartialReadError } from "../io/buffer.ts";
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { readAll } from "../streams/conversion.ts";
type Reader = Deno.Reader;

View File

@ -1,6 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/asserts.ts";
/** Compare to array buffers or data views in a way that timing based attacks
* cannot gain information about the platform. */

View File

@ -11,7 +11,7 @@
* @module
*/
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import type { ReadOptions } from "./csv/_io.ts";
import { Parser } from "./csv/_parser.ts";

View File

@ -3,7 +3,7 @@
// Copyright 2011 The Go Authors. All rights reserved. BSD license.
// https://github.com/golang/go/blob/master/LICENSE
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../../_util/assert.ts";
import { assert } from "../../_util/asserts.ts";
/**
* @property separator - Character which separates values. Default: ','

View File

@ -1,5 +1,5 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../../_util/assert.ts";
import { assert } from "../../_util/asserts.ts";
import {
ERR_BARE_QUOTE,
ERR_FIELD_COUNT,

View File

@ -9,7 +9,7 @@
* @module
*/
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
export interface ParseOptions {
/** Allow trailing commas at the end of arrays and objects. (default: `true`) */

View File

@ -7,7 +7,7 @@
*
* @module
*/
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
/** Combines recursively all intersection types and returns a new single type. */
type Id<T> = T extends Record<string, unknown>

View File

@ -4,7 +4,7 @@
import * as path from "../path/mod.ts";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { getFileInfoType, isSubdir, toPathString } from "./_util.ts";
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { isWindows } from "../_util/os.ts";
export interface CopyOptions {

View File

@ -9,7 +9,7 @@ import {
SEP_PATTERN,
} from "../path/mod.ts";
import { walk, walkSync } from "./walk.ts";
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { isWindows } from "../_util/os.ts";
import {
createWalkEntry,

View File

@ -2,7 +2,7 @@
// Documentation and interface for walk were adapted from Go
// https://golang.org/pkg/path/filepath/#Walk
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { join, normalize } from "../path/mod.ts";
import {
createWalkEntry,

View File

@ -3,7 +3,7 @@
// https://github.com/golang/go/blob/master/src/net/http/cookie.go
// This module is browser compatible.
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { toIMF } from "../datetime/mod.ts";
export interface Cookie {

View File

@ -10,7 +10,7 @@ import { contentType } from "../media_types/mod.ts";
import { serve, serveTls } from "./server.ts";
import { Status } from "./http_status.ts";
import { parse } from "../flags/mod.ts";
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { red } from "../fmt/colors.ts";
import { compareEtag, createCommonResponse } from "./util.ts";
import { DigestAlgorithm, toHashString } from "../crypto/mod.ts";

View File

@ -1,5 +1,5 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { BytesList } from "../bytes/bytes_list.ts";
import { concat, copy } from "../bytes/mod.ts";
import type { Reader, ReaderSync, Writer, WriterSync } from "./types.d.ts";

View File

@ -1,7 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { copy as copyBytes } from "../bytes/mod.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/asserts.ts";
const DEFAULT_BUFFER_SIZE = 32 * 1024;

View File

@ -1,5 +1,5 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import type { BufReader } from "./buffer.ts";
import type { Reader, Writer } from "./types.d.ts";

View File

@ -16,7 +16,7 @@ import {
RotatingFileHandler,
WriterHandler,
} from "./handlers.ts";
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import type { LevelName } from "./levels.ts";
export { LogLevels } from "./levels.ts";

View File

@ -3,7 +3,7 @@
import { Reporter } from "./reporter.js";
import { DecoderBuffer, EncoderBuffer } from "./buffer.js";
import { assert, assertEquals } from "../../../../../testing/asserts.ts";
import { assert } from "../../../../../_util/asserts.ts";
// Supported tags
const tags = [
@ -156,7 +156,7 @@ Node.prototype._init = function init(body) {
state.children = state.children.filter(function (child) {
return child._baseState.parent === this;
}, this);
assertEquals(state.children.length, 1, "Root node can have only one child");
assert(state.children.length === 1, "Root node can have only one child");
};
Node.prototype._useArgs = function useArgs(args) {

View File

@ -1,6 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import Dirent from "./_fs_dirent.ts";
import { assert } from "../../_util/assert.ts";
import { assert } from "../../_util/asserts.ts";
import { ERR_MISSING_ARGS } from "../internal/errors.ts";
export default class Dir {

View File

@ -1,6 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { deferred } from "../async/mod.ts";
import { assert, assertStringIncludes, fail } from "../testing/asserts.ts";
// import { deferred } from "../async/mod.ts";
import { assert } from "../_util/asserts.ts";
import { readAll } from "../streams/conversion.ts";
import { errorMap } from "./internal_binding/uv.ts";
import { codes } from "./internal/error_codes.ts";
@ -177,43 +177,6 @@ export function once<T = undefined>(
};
}
/**
* @param [expectedExecutions = 1]
* @param [timeout = 1000] Milliseconds to wait before the promise is forcefully exited */
export function mustCall<T extends unknown[]>(
fn: (...args: T) => void = () => {},
expectedExecutions = 1,
timeout = 1000,
): [Promise<void>, (...args: T) => void] {
if (expectedExecutions < 1) {
throw new Error("Expected executions can't be lower than 1");
}
let timesExecuted = 0;
const completed = deferred();
const abort = setTimeout(() => completed.reject(), timeout);
function callback(this: unknown, ...args: T) {
timesExecuted++;
if (timesExecuted === expectedExecutions) {
completed.resolve();
}
fn.apply(this, args);
}
const result = completed
.then(() => clearTimeout(abort))
.catch(() =>
fail(
`Async operation not completed: Expected ${expectedExecutions}, executed ${timesExecuted}`,
)
);
return [
result,
callback,
];
}
/** Asserts that an error thrown in a callback will not be wrongly caught. */
export async function assertCallbackErrorUncaught(
{ prelude, invocation, cleanup }: {
@ -252,7 +215,7 @@ export async function assertCallbackErrorUncaught(
p.stderr.close();
await cleanup?.();
assert(!status.success);
assertStringIncludes(stderr, "Error: success");
assert(stderr.includes("Error: success"));
}
export function makeMethodsEnumerable(klass: { new (): unknown }) {

View File

@ -2,7 +2,7 @@
// This module implements 'child_process' module of Node.JS API.
// ref: https://nodejs.org/api/child_process.html
import { assert } from "../../_util/assert.ts";
import { assert } from "../../_util/asserts.ts";
import { EventEmitter } from "../events.ts";
import { os } from "../internal_binding/constants.ts";
import { notImplemented, warnNotImplemented } from "../_utils.ts";

View File

@ -20,7 +20,7 @@ import {
errorMap,
mapSysErrnoToUvErrno,
} from "../internal_binding/uv.ts";
import { assert } from "../../_util/assert.ts";
import { assert } from "../../_util/asserts.ts";
import { isWindows } from "../../_util/os.ts";
import { os as osConstants } from "../internal_binding/constants.ts";
const {

View File

@ -24,7 +24,7 @@
// - https://github.com/nodejs/node/blob/master/src/handle_wrap.cc
// - https://github.com/nodejs/node/blob/master/src/handle_wrap.h
import { unreachable } from "../../testing/asserts.ts";
import { unreachable } from "../../_util/asserts.ts";
import { AsyncWrap, providerType } from "./async_wrap.ts";
export class HandleWrap extends AsyncWrap {

View File

@ -25,7 +25,7 @@
// - https://github.com/nodejs/node/blob/master/src/node_file.cc
// - https://github.com/nodejs/node/blob/master/src/node_file.h
import { assert } from "../../testing/asserts.ts";
import { assert } from "../../_util/asserts.ts";
/**
* Write to the given file from the given buffer synchronously.

View File

@ -25,7 +25,7 @@
// - https://github.com/nodejs/node/blob/master/src/pipe_wrap.h
import { notImplemented } from "../_utils.ts";
import { unreachable } from "../../testing/asserts.ts";
import { unreachable } from "../../_util/asserts.ts";
import { ConnectionWrap } from "./connection_wrap.ts";
import { AsyncWrap, providerType } from "./async_wrap.ts";
import { LibuvStreamWrap } from "./stream_wrap.ts";

View File

@ -25,7 +25,7 @@
// - https://github.com/nodejs/node/blob/master/src/tcp_wrap.h
import { notImplemented } from "../_utils.ts";
import { unreachable } from "../../testing/asserts.ts";
import { unreachable } from "../../_util/asserts.ts";
import { ConnectionWrap } from "./connection_wrap.ts";
import { AsyncWrap, providerType } from "./async_wrap.ts";
import { LibuvStreamWrap } from "./stream_wrap.ts";

View File

@ -26,7 +26,7 @@
//
// See also: http://docs.libuv.org/en/v1.x/errors.html#error-constants
import { unreachable } from "../../testing/asserts.ts";
import { unreachable } from "../../_util/asserts.ts";
import { osType } from "../../_util/os.ts";
import { uvTranslateSysError } from "./_libuv_winerror.ts";
import { os } from "./constants.ts";

View File

@ -27,7 +27,7 @@ import nodeMods from "./module_all.ts";
import upstreamMods from "./upstream_modules.ts";
import * as path from "../path/mod.ts";
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { fileURLToPath, pathToFileURL } from "./url.ts";
import { isWindows } from "../_util/os.ts";
import {

View File

@ -87,7 +87,7 @@ import {
PipeConnectWrap,
} from "./internal_binding/pipe_wrap.ts";
import { ShutdownWrap } from "./internal_binding/stream_wrap.ts";
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { isWindows } from "../_util/os.ts";
import { ADDRCONFIG, lookup as dnsLookup } from "./dns.ts";
import { codeMap } from "./internal_binding/uv.ts";

View File

@ -19,7 +19,7 @@ import {
isWindowsDeviceRoot,
normalizeString,
} from "./_util.ts";
import { assert } from "../../_util/assert.ts";
import { assert } from "../../_util/asserts.ts";
export const sep = "\\";
export const delimiter = ";";

View File

@ -6,7 +6,7 @@ import { EventEmitter } from "./events.ts";
import { validateString } from "./internal/validators.mjs";
import { ERR_INVALID_ARG_TYPE, ERR_UNKNOWN_SIGNAL } from "./internal/errors.ts";
import { getOptionValue } from "./internal/options.ts";
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { fromFileUrl, join } from "../path/mod.ts";
import {
arch,

View File

@ -19,7 +19,7 @@ import {
isWindowsDeviceRoot,
normalizeString,
} from "./_util.ts";
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
export const sep = "\\";
export const delimiter = ";";

View File

@ -1,5 +1,5 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
import { copy } from "../bytes/mod.ts";
const MAX_SIZE = 2 ** 32 - 2;

View File

@ -3,7 +3,7 @@
import { bytesToUuid, uuidToBytes } from "./_common.ts";
import { concat } from "../bytes/mod.ts";
import { assert } from "../_util/assert.ts";
import { assert } from "../_util/asserts.ts";
const UUID_RE =
/^[0-9a-f]{8}-[0-9a-f]{4}-[5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;