Skip to content

Commit

Permalink
chore(tests): use separate envs for each tests (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Feb 12, 2024
1 parent fe6a307 commit 12f1c31
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 209 deletions.
27 changes: 9 additions & 18 deletions tests/Disable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {runCli} from './_runCli';
const engine = new Engine();

beforeEach(async () => {
// `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
});

Expand All @@ -26,15 +27,10 @@ describe(`DisableCommand`, () => {
for (const variant of getBinaryNames(binName))
await makeBin(cwd, variant as Filename, {ignorePlatform: true});

const PATH = process.env.PATH;
try {
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${PATH}`;
await expect(runCli(cwd, [`disable`])).resolves.toMatchObject({
exitCode: 0,
});
} finally {
process.env.PATH = PATH;
}
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
await expect(runCli(cwd, [`disable`])).resolves.toMatchObject({
exitCode: 0,
});

const sortedEntries = xfs.readdirPromise(cwd).then(entries => {
return entries.sort();
Expand Down Expand Up @@ -84,15 +80,10 @@ describe(`DisableCommand`, () => {
const dontRemoveBin = await makeBin(cwd, `dont-remove` as Filename);
binNames.add(ppath.basename(dontRemoveBin));

const PATH = process.env.PATH;
try {
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${PATH}`;
await expect(runCli(cwd, [`disable`, `yarn`])).resolves.toMatchObject({
exitCode: 0,
});
} finally {
process.env.PATH = PATH;
}
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
await expect(runCli(cwd, [`disable`, `yarn`])).resolves.toMatchObject({
exitCode: 0,
});

for (const variant of getBinaryNames(`yarn`))
binNames.delete(variant);
Expand Down
35 changes: 13 additions & 22 deletions tests/Enable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {runCli} from './
const engine = new Engine();

beforeEach(async () => {
// `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
});
Expand All @@ -21,17 +22,12 @@ describe(`EnableCommand`, () => {
await xfs.mktempPromise(async cwd => {
const corepackBin = await makeBin(cwd, `corepack` as Filename);

const PATH = process.env.PATH;
try {
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${PATH}`;
await expect(runCli(cwd, [`enable`])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
} finally {
process.env.PATH = PATH;
}
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
await expect(runCli(cwd, [`enable`])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});

const sortedEntries = xfs.readdirPromise(cwd).then(entries => {
return entries.sort();
Expand Down Expand Up @@ -73,17 +69,12 @@ describe(`EnableCommand`, () => {
await xfs.mktempPromise(async cwd => {
const corepackBin = await makeBin(cwd, `corepack` as Filename);

const PATH = process.env.PATH;
try {
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${PATH}`;
await expect(runCli(cwd, [`enable`, `yarn`])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
} finally {
process.env.PATH = PATH;
}
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
await expect(runCli(cwd, [`enable`, `yarn`])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});

const sortedEntries = xfs.readdirPromise(cwd).then(entries => {
return entries.sort();
Expand Down
1 change: 1 addition & 0 deletions tests/Up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import process from 'node:process';
import {runCli} from './_runCli';

beforeEach(async () => {
// `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
});
Expand Down
1 change: 1 addition & 0 deletions tests/Use.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import process from 'node:process';
import {runCli} from './_runCli';

beforeEach(async () => {
// `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
});
Expand Down

0 comments on commit 12f1c31

Please sign in to comment.