Skip to content

Commit 575ba0e

Browse files
committed
fixup! .corepack.env as a lock file
1 parent d27f603 commit 575ba0e

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

tests/Up.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {ppath, xfs, npath} from '@yarnpkg/fslib';
22
import process from 'node:process';
3+
import {parseEnv} from 'node:util';
34
import {describe, beforeEach, it, expect} from 'vitest';
45

56
import {runCli} from './_runCli';
@@ -91,4 +92,42 @@ describe(`UpCommand`, () => {
9192
});
9293
});
9394
});
95+
96+
it(`should update the ".corepack.env" file from the current project`, async t => {
97+
// Skip that test on Node.js 18.x as it lacks support for .env files.
98+
if (process.version.startsWith(`v18.`)) t.skip();
99+
await Promise.all([
100+
`COREPACK_DEV_ENGINES_YARN=1.1.0\n`,
101+
`\nCOREPACK_DEV_ENGINES_YARN=1.1.0\n`,
102+
`COREPACK_DEV_ENGINES_YARN=1.1.0`,
103+
`\nCOREPACK_DEV_ENGINES_YARN=1.1.0`,
104+
`FOO=bar\nCOREPACK_DEV_ENGINES_YARN=1.1.0\n`,
105+
`FOO=bar\nCOREPACK_DEV_ENGINES_YARN=1.1.0`,
106+
].map(originalEnv => xfs.mktempPromise(async cwd => {
107+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json`), {
108+
devEngines: {packageManager: {name: `yarn`, version: `1.x || 2.x`}},
109+
});
110+
await xfs.writeFilePromise(ppath.join(cwd, `.corepack.env`), originalEnv);
111+
112+
await expect(runCli(cwd, [`up`])).resolves.toMatchObject({
113+
exitCode: 0,
114+
stderr: ``,
115+
stdout: expect.stringMatching(/^Installing yarn@2\.4\.3 in the project\.\.\.\n\n YN0000: (.*\n)+ YN0000: Done in \d+s \d+ms\n$/),
116+
});
117+
118+
try {
119+
await expect(xfs.readFilePromise(ppath.join(cwd, `.corepack.env`), `utf-8`).then(parseEnv)).resolves.toMatchObject({
120+
COREPACK_DEV_ENGINES_YARN: `2.4.3+sha512.8dd9fedc5451829619e526c56f42609ad88ae4776d9d3f9456d578ac085115c0c2f0fb02bb7d57fd2e1b6e1ac96efba35e80a20a056668f61c96934f67694fd0`,
121+
});
122+
} catch (cause) {
123+
throw new Error(JSON.stringify(originalEnv), {cause});
124+
}
125+
126+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
127+
exitCode: 0,
128+
stdout: `2.4.3\n`,
129+
stderr: ``,
130+
});
131+
})));
132+
});
94133
});

tests/Use.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,43 @@ describe(`UseCommand`, () => {
190190
});
191191
}
192192
});
193+
194+
it(`should update the ".corepack.env" file from the current project`, async t => {
195+
// Skip that test on Node.js 18.x as it lacks support for .env files.
196+
if (process.version.startsWith(`v18.`)) t.skip();
197+
await Promise.all([
198+
`COREPACK_DEV_ENGINES_YARN=2.1.0\n`,
199+
`\nCOREPACK_DEV_ENGINES_YARN=2.1.0\n`,
200+
`COREPACK_DEV_ENGINES_YARN=2.1.0`,
201+
`\nCOREPACK_DEV_ENGINES_YARN=2.1.0`,
202+
`FOO=bar\nCOREPACK_DEV_ENGINES_YARN=2.1.0\n`,
203+
`FOO=bar\nCOREPACK_DEV_ENGINES_YARN=2.1.0`,
204+
].map(originalEnv => xfs.mktempPromise(async cwd => {
205+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json`), {
206+
devEngines: {packageManager: {name: `yarn`, version: `1.x || 2.x`}},
207+
license: `MIT`, // To avoid Yarn warning.
208+
});
209+
await xfs.writeFilePromise(ppath.join(cwd, `.corepack.env`), originalEnv);
210+
211+
await expect(runCli(cwd, [`use`, `[email protected]`])).resolves.toMatchObject({
212+
exitCode: 0,
213+
stderr: ``,
214+
stdout: expect.stringMatching(/^Installing yarn@1\.22\.4 in the project\.\.\.\n\nyarn install v1\.22\.4\ninfo No lockfile found\.\n(.*\n)+Done in \d+\.\d+s\.\n$/),
215+
});
216+
217+
try {
218+
await expect(xfs.readFilePromise(ppath.join(cwd, `.corepack.env`), `utf-8`).then(parseEnv)).resolves.toMatchObject({
219+
COREPACK_DEV_ENGINES_YARN: `1.22.4+sha512.a1833b862fe52169bd6c2a033045a07df5bc6a23595c259e675fed1b2d035ab37abe6ce309720abb6636d68f03615054b6292dc0a70da31c8697fda228b50d18`,
220+
});
221+
} catch (cause) {
222+
throw new Error(JSON.stringify(originalEnv), {cause});
223+
}
224+
225+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
226+
exitCode: 0,
227+
stdout: `1.22.4\n`,
228+
stderr: ``,
229+
});
230+
})));
231+
});
193232
});

0 commit comments

Comments
 (0)