From 1c17894d78afb657a4e531f4c6315f0cea53deac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 14 Nov 2021 22:25:24 +0100 Subject: [PATCH] Don't rely on `.chdir()` in `@babel/node` and `@babel/cli` tests --- packages/babel-cli/test/index.js | 18 ++++-------------- packages/babel-node/test/fixtures.js | 16 +++------------- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/packages/babel-cli/test/index.js b/packages/babel-cli/test/index.js index 2e661c5fe2e4..85395b30a169 100644 --- a/packages/babel-cli/test/index.js +++ b/packages/babel-cli/test/index.js @@ -45,13 +45,13 @@ const readDir = function (loc, filter) { const saveInFiles = function (files) { // Place an empty .babelrc in each test so tests won't unexpectedly get to repo-level config. - if (!fs.existsSync(".babelrc")) { - outputFileSync(".babelrc", "{}"); + if (!fs.existsSync(path.join(tmpLoc, ".babelrc"))) { + outputFileSync(path.join(tmpLoc, ".babelrc"), "{}"); } Object.keys(files).forEach(function (filename) { const content = files[filename]; - outputFileSync(filename, content); + outputFileSync(path.join(tmpLoc, filename), content); }); }; @@ -152,7 +152,7 @@ const buildTest = function (binName, testName, opts) { args = args.concat(opts.args); const env = { ...process.env, ...opts.env }; - const spawn = child.spawn(process.execPath, args, { env }); + const spawn = child.spawn(process.execPath, args, { env, cwd: tmpLoc }); let stderr = ""; let stdout = ""; @@ -194,11 +194,7 @@ fs.readdirSync(fixtureLoc).forEach(function (binName) { const suiteLoc = path.join(fixtureLoc, binName); describe("bin/" + binName, function () { - let cwd; - beforeEach(() => { - cwd = process.cwd(); - if (fs.existsSync(tmpLoc)) { for (const child of fs.readdirSync(tmpLoc)) { rimraf.sync(path.join(tmpLoc, child)); @@ -206,12 +202,6 @@ fs.readdirSync(fixtureLoc).forEach(function (binName) { } else { fs.mkdirSync(tmpLoc); } - - process.chdir(tmpLoc); - }); - - afterEach(() => { - process.chdir(cwd); }); fs.readdirSync(suiteLoc).forEach(function (testName) { diff --git a/packages/babel-node/test/fixtures.js b/packages/babel-node/test/fixtures.js index 0518798fb642..5f25279a4b6e 100644 --- a/packages/babel-node/test/fixtures.js +++ b/packages/babel-node/test/fixtures.js @@ -35,11 +35,11 @@ const readDir = function (loc, filter) { const saveInFiles = function (files) { // Place an empty .babelrc in each test so tests won't unexpectedly get to repo-level config. - outputFileSync(".babelrc", "{}"); + outputFileSync(path.join(tmpLoc, ".babelrc"), "{}"); Object.keys(files).forEach(function (filename) { const content = files[filename]; - outputFileSync(filename, content); + outputFileSync(path.join(tmpLoc, filename), content); }); }; @@ -102,7 +102,7 @@ const buildTest = function (testName, opts) { saveInFiles(opts.inFiles); const args = [binLoc].concat(opts.args); - const spawnOpts = {}; + const spawnOpts = { cwd: tmpLoc }; if (opts.ipc) { spawnOpts.stdio = ["pipe", "pipe", "pipe", "ipc"]; } @@ -153,11 +153,7 @@ const buildTest = function (testName, opts) { }; describe("bin/babel-node", function () { - let cwd; - beforeEach(() => { - cwd = process.cwd(); - if (fs.existsSync(tmpLoc)) { for (const child of fs.readdirSync(tmpLoc)) { rimraf.sync(path.join(tmpLoc, child)); @@ -165,12 +161,6 @@ describe("bin/babel-node", function () { } else { fs.mkdirSync(tmpLoc); } - - process.chdir(tmpLoc); - }); - - afterEach(() => { - process.chdir(cwd); }); fs.readdirSync(fixtureLoc).forEach(function (testName) {