|
| 1 | +import {xfs, ppath, PortablePath, Filename} from '@yarnpkg/fslib'; |
| 2 | + |
| 3 | +const yarnrcRegexp = /^yarnPath:/; |
| 4 | + |
| 5 | +describe(`Commands`, () => { |
| 6 | + describe(`set version`, () => { |
| 7 | + test( |
| 8 | + `it shouldn't set yarnPath if corepack is enabled and the version is semver`, |
| 9 | + makeTemporaryEnv({}, { |
| 10 | + env: {COREPACK_ROOT: `/path/to/corepack`}, |
| 11 | + }, async ({path, run, source}) => { |
| 12 | + await run(`set`, `version`, `3.0.0`); |
| 13 | + await check(path, {corepackVersion: `3.0.0`, usePath: false}); |
| 14 | + }), |
| 15 | + ); |
| 16 | + |
| 17 | + test( |
| 18 | + `it should set yarnPath if corepack is disabled, even when the version is semver`, |
| 19 | + makeTemporaryEnv({}, { |
| 20 | + env: {COREPACK_ROOT: undefined}, |
| 21 | + }, async ({path, run, source}) => { |
| 22 | + await run(`set`, `version`, `3.0.0`); |
| 23 | + await check(path, {corepackVersion: `3.0.0`, usePath: true}); |
| 24 | + }), |
| 25 | + ); |
| 26 | + |
| 27 | + test( |
| 28 | + `it should always set yarnPath if one already exists`, |
| 29 | + makeTemporaryEnv({}, { |
| 30 | + env: {COREPACK_ROOT: `/path/to/corepack`}, |
| 31 | + }, async ({path, run, source}) => { |
| 32 | + // To force yarnPath to be set; followed by a sanity check |
| 33 | + await run(`set`, `version`, `3.0.0`, {env: {COREPACK_ROOT: undefined}}); |
| 34 | + await check(path, {corepackVersion: `3.0.0`, usePath: true}); |
| 35 | + |
| 36 | + await run(`set`, `version`, `3.0.0`); |
| 37 | + await check(path, {corepackVersion: `3.0.0`, usePath: true}); |
| 38 | + }), |
| 39 | + ); |
| 40 | + |
| 41 | + test( |
| 42 | + `it should always set yarnPath if --yarn-path is set`, |
| 43 | + makeTemporaryEnv({}, { |
| 44 | + env: {COREPACK_ROOT: `/path/to/corepack`}, |
| 45 | + }, async ({path, run, source}) => { |
| 46 | + await run(`set`, `version`, `3.0.0`, `--yarn-path`); |
| 47 | + await check(path, {corepackVersion: `3.0.0`, usePath: true}); |
| 48 | + }), |
| 49 | + ); |
| 50 | + |
| 51 | + test( |
| 52 | + `it should never set yarnPath if --no-yarn-path is set`, |
| 53 | + makeTemporaryEnv({}, { |
| 54 | + env: {COREPACK_ROOT: undefined}, |
| 55 | + }, async ({path, run, source}) => { |
| 56 | + await run(`set`, `version`, `3.0.0`, `--no-yarn-path`); |
| 57 | + await check(path, {corepackVersion: `3.0.0`, usePath: false}); |
| 58 | + }), |
| 59 | + ); |
| 60 | + |
| 61 | + test( |
| 62 | + `it should prevent using --no-yarn-path with arbitrary files`, |
| 63 | + makeTemporaryEnv({}, { |
| 64 | + env: {COREPACK_ROOT: undefined}, |
| 65 | + }, async ({path, run, source}) => { |
| 66 | + const yarnIndirection = ppath.join(path, `custom-yarn.cjs` as Filename); |
| 67 | + await xfs.writeFilePromise(yarnIndirection, ``); |
| 68 | + |
| 69 | + await expect(run(`set`, `version`, yarnIndirection, `--no-yarn-path`)).rejects.toThrow(); |
| 70 | + }), |
| 71 | + ); |
| 72 | + |
| 73 | + test( |
| 74 | + `it should set yarnPath if the version is an arbitrary file`, |
| 75 | + makeTemporaryEnv({}, { |
| 76 | + env: {COREPACK_ROOT: undefined}, |
| 77 | + }, async ({path, run, source}) => { |
| 78 | + const yarnIndirection = ppath.join(path, `custom-yarn.cjs` as Filename); |
| 79 | + await xfs.writeFilePromise(yarnIndirection, ``); |
| 80 | + |
| 81 | + await run(`set`, `version`, yarnIndirection); |
| 82 | + await check(path, {corepackVersion: /[0-9]+\./, usePath: true}); |
| 83 | + }), |
| 84 | + ); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +async function check(path: PortablePath, checks: {corepackVersion: string | RegExp, usePath: boolean}) { |
| 89 | + const releasesPath = ppath.join(path, `.yarn/releases` as PortablePath); |
| 90 | + const yarnrcPath = ppath.join(path, Filename.rc); |
| 91 | + const manifestPath = ppath.join(path, Filename.manifest); |
| 92 | + |
| 93 | + let releases: Array<string> | null; |
| 94 | + try { |
| 95 | + releases = await xfs.readdirPromise(releasesPath); |
| 96 | + } catch (err) { |
| 97 | + if (err.code === `ENOENT`) { |
| 98 | + releases = null; |
| 99 | + } else { |
| 100 | + throw err; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + let yarnrcFile; |
| 105 | + try { |
| 106 | + yarnrcFile = await xfs.readFilePromise(yarnrcPath, `utf8`); |
| 107 | + } catch (err) { |
| 108 | + if (err.code === `ENOENT`) { |
| 109 | + yarnrcFile = ``; |
| 110 | + } else { |
| 111 | + throw err; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (checks.usePath) |
| 116 | + expect(releases).toHaveLength(1); |
| 117 | + else |
| 118 | + expect(releases).toEqual(null); |
| 119 | + |
| 120 | + if (checks.usePath) |
| 121 | + expect(yarnrcFile).toMatch(yarnrcRegexp); |
| 122 | + else |
| 123 | + expect(yarnrcFile).not.toMatch(yarnrcRegexp); |
| 124 | + |
| 125 | + await expect(xfs.readJsonPromise(manifestPath)).resolves.toMatchObject({ |
| 126 | + packageManager: checks.corepackVersion instanceof RegExp |
| 127 | + ? expect.stringMatching(`yarn@${checks.corepackVersion.source}`) |
| 128 | + : `yarn@${checks.corepackVersion}`, |
| 129 | + }); |
| 130 | +} |
0 commit comments