Skip to content

Commit eec7201

Browse files
author
Maël Nison
committed
Adds a test
1 parent 3324612 commit eec7201

File tree

8 files changed

+56
-11
lines changed

8 files changed

+56
-11
lines changed

__tests__/integration.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import execa from 'execa';
55
import makeTemp from './_temp.js';
66
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';
79

810
jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000;
911

@@ -76,12 +78,37 @@ test('cache folder fallback', async () => {
7678
const cacheFolder = path.join(cwd, '.cache');
7779

7880
await fs.mkdirp(cacheFolder);
79-
await fs.chmod(cacheFolder, 0o000);
8081

8182
const command = path.resolve(__dirname, '../bin/yarn');
8283
const args = ['--preferred-cache-folder', cacheFolder];
8384

8485
const options = {cwd};
8586

86-
await Promise.all([execa(command, ['cache', 'dir'].concat(args), options)]);
87+
{
88+
const {stderr, stdout} = execa(command, ['cache', 'dir'].concat(args), options);
89+
90+
const stdoutPromise = misc.consumeStream(stdout);
91+
const stderrPromise = misc.consumeStream(stderr);
92+
93+
const [stdoutOutput, stderrOutput] = await Promise.all([stdoutPromise, stderrPromise]);
94+
95+
expect(stdoutOutput.toString().trim()).toEqual(path.join(cacheFolder, `v${constants.CACHE_VERSION}`));
96+
expect(stderrOutput.toString()).not.toMatch(/Skipping preferred cache folder/);
97+
}
98+
99+
await fs.chmod(cacheFolder, 0o000);
100+
101+
{
102+
const {stderr, stdout} = execa(command, ['cache', 'dir'].concat(args), options);
103+
104+
const stdoutPromise = misc.consumeStream(stdout);
105+
const stderrPromise = misc.consumeStream(stderr);
106+
107+
const [stdoutOutput, stderrOutput] = await Promise.all([stdoutPromise, stderrPromise]);
108+
109+
expect(stdoutOutput.toString().trim()).toEqual(
110+
path.join(constants.PREFERRED_MODULE_CACHE_DIRECTORIES[0], `v${constants.CACHE_VERSION}`),
111+
);
112+
expect(stderrOutput.toString()).toMatch(/Skipping preferred cache folder/);
113+
}
87114
});

src/cli/commands/cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const {run, setFlags, examples} = buildSubCommands('cache', {
5555
},
5656

5757
dir(config: Config, reporter: Reporter) {
58-
reporter.log(config.cacheFolder);
58+
reporter.log(config.cacheFolder, {force: true});
5959
},
6060

6161
async clean(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {

src/cli/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function main({
6161
'rather than installing modules into the node_modules folder relative to the cwd, output them here',
6262
);
6363
commander.option('--preferred-cache-folder <path>', 'specify a custom folder to store the yarn cache if possible');
64-
commander.option('--cache-folder <path>', 'specify a custom folder to store the yarn cache');
64+
commander.option('--cache-folder <path>', 'specify a custom folder that must be used to store the yarn cache');
6565
commander.option('--mutex <type>[:specifier]', 'use a mutex to ensure only one yarn instance is executing');
6666
commander.option('--emoji [bool]', 'enable emoji in output', process.platform === 'darwin');
6767
commander.option('-s, --silent', 'skip Yarn console logs, other types of logs (script output) will be printed');

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ export default class Config {
288288
}
289289

290290
for (let t = 0; t < preferredCacheFolders.length && !cacheRootFolder; ++t) {
291-
const tentativeCacheFolder = preferredCacheFolders[t];
291+
const tentativeCacheFolder = String(preferredCacheFolders[t]);
292292

293293
try {
294294
await fs.mkdirp(tentativeCacheFolder);

src/reporters/base-reporter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export default class BaseReporter {
195195
success(message: string) {}
196196

197197
// a simple log message
198-
log(message: string) {}
198+
log(message: string, {force = false}: {force?: boolean} = {}) {}
199199

200200
// a shell command has been executed
201201
command(command: string) {}

src/reporters/console/console-reporter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ export default class ConsoleReporter extends BaseReporter {
160160
this.log(this._prependEmoji(msg, '✨'));
161161
}
162162
163-
log(msg: string) {
163+
log(msg: string, {force = false}: {force?: boolean} = {}) {
164164
this._lastCategorySize = 0;
165-
this._log(msg);
165+
this._log(msg, {force});
166166
}
167167
168-
_log(msg: string) {
169-
if (this.isSilent) {
168+
_log(msg: string, {force = false}: {force?: boolean} = {}) {
169+
if (this.isSilent && !force) {
170170
return;
171171
}
172172
clearLine(this.stdout);

src/reporters/lang/en.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ const messages = {
183183

184184
cacheFolderSkipped: 'Skipping preferred cache folder $0 because it is not writable.',
185185
cacheFolderMissing:
186-
"Yarn hasn't been able to find a cache folder. Please use an explicit --cache-folder option to tell it what location to use, or make one of the preferred locations writable.",
186+
"Yarn hasn't been able to find a cache folder it can use. Please use the explicit --cache-folder option to tell it what location to use, or make one of the preferred locations writable.",
187187
cacheFolderSelected: 'Selected the next writable cache folder in the list, will be $0.',
188188

189189
execMissingCommand: 'Missing command name.',

src/util/misc.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
const _camelCase = require('camelcase');
44

5+
export function consumeStream(stream: Object): Promise<Buffer> {
6+
return new Promise((resolve, reject) => {
7+
const buffers = [];
8+
9+
stream.on(`data`, buffer => {
10+
buffers.push(buffer);
11+
});
12+
13+
stream.on(`end`, () => {
14+
resolve(Buffer.concat(buffers));
15+
});
16+
17+
stream.on(`error`, error => {
18+
reject(error);
19+
});
20+
});
21+
}
22+
523
export function sortAlpha(a: string, b: string): number {
624
// sort alphabetically in a deterministic way
725
const shortLen = Math.min(a.length, b.length);

0 commit comments

Comments
 (0)