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