|
1 | 1 | import {ppath, xfs, npath} from '@yarnpkg/fslib';
|
2 | 2 | import process from 'node:process';
|
| 3 | +import {parseEnv} from 'node:util'; |
3 | 4 | import {describe, beforeEach, it, expect} from 'vitest';
|
4 | 5 |
|
5 | 6 | import {runCli} from './_runCli';
|
@@ -91,4 +92,42 @@ describe(`UpCommand`, () => {
|
91 | 92 | });
|
92 | 93 | });
|
93 | 94 | });
|
| 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 | + }); |
94 | 133 | });
|
0 commit comments