Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't chdir when run tests #1743

Draft
wants to merge 4 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
},
"scripts": {
"prepublishOnly": "echo \"Error: must publish from dist/\" && exit 1",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"test:dev-package": "cross-env INSTALL_PACKAGE=1 yarn test",
"test:dist": "cross-env NODE_ENV=production yarn test",
"test:dist-standalone": "cross-env TEST_STANDALONE=1 yarn test:dist",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ async function formatFiles(context) {

let input;
try {
input = await fs.readFile(filename, "utf8");
input = await fs.readFile(path.join(process.cwd(), filename), "utf8");
} catch (error) {
// Add newline to split errors from filename line.
/* istanbul ignore next */
Expand Down
12 changes: 11 additions & 1 deletion tests/integration/cli-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import url from "node:url";
import { cosmiconfig } from "cosmiconfig";
import { prettierCli, thirdParty as thirdPartyModuleFile } from "./env.js";

const INTEGRATION_TEST_DIRECTORY = url.fileURLToPath(
new URL("./", import.meta.url)
);

async function run() {
const { options } = workerData;
let { dir, options } = workerData;

dir = path.resolve(INTEGRATION_TEST_DIRECTORY, dir);

Date.now = () => 0;
// eslint-disable-next-line require-await
Expand Down Expand Up @@ -35,6 +41,10 @@ async function run() {

process.stdin.isTTY = Boolean(options.isTTY);
process.stdout.isTTY = Boolean(options.stdoutIsTTY);
process.cwd = () => dir;
process.chdir = () => {
throw new Error("Can't call `process.chdir()` in worker.")
};

const { default: thirdParty } = await import(
url.pathToFileURL(thirdPartyModuleFile)
Expand Down
18 changes: 2 additions & 16 deletions tests/integration/run-prettier.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import path from "node:path";
import url from "node:url";
import { Worker } from "node:worker_threads";

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) => {
Expand Down Expand Up @@ -70,19 +65,10 @@ function runCliWorker(dir, args, options) {
});
}

async function run(dir, args, options) {
dir = path.resolve(INTEGRATION_TEST_DIRECTORY, dir);
function run(dir, args, options) {
args = Array.isArray(args) ? args : [args];

// Worker doesn't support `chdir`
const cwd = process.cwd();
process.chdir(dir);

try {
return await runCliWorker(dir, args, options);
} finally {
process.chdir(cwd);
}
return runCliWorker(dir, args, options);
}

let runningCli;
Expand Down