From e15760b29e16c7086006f31565b125dad344107d Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Sun, 2 Jun 2019 01:55:19 +0900 Subject: [PATCH 1/3] prettier: improve testing --- prettier/main_test.ts | 57 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index bbd7f4bc1499..56d120e23c3d 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -4,6 +4,7 @@ import { EOL } from "../fs/path/constants.ts"; import { assertEquals } from "../testing/asserts.ts"; import { test } from "../testing/mod.ts"; import { xrun } from "./util.ts"; +import { copy, emptyDir } from "../fs/mod.ts"; const { readAll, execPath } = Deno; const decoder = new TextDecoder(); @@ -43,17 +44,14 @@ function normalizeSourceCode(source: string): string { return source.replace(/\r/g, ""); } -async function clearTestdataChanges(): Promise { - await xrun({ args: ["git", "checkout", testdata] }).status(); -} - test(async function testPrettierCheckAndFormatFiles(): Promise { - await clearTestdataChanges(); + const tempDir = await Deno.makeTempDir(); + await copy(testdata, tempDir, { overwrite: true }); const files = [ - join(testdata, "0.ts"), - join(testdata, "1.js"), - join(testdata, "2.ts") + join(tempDir, "0.ts"), + join(tempDir, "1.js"), + join(tempDir, "2.ts") ]; var { code, stdout } = await run([...cmd, "--check", ...files]); @@ -64,21 +62,22 @@ test(async function testPrettierCheckAndFormatFiles(): Promise { assertEquals(code, 0); assertEquals( normalizeOutput(stdout), - `Formatting prettier/testdata/0.ts -Formatting prettier/testdata/1.js` + `Formatting ${tempDir}/0.ts +Formatting ${tempDir}/1.js` ); var { code, stdout } = await run([...cmd, "--check", ...files]); assertEquals(code, 0); assertEquals(normalizeOutput(stdout), "Every file is formatted"); - await clearTestdataChanges(); + emptyDir(tempDir); }); test(async function testPrettierCheckAndFormatDirs(): Promise { - await clearTestdataChanges(); + const tempDir = await Deno.makeTempDir(); + await copy(testdata, tempDir, { overwrite: true }); - const dirs = [join(testdata, "foo"), join(testdata, "bar")]; + const dirs = [join(tempDir, "foo"), join(tempDir, "bar")]; var { code, stdout } = await run([...cmd, "--check", ...dirs]); assertEquals(code, 1); @@ -88,26 +87,27 @@ test(async function testPrettierCheckAndFormatDirs(): Promise { assertEquals(code, 0); assertEquals( normalizeOutput(stdout), - `Formatting prettier/testdata/bar/0.ts -Formatting prettier/testdata/bar/1.js -Formatting prettier/testdata/foo/0.ts -Formatting prettier/testdata/foo/1.js` + `Formatting ${tempDir}/bar/0.ts +Formatting ${tempDir}/bar/1.js +Formatting ${tempDir}/foo/0.ts +Formatting ${tempDir}/foo/1.js` ); var { code, stdout } = await run([...cmd, "--check", ...dirs]); assertEquals(code, 0); assertEquals(normalizeOutput(stdout), "Every file is formatted"); - await clearTestdataChanges(); + emptyDir(tempDir); }); test(async function testPrettierOptions(): Promise { - await clearTestdataChanges(); + const tempDir = await Deno.makeTempDir(); + await copy(testdata, tempDir, { overwrite: true }); - const file0 = join(testdata, "opts", "0.ts"); - const file1 = join(testdata, "opts", "1.ts"); - const file2 = join(testdata, "opts", "2.ts"); - const file3 = join(testdata, "opts", "3.md"); + const file0 = join(tempDir, "opts", "0.ts"); + const file1 = join(tempDir, "opts", "1.ts"); + const file2 = join(tempDir, "opts", "2.ts"); + const file3 = join(tempDir, "opts", "3.md"); const getSourceCode = async (f: string): Promise => decoder.decode(await Deno.readFile(f)); @@ -205,14 +205,15 @@ incididunt ut labore et dolore magna aliqua. await run([...cmd, "--end-of-line", "crlf", "--write", file2]); assertEquals(await getSourceCode(file2), "console.log({ a: 1 });\r\n"); - await clearTestdataChanges(); + emptyDir(tempDir); }); test(async function testPrettierPrintToStdout(): Promise { - await clearTestdataChanges(); + const tempDir = await Deno.makeTempDir(); + await copy(testdata, tempDir, { overwrite: true }); - const file0 = join(testdata, "0.ts"); - const file1 = join(testdata, "formatted.ts"); + const file0 = join(tempDir, "0.ts"); + const file1 = join(tempDir, "formatted.ts"); const getSourceCode = async (f: string): Promise => decoder.decode(await Deno.readFile(f)); @@ -229,5 +230,5 @@ test(async function testPrettierPrintToStdout(): Promise { // The output will be formatted code even it is the same as the source file's content. assertEquals(formattedCode, "console.log(0);" + EOL); - await clearTestdataChanges(); + emptyDir(tempDir); }); From 40d2e17b50c5d00512a5249f3cb9db9b02cccd23 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Sun, 2 Jun 2019 11:45:15 +0900 Subject: [PATCH 2/3] test: check source file changes --- prettier/main_test.ts | 8 ++++---- test.ts | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/prettier/main_test.ts b/prettier/main_test.ts index 56d120e23c3d..13340b04ee1e 100644 --- a/prettier/main_test.ts +++ b/prettier/main_test.ts @@ -62,8 +62,8 @@ test(async function testPrettierCheckAndFormatFiles(): Promise { assertEquals(code, 0); assertEquals( normalizeOutput(stdout), - `Formatting ${tempDir}/0.ts -Formatting ${tempDir}/1.js` + normalizeOutput(`Formatting ${tempDir}/0.ts +Formatting ${tempDir}/1.js`) ); var { code, stdout } = await run([...cmd, "--check", ...files]); @@ -87,10 +87,10 @@ test(async function testPrettierCheckAndFormatDirs(): Promise { assertEquals(code, 0); assertEquals( normalizeOutput(stdout), - `Formatting ${tempDir}/bar/0.ts + normalizeOutput(`Formatting ${tempDir}/bar/0.ts Formatting ${tempDir}/bar/1.js Formatting ${tempDir}/foo/0.ts -Formatting ${tempDir}/foo/1.js` +Formatting ${tempDir}/foo/1.js`) ); var { code, stdout } = await run([...cmd, "--check", ...dirs]); diff --git a/test.ts b/test.ts index 478ae61f88b7..9e3666cc7d90 100755 --- a/test.ts +++ b/test.ts @@ -21,4 +21,43 @@ import "./textproto/test.ts"; import "./util/test.ts"; import "./ws/test.ts"; -import "./testing/main.ts"; +import { xrun } from "./prettier/util.ts"; +import { red, green } from "./colors/mod.ts"; +import { runTests } from "./testing/mod.ts"; + +async function run(): Promise { + const startTime = Date.now(); + await runTests(); + await checkSourceFileChanges(startTime); +} + +/** + * Checks whether any source file is changed since the given start time. + * If some files are changed, this function exits with 1. + */ +async function checkSourceFileChanges(startTime: number): Promise { + await Deno.stdout.write( + new TextEncoder().encode("test checkSourceFileChanges ... ") + ); + const changed = new TextDecoder() + .decode(await xrun({ args: ["git", "ls-files"], stdout: "piped" }).output()) + .trim() + .split("\n") + .filter(file => { + const stat = Deno.lstatSync(file); + if (stat != null) { + return (stat as any).modified * 1000 > startTime; + } + }); + if (changed.length > 0) { + console.log(red("FAILED")); + console.log( + `Error: Some source files are modified during test: ${changed.join(", ")}` + ); + Deno.exit(1); + } else { + console.log(green("ok")); + } +} + +run(); From 41d30837825683160eb2e80daca38ffbd2c476c8 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Mon, 3 Jun 2019 08:25:03 +0900 Subject: [PATCH 3/3] refactor: use console.log --- test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test.ts b/test.ts index 9e3666cc7d90..edaca7a21362 100755 --- a/test.ts +++ b/test.ts @@ -36,9 +36,7 @@ async function run(): Promise { * If some files are changed, this function exits with 1. */ async function checkSourceFileChanges(startTime: number): Promise { - await Deno.stdout.write( - new TextEncoder().encode("test checkSourceFileChanges ... ") - ); + console.log("test checkSourceFileChanges ..."); const changed = new TextDecoder() .decode(await xrun({ args: ["git", "ls-files"], stdout: "piped" }).output()) .trim()