1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00

refactor(std): remove testing dependencies from non-test code (#5838)

This commit is contained in:
Chris Knight 2020-06-07 14:20:33 +01:00 committed by GitHub
parent adffbacfe4
commit 3ef94c5473
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 75 additions and 28 deletions

15
std/_util/assert.ts Normal file
View file

@ -0,0 +1,15 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
export class DenoStdInternalError extends Error {
constructor(message: string) {
super(message);
this.name = "DenoStdInternalError";
}
}
/** Make an assertion, if not `true`, then throw. */
export function assert(expr: unknown, msg = ""): asserts expr {
if (!expr) {
throw new DenoStdInternalError(msg);
}
}

32
std/_util/assert_test.ts Normal file
View file

@ -0,0 +1,32 @@
import { assert, DenoStdInternalError } from "./assert.ts";
import { assertThrows } from "../testing/asserts.ts";
const { test } = Deno;
test({
name: "assert valid scenario",
fn(): void {
assert(true);
},
});
test({
name: "assert invalid scenario, no message",
fn(): void {
assertThrows(() => {
assert(false);
}, DenoStdInternalError);
},
});
test({
name: "assert invalid scenario, with message",
fn(): void {
assertThrows(
() => {
assert(false, "Oops! Should be true");
},
DenoStdInternalError,
"Oops! Should be true"
);
},
});

View file

@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
export function deepAssign(
target: Record<string, unknown>,

View file

@ -28,7 +28,7 @@
*/
import { MultiReader } from "../io/readers.ts";
import { BufReader } from "../io/bufio.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
const recordSize = 512;
const ustar = "ustar\u000000";

View file

@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
export type DateFormat = "mm-dd-yyyy" | "dd-mm-yyyy" | "yyyy-mm-dd";

View file

@ -7,7 +7,7 @@
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { StringReader } from "../io/readers.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
const INVALID_RUNE = ["\r", "\n", '"'];

View file

@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { deepAssign } from "../_util/deep_assign.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
class KeyValuePair {
constructor(public key: string, public value: unknown) {}

View file

@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
export interface Args {
/** Contains all the arguments that didn't have an option associated with

View file

@ -2,7 +2,7 @@
import * as path from "../path/mod.ts";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { isSubdir, getFileInfoType } from "./_util.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
const isWindows = Deno.build.os === "windows";

View file

@ -14,7 +14,7 @@ import {
walk,
walkSync,
} from "./walk.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
const { cwd } = Deno;
type FileInfo = Deno.FileInfo;

View file

@ -1,7 +1,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 { unimplemented, assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
import { basename, join, normalize } from "../path/mod.ts";
const { readDir, readDirSync, stat, statSync } = Deno;
@ -107,7 +107,7 @@ export async function* walk(
if (entry.isSymlink) {
if (followSymlinks) {
// TODO(ry) Re-enable followSymlinks.
unimplemented();
throw new Error("unimplemented");
} else {
continue;
}
@ -159,7 +159,7 @@ export function* walkSync(
for (const entry of readDirSync(root)) {
if (entry.isSymlink) {
if (followSymlinks) {
unimplemented();
throw new Error("unimplemented");
} else {
continue;
}

View file

@ -1,6 +1,6 @@
import { BufReader, BufWriter } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
import { encoder } from "../encoding/utf8.ts";
import { ServerRequest, Response } from "./server.ts";
import { STATUS_TEXT } from "./http_status.ts";

View file

@ -2,7 +2,7 @@
// Structured similarly to Go's cookie.go
// https://github.com/golang/go/blob/master/src/net/http/cookie.go
import { ServerRequest, Response } from "./server.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
import { toIMF } from "../datetime/mod.ts";
export interface Cookies {

View file

@ -10,7 +10,7 @@ const { args, stat, readDir, open, exit } = Deno;
import { posix, extname } from "../path/mod.ts";
import { listenAndServe, ServerRequest, Response } from "./server.ts";
import { parse } from "../flags/mod.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
interface EntryInfo {
mode: string;

View file

@ -1,7 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { encode } from "../encoding/utf8.ts";
import { BufReader, BufWriter } from "../io/bufio.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
import { deferred, Deferred, MuxAsyncIterator } from "../async/mod.ts";
import {
bodyReader,

View file

@ -7,7 +7,7 @@ type Reader = Deno.Reader;
type Writer = Deno.Writer;
type WriterSync = Deno.WriterSync;
import { charCode, copyBytes } from "./util.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
const DEFAULT_BUF_SIZE = 4096;
const MIN_BUF_SIZE = 16;

View file

@ -2,7 +2,7 @@
import { BufReader } from "./bufio.ts";
type Reader = Deno.Reader;
type Writer = Deno.Writer;
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
const DEFAULT_BUFFER_SIZE = 32 * 1024;

View file

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

View file

@ -12,7 +12,7 @@ import { extname } from "../path/mod.ts";
import { tempFile } from "../io/util.ts";
import { BufReader, BufWriter } from "../io/bufio.ts";
import { encoder } from "../encoding/utf8.ts";
import { assertStrictEquals, assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { hasOwnProperty } from "../_util/has_own_property.ts";
@ -178,7 +178,7 @@ class PartReader implements Reader, Closer {
);
if (this.n === 0) {
// Force buffered I/O to read more into buffer.
assertStrictEquals(eof, false);
assert(eof === false);
peekLength++;
}
}
@ -190,7 +190,7 @@ class PartReader implements Reader, Closer {
const nread = min(p.length, this.n);
const buf = p.subarray(0, nread);
const r = await br.readFull(buf);
assertStrictEquals(r, buf);
assert(r === buf);
this.n -= nread;
this.total += nread;
return nread;

View file

@ -1,5 +1,5 @@
import Dirent from "./_fs_dirent.ts";
import { assert } from "../../testing/asserts.ts";
import { assert } from "../../_util/assert.ts";
export default class Dir {
private dirPath: string | Uint8Array;

View file

@ -22,7 +22,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
import { validateIntegerRange } from "./util.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
export interface WrappedFunction extends Function {
listener: Function;

View file

@ -31,7 +31,7 @@ import * as nodeEvents from "./events.ts";
import * as nodeQueryString from "./querystring.ts";
import * as path from "../path/mod.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
import { pathToFileURL, fileURLToPath } from "./url.ts";
const CHAR_FORWARD_SLASH = "/".charCodeAt(0);

View file

@ -4,7 +4,7 @@
import { SEP, SEP_PATTERN } from "./separator.ts";
import { globrex } from "./_globrex.ts";
import { join, normalize } from "./mod.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
export interface GlobOptions {
extended?: boolean;

View file

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

View file

@ -8,7 +8,7 @@ import {
} from "./_common.ts";
import { Sha1 } from "../hash/sha1.ts";
import { isString } from "../node/util.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.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;

View file

@ -8,7 +8,7 @@ import { Sha1 } from "../hash/sha1.ts";
import { writeResponse } from "../http/_io.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { Deferred, deferred } from "../async/deferred.ts";
import { assert } from "../testing/asserts.ts";
import { assert } from "../_util/assert.ts";
import { concat } from "../bytes/mod.ts";
import Conn = Deno.Conn;
import Writer = Deno.Writer;