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

chore(tests): use separate envs for each tests #371

Merged
merged 12 commits into from
Feb 12, 2024
28 changes: 9 additions & 19 deletions tests/Disable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {runCli} from './_runCli';
const engine = new Engine();

beforeEach(async () => {
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.testEnv.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
});

describe(`DisableCommand`, () => {
Expand All @@ -26,15 +26,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.testEnv.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.testEnv.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 +79,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.testEnv.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.testEnv.PATH}`;
await expect(runCli(cwd, [`disable`, `yarn`])).resolves.toMatchObject({
exitCode: 0,
});

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

beforeEach(async () => {
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
process.testEnv.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.testEnv.COREPACK_DEFAULT_TO_LATEST = `0`;
});

describe(`EnableCommand`, () => {
it(`should add the binaries in the folder found in the PATH`, async () => {
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.testEnv.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.testEnv.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 +68,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.testEnv.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.testEnv.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
4 changes: 2 additions & 2 deletions tests/Up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import process from 'node:process';
import {runCli} from './_runCli';

beforeEach(async () => {
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
process.testEnv.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.testEnv.COREPACK_DEFAULT_TO_LATEST = `0`;
});

describe(`UpCommand`, () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/Use.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import process from 'node:process';
import {runCli} from './_runCli';

beforeEach(async () => {
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
process.testEnv.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise()),
process.testEnv.COREPACK_DEFAULT_TO_LATEST = `0`;
});

describe(`UseCommand`, () => {
Expand Down
16 changes: 13 additions & 3 deletions tests/_runCli.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import {PortablePath, npath} from '@yarnpkg/fslib';
import {spawn} from 'child_process';

declare global {
namespace NodeJS {
interface Process {
/** We use `testEnv` to avoid polluting the actual env. Its value is set in `setupTest.js`. */
testEnv: Record<string, string>;
}
}
}

export async function runCli(cwd: PortablePath, argv: Array<string>): Promise<{exitCode: number | null, stdout: string, stderr: string}> {
const out: Array<Buffer> = [];
const err: Array<Buffer> = [];

return new Promise((resolve, reject) => {
if (process.env.RUN_CLI_ID)
(process.env.RUN_CLI_ID as any)++;
if (process.testEnv.RUN_CLI_ID)
(process.testEnv.RUN_CLI_ID as any)++;

const child = spawn(process.execPath, [`--no-warnings`, `-r`, require.resolve(`./recordRequests.js`), require.resolve(`../dist/corepack.js`), ...argv], {
cwd: npath.fromPortablePath(cwd),
env: process.env,
env: process.testEnv,
stdio: `pipe`,
});

Expand Down
Loading