mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 08:09:08 -05:00
parent
68584f983e
commit
c164e696d7
10 changed files with 56 additions and 56 deletions
|
@ -27,7 +27,11 @@ async function main() {
|
||||||
await checkVersion();
|
await checkVersion();
|
||||||
|
|
||||||
const prettier = run({
|
const prettier = run({
|
||||||
args: ["bash", "-c", "prettier --write *.ts */**/*.ts *.md */**/*.md"]
|
args: [
|
||||||
|
"bash",
|
||||||
|
"-c",
|
||||||
|
"prettier --write *.ts */*.ts */**/*.ts *.md */**/*.md"
|
||||||
|
]
|
||||||
});
|
});
|
||||||
const s = await prettier.status();
|
const s = await prettier.status();
|
||||||
exit(s.code);
|
exit(s.code);
|
||||||
|
|
|
@ -20,19 +20,17 @@ export class BaseHandler {
|
||||||
return this.log(msg);
|
return this.log(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
log(msg: string) { }
|
log(msg: string) {}
|
||||||
async setup() { }
|
async setup() {}
|
||||||
async destroy() { }
|
async destroy() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export class ConsoleHandler extends BaseHandler {
|
export class ConsoleHandler extends BaseHandler {
|
||||||
log(msg: string) {
|
log(msg: string) {
|
||||||
console.log(msg);
|
console.log(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export abstract class WriterHandler extends BaseHandler {
|
export abstract class WriterHandler extends BaseHandler {
|
||||||
protected _writer: Writer;
|
protected _writer: Writer;
|
||||||
|
|
||||||
|
@ -43,7 +41,6 @@ export abstract class WriterHandler extends BaseHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export class FileHandler extends WriterHandler {
|
export class FileHandler extends WriterHandler {
|
||||||
private _file: File;
|
private _file: File;
|
||||||
private _filename: string;
|
private _filename: string;
|
||||||
|
@ -55,11 +52,11 @@ export class FileHandler extends WriterHandler {
|
||||||
|
|
||||||
async setup() {
|
async setup() {
|
||||||
// open file in append mode - write only
|
// open file in append mode - write only
|
||||||
this._file = await open(this._filename, 'a');
|
this._file = await open(this._filename, "a");
|
||||||
this._writer = this._file;
|
this._writer = this._file;
|
||||||
}
|
}
|
||||||
|
|
||||||
async destroy() {
|
async destroy() {
|
||||||
await this._file.close();
|
await this._file.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
import { Logger } from "./logger.ts";
|
import { Logger } from "./logger.ts";
|
||||||
import { BaseHandler, ConsoleHandler, WriterHandler, FileHandler } from "./handlers.ts";
|
import {
|
||||||
|
BaseHandler,
|
||||||
|
ConsoleHandler,
|
||||||
|
WriterHandler,
|
||||||
|
FileHandler
|
||||||
|
} from "./handlers.ts";
|
||||||
|
|
||||||
export class LoggerConfig {
|
export class LoggerConfig {
|
||||||
level?: string;
|
level?: string;
|
||||||
|
@ -18,14 +23,12 @@ export interface LogConfig {
|
||||||
const DEFAULT_LEVEL = "INFO";
|
const DEFAULT_LEVEL = "INFO";
|
||||||
const DEFAULT_NAME = "";
|
const DEFAULT_NAME = "";
|
||||||
const DEFAULT_CONFIG: LogConfig = {
|
const DEFAULT_CONFIG: LogConfig = {
|
||||||
handlers: {
|
handlers: {},
|
||||||
|
|
||||||
},
|
|
||||||
|
|
||||||
loggers: {
|
loggers: {
|
||||||
"": {
|
"": {
|
||||||
level: "INFO",
|
level: "INFO",
|
||||||
handlers: [""],
|
handlers: [""]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -38,21 +41,26 @@ const state = {
|
||||||
defaultLogger,
|
defaultLogger,
|
||||||
handlers: new Map(),
|
handlers: new Map(),
|
||||||
loggers: new Map(),
|
loggers: new Map(),
|
||||||
config: DEFAULT_CONFIG,
|
config: DEFAULT_CONFIG
|
||||||
};
|
};
|
||||||
|
|
||||||
export const handlers = {
|
export const handlers = {
|
||||||
BaseHandler,
|
BaseHandler,
|
||||||
ConsoleHandler,
|
ConsoleHandler,
|
||||||
WriterHandler,
|
WriterHandler,
|
||||||
FileHandler,
|
FileHandler
|
||||||
};
|
};
|
||||||
|
|
||||||
export const debug = (msg: string, ...args: any[]) => defaultLogger.debug(msg, ...args);
|
export const debug = (msg: string, ...args: any[]) =>
|
||||||
export const info = (msg: string, ...args: any[]) => defaultLogger.info(msg, ...args);
|
defaultLogger.debug(msg, ...args);
|
||||||
export const warning = (msg: string, ...args: any[]) => defaultLogger.warning(msg, ...args);
|
export const info = (msg: string, ...args: any[]) =>
|
||||||
export const error = (msg: string, ...args: any[]) => defaultLogger.error(msg, ...args);
|
defaultLogger.info(msg, ...args);
|
||||||
export const critical = (msg: string, ...args: any[]) => defaultLogger.critical(msg, ...args);
|
export const warning = (msg: string, ...args: any[]) =>
|
||||||
|
defaultLogger.warning(msg, ...args);
|
||||||
|
export const error = (msg: string, ...args: any[]) =>
|
||||||
|
defaultLogger.error(msg, ...args);
|
||||||
|
export const critical = (msg: string, ...args: any[]) =>
|
||||||
|
defaultLogger.critical(msg, ...args);
|
||||||
|
|
||||||
export function getLogger(name?: string) {
|
export function getLogger(name?: string) {
|
||||||
if (!name) {
|
if (!name) {
|
||||||
|
@ -108,4 +116,4 @@ export async function setup(config: LogConfig) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setup(DEFAULT_CONFIG);
|
setup(DEFAULT_CONFIG);
|
||||||
|
|
|
@ -7,7 +7,7 @@ export interface LogRecord {
|
||||||
datetime: Date;
|
datetime: Date;
|
||||||
level: number;
|
level: number;
|
||||||
levelName: string;
|
levelName: string;
|
||||||
};
|
}
|
||||||
|
|
||||||
export class Logger {
|
export class Logger {
|
||||||
level: number;
|
level: number;
|
||||||
|
@ -17,14 +17,14 @@ export class Logger {
|
||||||
constructor(levelName: string, handlers?: BaseHandler[]) {
|
constructor(levelName: string, handlers?: BaseHandler[]) {
|
||||||
this.level = getLevelByName(levelName);
|
this.level = getLevelByName(levelName);
|
||||||
this.levelName = levelName;
|
this.levelName = levelName;
|
||||||
|
|
||||||
this.handlers = handlers || [];
|
this.handlers = handlers || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
_log(level: number, msg: string, ...args: any[]) {
|
_log(level: number, msg: string, ...args: any[]) {
|
||||||
if (this.level > level) return;
|
if (this.level > level) return;
|
||||||
|
|
||||||
// TODO: it'd be a good idea to make it immutable, so
|
// TODO: it'd be a good idea to make it immutable, so
|
||||||
// no handler mangles it by mistake
|
// no handler mangles it by mistake
|
||||||
// TODO: iterpolate msg with values
|
// TODO: iterpolate msg with values
|
||||||
const record: LogRecord = {
|
const record: LogRecord = {
|
||||||
|
@ -32,8 +32,8 @@ export class Logger {
|
||||||
args: args,
|
args: args,
|
||||||
datetime: new Date(),
|
datetime: new Date(),
|
||||||
level: level,
|
level: level,
|
||||||
levelName: getLevelName(level),
|
levelName: getLevelName(level)
|
||||||
}
|
};
|
||||||
|
|
||||||
this.handlers.forEach(handler => {
|
this.handlers.forEach(handler => {
|
||||||
handler.handle(record);
|
handler.handle(record);
|
||||||
|
|
|
@ -23,7 +23,6 @@ test(function testDefaultlogMethods() {
|
||||||
log.error("Foobar");
|
log.error("Foobar");
|
||||||
log.critical("Foobar");
|
log.critical("Foobar");
|
||||||
|
|
||||||
const logger = log.getLogger('');
|
const logger = log.getLogger("");
|
||||||
console.log(logger);
|
console.log(logger);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,7 @@
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
import { Buffer, Reader, ReadResult } from "deno";
|
import { Buffer, Reader, ReadResult } from "deno";
|
||||||
import {
|
import { test, assert, assertEqual } from "../testing/mod.ts";
|
||||||
test,
|
|
||||||
assert,
|
|
||||||
assertEqual
|
|
||||||
} from "../testing/mod.ts";
|
|
||||||
import { BufReader, BufState, BufWriter } from "./bufio.ts";
|
import { BufReader, BufState, BufWriter } from "./bufio.ts";
|
||||||
import * as iotest from "./iotest.ts";
|
import * as iotest from "./iotest.ts";
|
||||||
import { charCode, copyBytes, stringsReader } from "./util.ts";
|
import { charCode, copyBytes, stringsReader } from "./util.ts";
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
import { readFile } from "deno";
|
import { readFile } from "deno";
|
||||||
|
|
||||||
import {
|
import { test, assert, assertEqual } from "../testing/mod.ts";
|
||||||
test,
|
|
||||||
assert,
|
|
||||||
assertEqual
|
|
||||||
} from "../testing/mod.ts";
|
|
||||||
|
|
||||||
// Promise to completeResolve when all tests completes
|
// Promise to completeResolve when all tests completes
|
||||||
let completeResolve;
|
let completeResolve;
|
||||||
|
@ -27,7 +23,9 @@ export function runTests(serverReadyPromise: Promise<any>) {
|
||||||
assert(res.headers.has("access-control-allow-headers"));
|
assert(res.headers.has("access-control-allow-headers"));
|
||||||
assertEqual(res.headers.get("content-type"), "text/yaml");
|
assertEqual(res.headers.get("content-type"), "text/yaml");
|
||||||
const downloadedFile = await res.text();
|
const downloadedFile = await res.text();
|
||||||
const localFile = new TextDecoder().decode(await readFile("./azure-pipelines.yml"));
|
const localFile = new TextDecoder().decode(
|
||||||
|
await readFile("./azure-pipelines.yml")
|
||||||
|
);
|
||||||
assertEqual(downloadedFile, localFile);
|
assertEqual(downloadedFile, localFile);
|
||||||
maybeCompleteTests();
|
maybeCompleteTests();
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,11 +6,7 @@
|
||||||
// https://github.com/golang/go/blob/master/src/net/http/responsewrite_test.go
|
// https://github.com/golang/go/blob/master/src/net/http/responsewrite_test.go
|
||||||
|
|
||||||
import { Buffer } from "deno";
|
import { Buffer } from "deno";
|
||||||
import {
|
import { test, assert, assertEqual } from "../testing/mod.ts";
|
||||||
test,
|
|
||||||
assert,
|
|
||||||
assertEqual
|
|
||||||
} from "../testing/mod.ts";
|
|
||||||
import {
|
import {
|
||||||
listenAndServe,
|
listenAndServe,
|
||||||
ServerRequest,
|
ServerRequest,
|
||||||
|
|
|
@ -6,11 +6,7 @@
|
||||||
import { BufReader } from "./bufio.ts";
|
import { BufReader } from "./bufio.ts";
|
||||||
import { TextProtoReader, append } from "./textproto.ts";
|
import { TextProtoReader, append } from "./textproto.ts";
|
||||||
import { stringsReader } from "./util.ts";
|
import { stringsReader } from "./util.ts";
|
||||||
import {
|
import { test, assert, assertEqual } from "../testing/mod.ts";
|
||||||
test,
|
|
||||||
assert,
|
|
||||||
assertEqual
|
|
||||||
} from "../testing/mod.ts";
|
|
||||||
|
|
||||||
function reader(s: string): TextProtoReader {
|
function reader(s: string): TextProtoReader {
|
||||||
return new TextProtoReader(new BufReader(stringsReader(s)));
|
return new TextProtoReader(new BufReader(stringsReader(s)));
|
||||||
|
|
|
@ -1,24 +1,30 @@
|
||||||
# Testing
|
# Testing
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { test, assert, equal, assertEqual } from 'https://deno.land/x/testing/mod.ts';
|
import {
|
||||||
|
test,
|
||||||
|
assert,
|
||||||
|
equal,
|
||||||
|
assertEqual
|
||||||
|
} from "https://deno.land/x/testing/mod.ts";
|
||||||
|
|
||||||
test({
|
test({
|
||||||
name: 'testing example',
|
name: "testing example",
|
||||||
fn() {
|
fn() {
|
||||||
assert(equal("world", "world"));
|
assert(equal("world", "world"));
|
||||||
assert(!equal("hello", "world"));
|
assert(!equal("hello", "world"));
|
||||||
assert(equal({ hello: "world" }, { hello: "world" }));
|
assert(equal({ hello: "world" }, { hello: "world" }));
|
||||||
assert(!equal({ world: "hello" }, { hello: "world" }));
|
assert(!equal({ world: "hello" }, { hello: "world" }));
|
||||||
assertEqual("world", "world");
|
assertEqual("world", "world");
|
||||||
assertEqual({hello: "world"}, {hello: "world"});
|
assertEqual({ hello: "world" }, { hello: "world" });
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
Short syntax (named function instead of object):
|
Short syntax (named function instead of object):
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
test(function example() {
|
test(function example() {
|
||||||
assert(equal("world", "world"));
|
assert(equal("world", "world"));
|
||||||
|
@ -26,6 +32,6 @@ test(function example() {
|
||||||
assert(equal({ hello: "world" }, { hello: "world" }));
|
assert(equal({ hello: "world" }, { hello: "world" }));
|
||||||
assert(!equal({ world: "hello" }, { hello: "world" }));
|
assert(!equal({ world: "hello" }, { hello: "world" }));
|
||||||
assertEqual("world", "world");
|
assertEqual("world", "world");
|
||||||
assertEqual({hello: "world"}, {hello: "world"});
|
assertEqual({ hello: "world" }, { hello: "world" });
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in a new issue