Skip to content

Commit

Permalink
Adds a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Maël Nison committed Aug 17, 2017
1 parent 3324612 commit eec7201
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 11 deletions.
31 changes: 29 additions & 2 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import execa from 'execa';
import makeTemp from './_temp.js';
import * as fs from '../src/util/fs.js';
import * as misc from '../src/util/misc.js';
import * as constants from '../src/constants.js';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000;

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

await fs.mkdirp(cacheFolder);
await fs.chmod(cacheFolder, 0o000);

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

const options = {cwd};

await Promise.all([execa(command, ['cache', 'dir'].concat(args), options)]);
{
const {stderr, stdout} = execa(command, ['cache', 'dir'].concat(args), options);

const stdoutPromise = misc.consumeStream(stdout);
const stderrPromise = misc.consumeStream(stderr);

const [stdoutOutput, stderrOutput] = await Promise.all([stdoutPromise, stderrPromise]);

expect(stdoutOutput.toString().trim()).toEqual(path.join(cacheFolder, `v${constants.CACHE_VERSION}`));
expect(stderrOutput.toString()).not.toMatch(/Skipping preferred cache folder/);
}

await fs.chmod(cacheFolder, 0o000);

{
const {stderr, stdout} = execa(command, ['cache', 'dir'].concat(args), options);

const stdoutPromise = misc.consumeStream(stdout);
const stderrPromise = misc.consumeStream(stderr);

const [stdoutOutput, stderrOutput] = await Promise.all([stdoutPromise, stderrPromise]);

expect(stdoutOutput.toString().trim()).toEqual(
path.join(constants.PREFERRED_MODULE_CACHE_DIRECTORIES[0], `v${constants.CACHE_VERSION}`),
);
expect(stderrOutput.toString()).toMatch(/Skipping preferred cache folder/);
}
});
2 changes: 1 addition & 1 deletion src/cli/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const {run, setFlags, examples} = buildSubCommands('cache', {
},

dir(config: Config, reporter: Reporter) {
reporter.log(config.cacheFolder);
reporter.log(config.cacheFolder, {force: true});
},

async clean(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function main({
'rather than installing modules into the node_modules folder relative to the cwd, output them here',
);
commander.option('--preferred-cache-folder <path>', 'specify a custom folder to store the yarn cache if possible');
commander.option('--cache-folder <path>', 'specify a custom folder to store the yarn cache');
commander.option('--cache-folder <path>', 'specify a custom folder that must be used to store the yarn cache');
commander.option('--mutex <type>[:specifier]', 'use a mutex to ensure only one yarn instance is executing');
commander.option('--emoji [bool]', 'enable emoji in output', process.platform === 'darwin');
commander.option('-s, --silent', 'skip Yarn console logs, other types of logs (script output) will be printed');
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export default class Config {
}

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

try {
await fs.mkdirp(tentativeCacheFolder);
Expand Down
2 changes: 1 addition & 1 deletion src/reporters/base-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export default class BaseReporter {
success(message: string) {}

// a simple log message
log(message: string) {}
log(message: string, {force = false}: {force?: boolean} = {}) {}

// a shell command has been executed
command(command: string) {}
Expand Down
8 changes: 4 additions & 4 deletions src/reporters/console/console-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ export default class ConsoleReporter extends BaseReporter {
this.log(this._prependEmoji(msg, '✨'));
}
log(msg: string) {
log(msg: string, {force = false}: {force?: boolean} = {}) {
this._lastCategorySize = 0;
this._log(msg);
this._log(msg, {force});
}
_log(msg: string) {
if (this.isSilent) {
_log(msg: string, {force = false}: {force?: boolean} = {}) {
if (this.isSilent && !force) {
return;
}
clearLine(this.stdout);
Expand Down
2 changes: 1 addition & 1 deletion src/reporters/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ const messages = {

cacheFolderSkipped: 'Skipping preferred cache folder $0 because it is not writable.',
cacheFolderMissing:
"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.",
"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.",
cacheFolderSelected: 'Selected the next writable cache folder in the list, will be $0.',

execMissingCommand: 'Missing command name.',
Expand Down
18 changes: 18 additions & 0 deletions src/util/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

const _camelCase = require('camelcase');

export function consumeStream(stream: Object): Promise<Buffer> {
return new Promise((resolve, reject) => {
const buffers = [];

stream.on(`data`, buffer => {
buffers.push(buffer);
});

stream.on(`end`, () => {
resolve(Buffer.concat(buffers));
});

stream.on(`error`, error => {
reject(error);
});
});
}

export function sortAlpha(a: string, b: string): number {
// sort alphabetically in a deterministic way
const shortLen = Math.min(a.length, b.length);
Expand Down

0 comments on commit eec7201

Please sign in to comment.