summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoshiya Hinosawa <stibium121@gmail.com>2019-01-29 01:17:00 +0900
committerRyan Dahl <ry@tinyclouds.org>2019-01-28 11:17:00 -0500
commitd2a336ee02649184c0e6c8e81bebc8e5fec1a4a0 (patch)
treefd88eeee5f18c8f598053695c50528aa53ebc729
parentb79d2a9d41963b3c7cfa382d842a86c68b940666 (diff)
fix: skip formatting of deleted files (denoland/deno_std#161)
Original: https://github.com/denoland/deno_std/commit/c75884560117cbff8864ae48df9d2a280ba2cece
-rwxr-xr-xformat.ts28
-rw-r--r--log/handlers_test.ts50
-rw-r--r--log/logger_test.ts17
-rw-r--r--log/mod.ts20
-rw-r--r--log/test.ts21
5 files changed, 71 insertions, 65 deletions
diff --git a/format.ts b/format.ts
index 681e67774..8cd3f9af8 100755
--- a/format.ts
+++ b/format.ts
@@ -38,6 +38,18 @@ async function getSourceFiles() {
.split(/\r?\n/);
}
+async function readFileIfExists(filename: string): Promise<string | null> {
+ let data;
+ try {
+ data = await readFile(filename);
+ } catch (e) {
+ // The file is deleted. Returns null.
+ return null;
+ }
+
+ return decoder.decode(data);
+}
+
/**
* Checks if the file has been formatted with prettier.
*/
@@ -45,7 +57,13 @@ async function checkFile(
filename: string,
parser: "typescript" | "markdown"
): Promise<boolean> {
- const text = decoder.decode(await readFile(filename));
+ const text = await readFileIfExists(filename);
+
+ if (!text) {
+ // The file is deleted. Skip.
+ return;
+ }
+
const formatted = prettier.check(text, {
parser,
plugins: prettierPlugins
@@ -66,7 +84,13 @@ async function formatFile(
filename: string,
parser: "typescript" | "markdown"
): Promise<void> {
- const text = decoder.decode(await readFile(filename));
+ const text = await readFileIfExists(filename);
+
+ if (!text) {
+ // The file is deleted. Skip.
+ return;
+ }
+
const formatted = prettier.format(text, {
parser,
plugins: prettierPlugins
diff --git a/log/handlers_test.ts b/log/handlers_test.ts
index 3cb34fca6..bbdf6e0a5 100644
--- a/log/handlers_test.ts
+++ b/log/handlers_test.ts
@@ -13,31 +13,31 @@ class TestHandler extends BaseHandler {
test(function simpleHandler() {
const cases = new Map<number, string[]>([
- [LogLevel.DEBUG, [
- "DEBUG debug-test",
- "INFO info-test",
- "WARNING warning-test",
- "ERROR error-test",
- "CRITICAL critical-test"
- ]],
- [LogLevel.INFO, [
- "INFO info-test",
- "WARNING warning-test",
- "ERROR error-test",
- "CRITICAL critical-test"
- ]],
- [LogLevel.WARNING, [
- "WARNING warning-test",
- "ERROR error-test",
- "CRITICAL critical-test"
- ]],
- [LogLevel.ERROR, [
- "ERROR error-test",
- "CRITICAL critical-test"
- ]],
- [LogLevel.CRITICAL, [
- "CRITICAL critical-test"
- ]]
+ [
+ LogLevel.DEBUG,
+ [
+ "DEBUG debug-test",
+ "INFO info-test",
+ "WARNING warning-test",
+ "ERROR error-test",
+ "CRITICAL critical-test"
+ ]
+ ],
+ [
+ LogLevel.INFO,
+ [
+ "INFO info-test",
+ "WARNING warning-test",
+ "ERROR error-test",
+ "CRITICAL critical-test"
+ ]
+ ],
+ [
+ LogLevel.WARNING,
+ ["WARNING warning-test", "ERROR error-test", "CRITICAL critical-test"]
+ ],
+ [LogLevel.ERROR, ["ERROR error-test", "CRITICAL critical-test"]],
+ [LogLevel.CRITICAL, ["CRITICAL critical-test"]]
]);
for (const [testCase, messages] of cases.entries()) {
diff --git a/log/logger_test.ts b/log/logger_test.ts
index 7a5484105..2456e9fc2 100644
--- a/log/logger_test.ts
+++ b/log/logger_test.ts
@@ -8,7 +8,7 @@ class TestHandler extends BaseHandler {
public records: LogRecord[] = [];
handle(record: LogRecord): void {
- this.records.push({...record, datetime: null });
+ this.records.push({ ...record, datetime: null });
super.handle(record);
}
@@ -83,22 +83,13 @@ test(function logFunctions() {
doLog("WARNING");
- assertEqual(handler.messages, [
- "WARNING baz",
- "ERROR boo",
- "CRITICAL doo"
- ]);
+ assertEqual(handler.messages, ["WARNING baz", "ERROR boo", "CRITICAL doo"]);
doLog("ERROR");
- assertEqual(handler.messages, [
- "ERROR boo",
- "CRITICAL doo"
- ]);
+ assertEqual(handler.messages, ["ERROR boo", "CRITICAL doo"]);
doLog("CRITICAL");
- assertEqual(handler.messages, [
- "CRITICAL doo"
- ]);
+ assertEqual(handler.messages, ["CRITICAL doo"]);
});
diff --git a/log/mod.ts b/log/mod.ts
index 596b72a37..821c2f82a 100644
--- a/log/mod.ts
+++ b/log/mod.ts
@@ -23,11 +23,11 @@ export interface LogConfig {
const DEFAULT_LEVEL = "INFO";
const DEFAULT_CONFIG: LogConfig = {
handlers: {
- "default": new ConsoleHandler(DEFAULT_LEVEL)
+ default: new ConsoleHandler(DEFAULT_LEVEL)
},
loggers: {
- "default": {
+ default: {
level: DEFAULT_LEVEL,
handlers: ["default"]
}
@@ -48,19 +48,19 @@ export const handlers = {
};
export const debug = (msg: string, ...args: any[]) =>
- getLogger('default').debug(msg, ...args);
+ getLogger("default").debug(msg, ...args);
export const info = (msg: string, ...args: any[]) =>
- getLogger('default').info(msg, ...args);
+ getLogger("default").info(msg, ...args);
export const warning = (msg: string, ...args: any[]) =>
- getLogger('default').warning(msg, ...args);
+ getLogger("default").warning(msg, ...args);
export const error = (msg: string, ...args: any[]) =>
- getLogger('default').error(msg, ...args);
+ getLogger("default").error(msg, ...args);
export const critical = (msg: string, ...args: any[]) =>
- getLogger('default').critical(msg, ...args);
+ getLogger("default").critical(msg, ...args);
export function getLogger(name?: string) {
if (!name) {
- return state.loggers.get('default');
+ return state.loggers.get("default");
}
if (!state.loggers.has(name)) {
@@ -78,8 +78,8 @@ export function getHandler(name: string) {
export async function setup(config: LogConfig) {
state.config = {
- handlers: {...DEFAULT_CONFIG.handlers, ...config.handlers},
- loggers: {...DEFAULT_CONFIG.loggers, ...config.loggers}
+ handlers: { ...DEFAULT_CONFIG.handlers, ...config.handlers },
+ loggers: { ...DEFAULT_CONFIG.loggers, ...config.loggers }
};
// tear down existing handlers
diff --git a/log/test.ts b/log/test.ts
index d64809605..3d803cc0c 100644
--- a/log/test.ts
+++ b/log/test.ts
@@ -52,10 +52,7 @@ test(async function defaultHandlers() {
logger("foo");
logger("bar", 1, 2);
- assertEqual(handler.messages, [
- `${levelName} foo`,
- `${levelName} bar`
- ]);
+ assertEqual(handler.messages, [`${levelName} foo`, `${levelName} bar`]);
}
});
@@ -64,7 +61,7 @@ test(async function getLogger() {
await log.setup({
handlers: {
- default: handler
+ default: handler
},
loggers: {
default: {
@@ -77,9 +74,7 @@ test(async function getLogger() {
const logger = log.getLogger();
assertEqual(logger.levelName, "DEBUG");
- assertEqual(logger.handlers, [
- handler
- ]);
+ assertEqual(logger.handlers, [handler]);
});
test(async function getLoggerWithName() {
@@ -100,17 +95,13 @@ test(async function getLoggerWithName() {
const logger = log.getLogger("bar");
assertEqual(logger.levelName, "INFO");
- assertEqual(logger.handlers, [
- fooHandler
- ]);
+ assertEqual(logger.handlers, [fooHandler]);
});
test(async function getLoggerUnknown() {
await log.setup({
- handlers: {
- },
- loggers: {
- }
+ handlers: {},
+ loggers: {}
});
const logger = log.getLogger("nonexistent");