Refactor CLI tests (#13208)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-08-03 14:19:00 +08:00
1 parent cdb1494170
commit 55779bb96c
4 files changed
+46 -53

No files matched your search

+23 -26
View File
@@ -2,7 +2,6 @@ import path from "node:path";
import { promises as fs } from "node:fs";
import { fileURLToPath } from "node:url";
import rimraf from "rimraf";
import stripAnsi from "strip-ansi";
function resolveDir(dir) {
return fileURLToPath(new URL(`../${dir}/`, import.meta.url));
@@ -40,7 +39,7 @@ describe("--cache option", () => {
"invalid",
".",
]);
expect(stripAnsi(stderr.trim())).toBe(
expect(stderr.trim()).toBe(
'[error] Invalid --cache-strategy value. Expected "content" or "metadata", but received "invalid".'
);
});
@@ -51,9 +50,7 @@ describe("--cache option", () => {
["--cache", "--stdin-filepath", "foo.js"],
{ input: "const a = a;" }
);
expect(stripAnsi(stderr.trim())).toBe(
"[error] `--cache` cannot be used with stdin."
);
expect(stderr.trim()).toBe("[error] `--cache` cannot be used with stdin.");
});
it("throws error when use `--cache-strategy` without `--cache`.", async () => {
@@ -64,7 +61,7 @@ describe("--cache option", () => {
input: "const a = a;",
}
);
expect(stripAnsi(stderr.trim())).toBe(
expect(stderr.trim()).toBe(
"[error] `--cache-strategy` cannot be used without `--cache`."
);
});
@@ -76,7 +73,7 @@ describe("--cache option", () => {
"--cache-location",
"dir",
]);
expect(stripAnsi(stderr.trim())).toEqual(
expect(stderr.trim()).toEqual(
expect.stringMatching(
/\[error] Resolved --cache-location '.+' is a directory/
)
@@ -101,7 +98,7 @@ describe("--cache option", () => {
"metadata",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect(firstStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -115,7 +112,7 @@ describe("--cache option", () => {
"metadata",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect(secondStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/),
expect.stringMatching(/^b\.js .+ms \(cached\)$/),
@@ -131,7 +128,7 @@ describe("--cache option", () => {
"metadata",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect(firstStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -148,7 +145,7 @@ describe("--cache option", () => {
"metadata",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect(secondStdout.split("\n").filter(Boolean)).toEqual(
// the cache of `b.js` is only available.
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
@@ -165,7 +162,7 @@ describe("--cache option", () => {
"metadata",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect(firstStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -183,7 +180,7 @@ describe("--cache option", () => {
"metadata",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect(secondStdout.split("\n").filter(Boolean)).toEqual(
// the cache of `b.js` is only available.
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
@@ -200,7 +197,7 @@ describe("--cache option", () => {
"metadata",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect(firstStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -216,7 +213,7 @@ describe("--cache option", () => {
"all",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect(secondStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -256,7 +253,7 @@ describe("--cache option", () => {
"--write",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect(firstStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -270,7 +267,7 @@ describe("--cache option", () => {
"--write",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect(secondStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/),
expect.stringMatching(/^b\.js .+ms \(cached\)$/),
@@ -286,7 +283,7 @@ describe("--cache option", () => {
"--write",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect(firstStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -303,7 +300,7 @@ describe("--cache option", () => {
"--write",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect(secondStdout.split("\n").filter(Boolean)).toEqual(
// the cache of `b.js` is only available.
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
@@ -320,7 +317,7 @@ describe("--cache option", () => {
"--write",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect(firstStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -338,7 +335,7 @@ describe("--cache option", () => {
"--write",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect(secondStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/),
expect.stringMatching(/^b\.js .+ms \(cached\)$/),
@@ -354,7 +351,7 @@ describe("--cache option", () => {
"--write",
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect(firstStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -370,7 +367,7 @@ describe("--cache option", () => {
"all",
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect(secondStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -411,7 +408,7 @@ describe("--cache option", () => {
"a.js",
".",
]);
expect(stripAnsi(stderr).trim()).toEqual(
expect(stderr.trim()).toEqual(
expect.stringMatching(/\[error] '.+' isn't a valid JSON file/)
);
});
@@ -441,7 +438,7 @@ describe("--cache option", () => {
nonDefaultCacheFileName,
".",
]);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect(firstStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
@@ -455,7 +452,7 @@ describe("--cache option", () => {
nonDefaultCacheFileName,
".",
]);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect(secondStdout.split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/),
expect.stringMatching(/^b\.js .+ms \(cached\)$/),
+1 -2
View File
@@ -1,4 +1,3 @@
import stripAnsi from "strip-ansi";
test("do not show logs with --log-level silent", async () => {
await runPrettierWithLogLevel("silent", null);
});
@@ -124,7 +123,7 @@ async function runPrettierWithLogLevel(logLevel, patterns) {
expect(result.status).toBe(2);
const stderr = stripAnsi(result.stderr);
const { stderr } = result;
if (patterns) {
for (const pattern of patterns) {
+2 -1
View File
@@ -3,9 +3,10 @@ import fs from "node:fs";
import path from "node:path";
import url from "node:url";
import { cosmiconfig } from "cosmiconfig";
import { prettierCli, thirdParty as thirdPartyModuleFile } from "./env.js";
async function run() {
const { options, thirdParty: thirdPartyModuleFile, prettierCli } = workerData;
const { options } = workerData;
Date.now = () => 0;
// eslint-disable-next-line require-await
+20 -24
View File
@@ -1,11 +1,11 @@
import path from "node:path";
import url from "node:url";
import { Worker } from "node:worker_threads";
import stripAnsi from "strip-ansi";
import createEsmUtils from "esm-utils";
import { prettierCli, thirdParty } from "./env.js";
const { __dirname, require } = createEsmUtils(import.meta);
const CLI_WORKER_FILE = require.resolve("./cli-worker.js");
const CLI_WORKER_FILE = new URL("./cli-worker.js", import.meta.url);
const INTEGRATION_TEST_DIRECTORY = url.fileURLToPath(
new URL("./", import.meta.url)
);
const streamToString = (stream) =>
new Promise((resolve, reject) => {
@@ -38,12 +38,11 @@ function runCliWorker(dir, args, options) {
stdout: true,
stderr: true,
env: {
PRETTIER_DIR: process.env.PRETTIER_DIR,
...process.env,
NO_COLOR: 1,
},
workerData: {
dir,
prettierCli,
thirdParty,
options,
},
trackUnmanagedFds: false,
@@ -72,7 +71,7 @@ function runCliWorker(dir, args, options) {
}
async function run(dir, args, options) {
dir = normalizeDir(dir);
dir = path.resolve(INTEGRATION_TEST_DIRECTORY, dir);
args = Array.isArray(args) ? args : [args];
// Worker doesn't support `chdir`
@@ -80,7 +79,7 @@ async function run(dir, args, options) {
process.chdir(dir);
try {
return await runCliWorker(dir, args, options, cwd);
return await runCliWorker(dir, args, options);
} finally {
process.chdir(cwd);
}
@@ -128,15 +127,17 @@ function runPrettier(dir, args = [], options = {}) {
for (const name of ["status", "stdout", "stderr", "write"]) {
test(`${options.title || ""}(${name})`, async () => {
const result = await runCli();
const value =
// \r is trimmed from jest snapshots by default;
// manually replacing this character with /*CR*/ to test its true presence
// If ignoreLineEndings is specified, \r is simply deleted instead
typeof result[name] === "string"
? options.ignoreLineEndings
? stripAnsi(result[name]).replace(/\r/g, "")
: stripAnsi(result[name]).replace(/\r/g, "/*CR*/")
: result[name];
let value = result[name];
// \r is trimmed from jest snapshots by default;
// manually replacing this character with /*CR*/ to test its true presence
// If ignoreLineEndings is specified, \r is simply deleted instead
if (name === "stdout" || name === "stderr") {
value = result[name].replace(
/\r/g,
options.ignoreLineEndings ? "" : "/*CR*/"
);
}
if (name in testOptions) {
if (name === "status" && testOptions[name] === "non-zero") {
expect(value).not.toBe(0);
@@ -155,9 +156,4 @@ function runPrettier(dir, args = [], options = {}) {
}
}
function normalizeDir(dir) {
const isRelative = dir[0] !== "/";
return isRelative ? path.resolve(__dirname, dir) : dir;
}
export default runPrettier;