|
| 1 | +import {Filename, ppath, xfs} from '@yarnpkg/fslib'; |
| 2 | + |
| 3 | +describe(`DotEnv files`, () => { |
| 4 | + it(`should automatically inject a .env file in the environment`, makeTemporaryEnv({}, async ({path, run, source}) => { |
| 5 | + await run(`install`); |
| 6 | + |
| 7 | + await xfs.writeFilePromise(ppath.join(path, `.env`), [ |
| 8 | + `INJECTED_FROM_ENV_FILE=hello\n`, |
| 9 | + ].join(``)); |
| 10 | + |
| 11 | + await expect(run(`exec`, `env`)).resolves.toMatchObject({ |
| 12 | + stdout: expect.stringMatching(/^INJECTED_FROM_ENV_FILE=hello$/m), |
| 13 | + }); |
| 14 | + })); |
| 15 | + |
| 16 | + it(`should allow .env variables to be interpolated`, makeTemporaryEnv({}, async ({path, run, source}) => { |
| 17 | + await run(`install`); |
| 18 | + |
| 19 | + await xfs.writeFilePromise(ppath.join(path, `.env`), [ |
| 20 | + `INJECTED_FROM_ENV_FILE=\${FOO}\n`, |
| 21 | + ].join(``)); |
| 22 | + |
| 23 | + await expect(run(`exec`, `env`, {env: {FOO: `foo`}})).resolves.toMatchObject({ |
| 24 | + stdout: expect.stringMatching(/^INJECTED_FROM_ENV_FILE=foo$/m), |
| 25 | + }); |
| 26 | + })); |
| 27 | + |
| 28 | + it(`should allow .env variables to be used in the next ones`, makeTemporaryEnv({}, async ({path, run, source}) => { |
| 29 | + await run(`install`); |
| 30 | + |
| 31 | + await xfs.writeFilePromise(ppath.join(path, `.env`), [ |
| 32 | + `INJECTED_FROM_ENV_FILE_1=hello\n`, |
| 33 | + `INJECTED_FROM_ENV_FILE_2=\${INJECTED_FROM_ENV_FILE_1} world\n`, |
| 34 | + ].join(``)); |
| 35 | + |
| 36 | + await expect(run(`exec`, `env`, {env: {FOO: `foo`}})).resolves.toMatchObject({ |
| 37 | + stdout: expect.stringMatching(/^INJECTED_FROM_ENV_FILE_2=hello world$/m), |
| 38 | + }); |
| 39 | + })); |
| 40 | + |
| 41 | + it(`shouldn't read the .env if the injectEnvironmentFiles setting is defined`, makeTemporaryEnv({}, async ({path, run, source}) => { |
| 42 | + await xfs.writeJsonPromise(ppath.join(path, Filename.rc), { |
| 43 | + injectEnvironmentFiles: [], |
| 44 | + }); |
| 45 | + |
| 46 | + await xfs.writeFilePromise(ppath.join(path, `.my-env`), [ |
| 47 | + `INJECTED_FROM_ENV_FILE=hello\n`, |
| 48 | + ].join(``)); |
| 49 | + |
| 50 | + await run(`install`); |
| 51 | + |
| 52 | + await expect(run(`exec`, `env`)).resolves.toMatchObject({ |
| 53 | + stdout: expect.not.stringMatching(/^INJECTED_FROM_ENV_FILE=/m), |
| 54 | + }); |
| 55 | + })); |
| 56 | + |
| 57 | + it(`should allow multiple environment files to be defined`, makeTemporaryEnv({}, async ({path, run, source}) => { |
| 58 | + await xfs.writeJsonPromise(ppath.join(path, Filename.rc), { |
| 59 | + injectEnvironmentFiles: [`.my-env`, `.my-other-env`], |
| 60 | + }); |
| 61 | + |
| 62 | + await xfs.writeFilePromise(ppath.join(path, `.my-env`), [ |
| 63 | + `INJECTED_FROM_ENV_FILE_1=hello\n`, |
| 64 | + ].join(``)); |
| 65 | + |
| 66 | + await xfs.writeFilePromise(ppath.join(path, `.my-other-env`), [ |
| 67 | + `INJECTED_FROM_ENV_FILE_2=world\n`, |
| 68 | + ].join(``)); |
| 69 | + |
| 70 | + await run(`install`); |
| 71 | + |
| 72 | + const {stdout} = await run(`exec`, `env`); |
| 73 | + |
| 74 | + expect(stdout).toMatch(/^INJECTED_FROM_ENV_FILE_1=hello$/m); |
| 75 | + expect(stdout).toMatch(/^INJECTED_FROM_ENV_FILE_2=world$/m); |
| 76 | + })); |
| 77 | + |
| 78 | + it(`should let the last environment file override the first`, makeTemporaryEnv({}, async ({path, run, source}) => { |
| 79 | + await xfs.writeJsonPromise(ppath.join(path, Filename.rc), { |
| 80 | + injectEnvironmentFiles: [`.my-env`, `.my-other-env`], |
| 81 | + }); |
| 82 | + |
| 83 | + await xfs.writeFilePromise(ppath.join(path, `.my-env`), [ |
| 84 | + `INJECTED_FROM_ENV_FILE=hello\n`, |
| 85 | + ].join(``)); |
| 86 | + |
| 87 | + await xfs.writeFilePromise(ppath.join(path, `.my-other-env`), [ |
| 88 | + `INJECTED_FROM_ENV_FILE=world\n`, |
| 89 | + ].join(``)); |
| 90 | + |
| 91 | + await run(`install`); |
| 92 | + |
| 93 | + await expect(run(`exec`, `env`)).resolves.toMatchObject({ |
| 94 | + stdout: expect.stringMatching(/^INJECTED_FROM_ENV_FILE=world$/m), |
| 95 | + }); |
| 96 | + })); |
| 97 | + |
| 98 | + it(`should throw an error if the settings reference a non-existing file`, makeTemporaryEnv({}, async ({path, run, source}) => { |
| 99 | + await xfs.writeJsonPromise(ppath.join(path, Filename.rc), { |
| 100 | + injectEnvironmentFiles: [`.my-env`], |
| 101 | + }); |
| 102 | + |
| 103 | + await expect(run(`install`)).rejects.toThrow(); |
| 104 | + })); |
| 105 | + |
| 106 | + it(`shouldn't throw an error if the settings reference a non-existing file with a ?-suffixed path`, makeTemporaryEnv({}, async ({path, run, source}) => { |
| 107 | + await xfs.writeJsonPromise(ppath.join(path, Filename.rc), { |
| 108 | + injectEnvironmentFiles: [`.my-env?`], |
| 109 | + }); |
| 110 | + |
| 111 | + await run(`install`); |
| 112 | + })); |
| 113 | +}); |
0 commit comments