Skip to content

Commit

Permalink
Use Jest mock to fix user home location
Browse files Browse the repository at this point in the history
  • Loading branch information
TimvdLippe committed Mar 12, 2017
1 parent 814b962 commit 1f52ca8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
6 changes: 1 addition & 5 deletions __tests__/commands/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ export async function run<T, R>(

// remove the lockfile if we create one and it didn't exist before
const lockfile = await createLockfile(cwd);
const homeFolderLocation = process.env.YARN_HOME_FOLDER;
process.env.YARN_HOME_FOLDER = path.join(cwd, '.yarn-home');

// create directories
await fs.mkdirp(path.join(cwd, '.yarn-global'));
Expand All @@ -126,14 +124,12 @@ export async function run<T, R>(
production: flags.production,
}, reporter);

const install = await factory(args, flags, config, reporter, lockfile, () => out);
const install = await factory(args, flags, config, reporter, lockfile, () => out, path.join(cwd, '.yarn-home'));

if (checkInstalled) {
await checkInstalled(config, reporter, install);
}
} catch (err) {
throw new Error(`${err && err.stack} \nConsole output:\n ${out}`);
} finally {
process.env.YARN_HOME_FOLDER = homeFolderLocation;
}
}
11 changes: 9 additions & 2 deletions __tests__/commands/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import type {CLIFunctionReturn} from '../../src/types.js';
import {ConsoleReporter} from '../../src/reporters/index.js';
import {run as buildRun} from './_helpers.js';
import {run as global} from '../../src/cli/commands/global.js';
import * as fs from '../../src/util/fs.js';
import assert from 'assert';
const isCI = require('is-ci');
Expand All @@ -18,7 +17,15 @@ const runGlobal = buildRun.bind(
null,
ConsoleReporter,
fixturesLoc,
(args, flags, config, reporter): CLIFunctionReturn => {
(args, flags, config, reporter, _lockfile, _out, homeFolderLocation): CLIFunctionReturn => {
const automock = jest.genMockFromModule('../../src/util/user-home-dir');
jest.setMock('../../src/util/user-home-dir', Object.assign(automock, homeFolderLocation));

jest.resetModules();
jest.mock('../../src/util/user-home-dir');

const global = require('../../src/cli/commands/global.js').run;
jest.unmock('../../src/util/user-home-dir');
return global(config, reporter, flags, args);
},
);
Expand Down
2 changes: 1 addition & 1 deletion src/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class NpmRegistry extends Registry {
async getPossibleConfigLocations(filename: string): Promise<Array<[boolean, string, string]>> {
const possibles = [
[false, path.join(this.cwd, filename)],
[true, this.config.userconfig || path.join(process.env.YARN_HOME_FOLDER || userHome, filename)],
[true, this.config.userconfig || path.join(userHome, filename)],
[false, path.join(getGlobalPrefix(), filename)],
];

Expand Down

0 comments on commit 1f52ca8

Please sign in to comment.