Skip to content

Commit

Permalink
Don't rely on .chdir() in @babel/node and @babel/cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 14, 2021
1 parent 5a6ee69 commit 1c17894
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
18 changes: 4 additions & 14 deletions packages/babel-cli/test/index.js
Expand Up @@ -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);
});
};

Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -194,24 +194,14 @@ 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));
}
} else {
fs.mkdirSync(tmpLoc);
}

process.chdir(tmpLoc);
});

afterEach(() => {
process.chdir(cwd);
});

fs.readdirSync(suiteLoc).forEach(function (testName) {
Expand Down
16 changes: 3 additions & 13 deletions packages/babel-node/test/fixtures.js
Expand Up @@ -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);
});
};

Expand Down Expand Up @@ -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"];
}
Expand Down Expand Up @@ -153,24 +153,14 @@ 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));
}
} else {
fs.mkdirSync(tmpLoc);
}

process.chdir(tmpLoc);
});

afterEach(() => {
process.chdir(cwd);
});

fs.readdirSync(fixtureLoc).forEach(function (testName) {
Expand Down

0 comments on commit 1c17894

Please sign in to comment.