mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 20:59:10 +00:00
Check file changes during test (denoland/deno_std#476)
Original: 7daa887b09
This commit is contained in:
parent
e729d442fb
commit
21684c679b
2 changed files with 67 additions and 29 deletions
|
@ -4,6 +4,7 @@ import { EOL } from "../fs/path/constants.ts";
|
||||||
import { assertEquals } from "../testing/asserts.ts";
|
import { assertEquals } from "../testing/asserts.ts";
|
||||||
import { test } from "../testing/mod.ts";
|
import { test } from "../testing/mod.ts";
|
||||||
import { xrun } from "./util.ts";
|
import { xrun } from "./util.ts";
|
||||||
|
import { copy, emptyDir } from "../fs/mod.ts";
|
||||||
const { readAll, execPath } = Deno;
|
const { readAll, execPath } = Deno;
|
||||||
|
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
|
@ -43,17 +44,14 @@ function normalizeSourceCode(source: string): string {
|
||||||
return source.replace(/\r/g, "");
|
return source.replace(/\r/g, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function clearTestdataChanges(): Promise<void> {
|
|
||||||
await xrun({ args: ["git", "checkout", testdata] }).status();
|
|
||||||
}
|
|
||||||
|
|
||||||
test(async function testPrettierCheckAndFormatFiles(): Promise<void> {
|
test(async function testPrettierCheckAndFormatFiles(): Promise<void> {
|
||||||
await clearTestdataChanges();
|
const tempDir = await Deno.makeTempDir();
|
||||||
|
await copy(testdata, tempDir, { overwrite: true });
|
||||||
|
|
||||||
const files = [
|
const files = [
|
||||||
join(testdata, "0.ts"),
|
join(tempDir, "0.ts"),
|
||||||
join(testdata, "1.js"),
|
join(tempDir, "1.js"),
|
||||||
join(testdata, "2.ts")
|
join(tempDir, "2.ts")
|
||||||
];
|
];
|
||||||
|
|
||||||
var { code, stdout } = await run([...cmd, "--check", ...files]);
|
var { code, stdout } = await run([...cmd, "--check", ...files]);
|
||||||
|
@ -64,21 +62,22 @@ test(async function testPrettierCheckAndFormatFiles(): Promise<void> {
|
||||||
assertEquals(code, 0);
|
assertEquals(code, 0);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
normalizeOutput(stdout),
|
normalizeOutput(stdout),
|
||||||
`Formatting prettier/testdata/0.ts
|
normalizeOutput(`Formatting ${tempDir}/0.ts
|
||||||
Formatting prettier/testdata/1.js`
|
Formatting ${tempDir}/1.js`)
|
||||||
);
|
);
|
||||||
|
|
||||||
var { code, stdout } = await run([...cmd, "--check", ...files]);
|
var { code, stdout } = await run([...cmd, "--check", ...files]);
|
||||||
assertEquals(code, 0);
|
assertEquals(code, 0);
|
||||||
assertEquals(normalizeOutput(stdout), "Every file is formatted");
|
assertEquals(normalizeOutput(stdout), "Every file is formatted");
|
||||||
|
|
||||||
await clearTestdataChanges();
|
emptyDir(tempDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
test(async function testPrettierCheckAndFormatDirs(): Promise<void> {
|
test(async function testPrettierCheckAndFormatDirs(): Promise<void> {
|
||||||
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]);
|
var { code, stdout } = await run([...cmd, "--check", ...dirs]);
|
||||||
assertEquals(code, 1);
|
assertEquals(code, 1);
|
||||||
|
@ -88,26 +87,27 @@ test(async function testPrettierCheckAndFormatDirs(): Promise<void> {
|
||||||
assertEquals(code, 0);
|
assertEquals(code, 0);
|
||||||
assertEquals(
|
assertEquals(
|
||||||
normalizeOutput(stdout),
|
normalizeOutput(stdout),
|
||||||
`Formatting prettier/testdata/bar/0.ts
|
normalizeOutput(`Formatting ${tempDir}/bar/0.ts
|
||||||
Formatting prettier/testdata/bar/1.js
|
Formatting ${tempDir}/bar/1.js
|
||||||
Formatting prettier/testdata/foo/0.ts
|
Formatting ${tempDir}/foo/0.ts
|
||||||
Formatting prettier/testdata/foo/1.js`
|
Formatting ${tempDir}/foo/1.js`)
|
||||||
);
|
);
|
||||||
|
|
||||||
var { code, stdout } = await run([...cmd, "--check", ...dirs]);
|
var { code, stdout } = await run([...cmd, "--check", ...dirs]);
|
||||||
assertEquals(code, 0);
|
assertEquals(code, 0);
|
||||||
assertEquals(normalizeOutput(stdout), "Every file is formatted");
|
assertEquals(normalizeOutput(stdout), "Every file is formatted");
|
||||||
|
|
||||||
await clearTestdataChanges();
|
emptyDir(tempDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
test(async function testPrettierOptions(): Promise<void> {
|
test(async function testPrettierOptions(): Promise<void> {
|
||||||
await clearTestdataChanges();
|
const tempDir = await Deno.makeTempDir();
|
||||||
|
await copy(testdata, tempDir, { overwrite: true });
|
||||||
|
|
||||||
const file0 = join(testdata, "opts", "0.ts");
|
const file0 = join(tempDir, "opts", "0.ts");
|
||||||
const file1 = join(testdata, "opts", "1.ts");
|
const file1 = join(tempDir, "opts", "1.ts");
|
||||||
const file2 = join(testdata, "opts", "2.ts");
|
const file2 = join(tempDir, "opts", "2.ts");
|
||||||
const file3 = join(testdata, "opts", "3.md");
|
const file3 = join(tempDir, "opts", "3.md");
|
||||||
|
|
||||||
const getSourceCode = async (f: string): Promise<string> =>
|
const getSourceCode = async (f: string): Promise<string> =>
|
||||||
decoder.decode(await Deno.readFile(f));
|
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]);
|
await run([...cmd, "--end-of-line", "crlf", "--write", file2]);
|
||||||
assertEquals(await getSourceCode(file2), "console.log({ a: 1 });\r\n");
|
assertEquals(await getSourceCode(file2), "console.log({ a: 1 });\r\n");
|
||||||
|
|
||||||
await clearTestdataChanges();
|
emptyDir(tempDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
test(async function testPrettierPrintToStdout(): Promise<void> {
|
test(async function testPrettierPrintToStdout(): Promise<void> {
|
||||||
await clearTestdataChanges();
|
const tempDir = await Deno.makeTempDir();
|
||||||
|
await copy(testdata, tempDir, { overwrite: true });
|
||||||
|
|
||||||
const file0 = join(testdata, "0.ts");
|
const file0 = join(tempDir, "0.ts");
|
||||||
const file1 = join(testdata, "formatted.ts");
|
const file1 = join(tempDir, "formatted.ts");
|
||||||
|
|
||||||
const getSourceCode = async (f: string): Promise<string> =>
|
const getSourceCode = async (f: string): Promise<string> =>
|
||||||
decoder.decode(await Deno.readFile(f));
|
decoder.decode(await Deno.readFile(f));
|
||||||
|
@ -229,5 +230,5 @@ test(async function testPrettierPrintToStdout(): Promise<void> {
|
||||||
// The output will be formatted code even it is the same as the source file's content.
|
// The output will be formatted code even it is the same as the source file's content.
|
||||||
assertEquals(formattedCode, "console.log(0);" + EOL);
|
assertEquals(formattedCode, "console.log(0);" + EOL);
|
||||||
|
|
||||||
await clearTestdataChanges();
|
emptyDir(tempDir);
|
||||||
});
|
});
|
||||||
|
|
39
test.ts
39
test.ts
|
@ -21,4 +21,41 @@ import "./textproto/test.ts";
|
||||||
import "./util/test.ts";
|
import "./util/test.ts";
|
||||||
import "./ws/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<void> {
|
||||||
|
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<void> {
|
||||||
|
console.log("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();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue