|
4 | 4 | import execa from 'execa'; |
5 | 5 | import makeTemp from './_temp.js'; |
6 | 6 | import * as fs from '../src/util/fs.js'; |
| 7 | +import * as misc from '../src/util/misc.js'; |
| 8 | +import * as constants from '../src/constants.js'; |
7 | 9 |
|
8 | 10 | jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000; |
9 | 11 |
|
@@ -70,3 +72,37 @@ test('--mutex network', async () => { |
70 | 72 | execa(command, ['add', 'foo'].concat(args), options), |
71 | 73 | ]); |
72 | 74 | }); |
| 75 | + |
| 76 | +test('cache folder fallback', async () => { |
| 77 | + const cwd = await makeTemp(); |
| 78 | + const cacheFolder = path.join(cwd, '.cache'); |
| 79 | + |
| 80 | + const command = path.resolve(__dirname, '../bin/yarn'); |
| 81 | + const args = ['--preferred-cache-folder', cacheFolder]; |
| 82 | + |
| 83 | + const options = {cwd}; |
| 84 | + |
| 85 | + function runCacheDir(): Promise<Array<Buffer>> { |
| 86 | + const {stderr, stdout} = execa(command, ['cache', 'dir'].concat(args), options); |
| 87 | + |
| 88 | + const stdoutPromise = misc.consumeStream(stdout); |
| 89 | + const stderrPromise = misc.consumeStream(stderr); |
| 90 | + |
| 91 | + return Promise.all([stdoutPromise, stderrPromise]); |
| 92 | + } |
| 93 | + |
| 94 | + const [stdoutOutput, stderrOutput] = await runCacheDir(); |
| 95 | + |
| 96 | + expect(stdoutOutput.toString().trim()).toEqual(path.join(cacheFolder, `v${constants.CACHE_VERSION}`)); |
| 97 | + expect(stderrOutput.toString()).not.toMatch(/Skipping preferred cache folder/); |
| 98 | + |
| 99 | + await fs.unlink(cacheFolder); |
| 100 | + await fs.writeFile(cacheFolder, `not a directory`); |
| 101 | + |
| 102 | + const [stdoutOutput2, stderrOutput2] = await runCacheDir(); |
| 103 | + |
| 104 | + expect(stdoutOutput2.toString().trim()).toEqual( |
| 105 | + path.join(constants.PREFERRED_MODULE_CACHE_DIRECTORIES[0], `v${constants.CACHE_VERSION}`), |
| 106 | + ); |
| 107 | + expect(stderrOutput2.toString()).toMatch(/Skipping preferred cache folder/); |
| 108 | +}); |
0 commit comments