Skip to content

Commit

Permalink
Changes variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
Maël Nison committed Mar 1, 2017
1 parent c2b2653 commit cab0c1d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 32 deletions.
21 changes: 13 additions & 8 deletions __tests__/commands/install/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ const path = require('path');
const stream = require('stream');
const os = require('os');

async function mockConstants(mocks: Object, cb: (config: Config) => Promise<void>): Promise<void> {
async function mockConstants(base: Config, mocks: Object, cb: (config: Config) => Promise<void>): Promise<void> {
// We cannot put this function inside _helpers, because we need to change the "request" variable
// after resetting the modules. Updating this variable is required because some tests check what
// happened during the Yarn execution, and they need to use the same instance of "request" than
// the Yarn environment.

const opts = {};

for (let name of [ 'binLinks', 'cwd', 'globalFolder', 'linkFolder', 'production' ]) opts[name] = base[name];
opts.cacheFolder = base._cacheRootFolder;

const automock = jest.genMockFromModule('../../../src/constants');
jest.setMock('../../../src/constants', Object.assign(automock, mocks));

jest.resetModules();
request = require('request');

jest.mock('../../../src/constants');
await cb(await require('../../../src/config.js').default.create());
await cb(await require('../../../src/config.js').default.create(opts, base.reporter));
jest.unmock('../../../src/constants');
}

Expand All @@ -44,7 +49,7 @@ afterEach(request.__resetAuthedRequests);

test.concurrent('properly find and save build artifacts', async () => {
await runInstall({}, 'artifacts-finds-and-saves', async (config): Promise<void> => {
const cacheFolder = path.join(config.versionedCacheFolder, 'npm-dummy-0.0.0');
const cacheFolder = path.join(config.cacheFolder, 'npm-dummy-0.0.0');
assert.deepEqual(
(await fs.readJson(path.join(cacheFolder, constants.METADATA_FILENAME))).artifacts,
['dummy', path.join('dummy', 'dummy.txt'), 'dummy.txt'],
Expand All @@ -65,7 +70,7 @@ test('changes the cache path when bumping the cache version', async () => {
await cache(config, reporter, {}, ['dir']);
assert.ok(!!(JSON.parse(String(inOut.read())) : any).data.match(/[\\\/]v1[\\\/]?$/));

await mockConstants({CACHE_VERSION: 42}, async (config): Promise<void> => {
await mockConstants(config, {CACHE_VERSION: 42}, async (config): Promise<void> => {
await cache(config, reporter, {}, ['dir']);
assert.ok(!!(JSON.parse(String(inOut.read())) : any).data.match(/[\\\/]v42[\\\/]?$/));
});
Expand All @@ -85,14 +90,14 @@ test('changes the cache directory when bumping the cache version', async () => {
await fs.writeFile(path.join(cachePath, 'yarn.test'), 'YARN TEST');
await fs.unlink(path.join(config.cwd, 'node_modules'));

const firstReinstall = new Install({}, config, reporter, lockfile);
const firstReinstall = new Install({skipIntegrityCheck: true}, config, reporter, lockfile);
await firstReinstall.init();

assert.ok(await fs.exists(path.join(config.cwd, 'node_modules', 'is-array', 'yarn.test')));

await mockConstants({CACHE_VERSION: 42}, async (config): Promise<void> => {
const firstReinstall = new Install({}, config, reporter, lockfile);
await firstReinstall.init();
await mockConstants(config, {CACHE_VERSION: 42}, async (config): Promise<void> => {
const secondReinstall = new Install({skipIntegrityCheck: true}, config, reporter, lockfile);
await secondReinstall.init();

assert.ok(!await fs.exists(path.join(config.cwd, 'node_modules', 'is-array', 'yarn.test')));
});
Expand Down
4 changes: 2 additions & 2 deletions __tests__/package-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ function addTest(pattern, registry = 'npm', init: ?(cacheFolder: string) => Prom
}, reporter);

await fs.mkdirp(path.join(loc, 'node_modules'));
await fs.mkdirp(config.versionedCacheFolder);
await fs.mkdirp(config.cacheFolder);

if (init) {
await init(config.versionedCacheFolder);
await init(config.cacheFolder);
}

const resolver = new PackageResolver(config, lockfile);
Expand Down
12 changes: 6 additions & 6 deletions src/cli/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const {run, setFlags} = buildSubCommands('cache', {
args: Array<string>,
): Promise<void> {
async function readCacheMetadata(
parentDir = config.versionedCacheFolder,
parentDir = config.cacheFolder,
metadataFile = METADATA_FILENAME,
): Promise<[]> {
const folders = await fs.readdir(parentDir);
Expand All @@ -31,7 +31,7 @@ export const {run, setFlags} = buildSubCommands('cache', {
continue;
}

const loc = path.join(config.versionedCacheFolder, parentDir.replace(config.versionedCacheFolder, ''), folder);
const loc = path.join(config.cacheFolder, parentDir.replace(config.cacheFolder, ''), folder);
// Check if this is a scoped package
if (!(await fs.exists(path.join(loc, metadataFile)))) {
// If so, recurrently read scoped packages metadata
Expand All @@ -54,7 +54,7 @@ export const {run, setFlags} = buildSubCommands('cache', {
config: Config,
reporter: Reporter,
) {
reporter.log(config.versionedCacheFolder);
reporter.log(config.cacheFolder);
},

async clean(
Expand All @@ -63,9 +63,9 @@ export const {run, setFlags} = buildSubCommands('cache', {
flags: Object,
args: Array<string>,
): Promise<void> {
if (config.versionedCacheFolder) {
await fs.unlink(config.cacheFolder);
await fs.mkdirp(config.versionedCacheFolder);
if (config.cacheFolder) {
await fs.unlink(config._cacheRootFolder);
await fs.mkdirp(config.cacheFolder);
reporter.success(reporter.lang('clearedCache'));
}
},
Expand Down
3 changes: 1 addition & 2 deletions src/cli/commands/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ async function updateCwd(config: Config): Promise<void> {
cwd: config.globalFolder,
binLinks: true,
globalFolder: config.globalFolder,
cacheFolder: config.cacheFolder,
versionedCacheFolder: config.versionedCacheFolder,
cacheFolder: config._cacheRootFolder,
linkFolder: config.linkFolder,
});
}
Expand Down
3 changes: 1 addition & 2 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,7 @@ config.init({
binLinks: commander.binLinks,
modulesFolder: commander.modulesFolder,
globalFolder: commander.globalFolder,
cacheFolder: commander.cacheFolder,
versionedCacheFolder: commander.versionedCacheFolder,
cacheRootFolder: commander.cacheFolder,
preferOffline: commander.preferOffline,
captureHar: commander.har,
ignorePlatform: commander.ignorePlatform,
Expand Down
22 changes: 12 additions & 10 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const path = require('path');

export type ConfigOptions = {
cwd?: ?string,
_cacheRootFolder?: ?string,
cacheFolder?: ?string,
versionedCacheFolder?: ?string,
tempFolder?: ?string,
modulesFolder?: ?string,
globalFolder?: ?string,
Expand Down Expand Up @@ -112,10 +112,10 @@ export default class Config {
modulesFolder: ?string;

//
cacheFolder: string;
_cacheRootFolder: string;

//
versionedCacheFolder: string;
cacheFolder: string;

//
tempFolder: string;
Expand Down Expand Up @@ -235,17 +235,19 @@ export default class Config {
key: String(opts.key || this.getOption('key') || ''),
networkConcurrency: this.networkConcurrency,
});

this.cacheFolder = String(
this._cacheRootFolder = String(
opts.cacheFolder ||
this.getOption('cache-folder') ||
constants.MODULE_CACHE_DIRECTORY,
);

if (!opts.cacheFolder)
throw new Error([opts.cacheFolder, this._cacheRootFolder]);

//init & create cacheFolder, tempFolder
this.versionedCacheFolder = path.join(this.cacheFolder, 'v' + String(constants.CACHE_VERSION));
this.tempFolder = opts.tempFolder || path.join(this.versionedCacheFolder, '.tmp');
await fs.mkdirp(this.versionedCacheFolder);
this.cacheFolder = path.join(this._cacheRootFolder, 'v' + String(constants.CACHE_VERSION));
this.tempFolder = opts.tempFolder || path.join(this.cacheFolder, '.tmp');
await fs.mkdirp(this.cacheFolder);
await fs.mkdirp(this.tempFolder);

if (opts.production === 'false') {
Expand Down Expand Up @@ -300,7 +302,7 @@ export default class Config {
*/

generateHardModulePath(pkg: ?PackageReference, ignoreLocation?: ?boolean): string {
invariant(this.versionedCacheFolder, 'No package root');
invariant(this.cacheFolder, 'No package root');
invariant(pkg, 'Undefined package');

if (pkg.location && !ignoreLocation) {
Expand All @@ -322,7 +324,7 @@ export default class Config {
uid += `-${hash}`;
}

return path.join(this.versionedCacheFolder, `${name}-${uid}`);
return path.join(this.cacheFolder, `${name}-${uid}`);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/registries/npm-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export default class NpmResolver extends RegistryResolver {
// find modules of this name
const prefix = scope ? this.name.split(/\/|%2f/)[1] : `npm-${this.name}-`;

invariant(this.config.versionedCacheFolder, 'expected packages root');
const cacheFolder = path.join(this.config.versionedCacheFolder, scope ? 'npm-' + scope : '');
invariant(this.config.cacheFolder, 'expected packages root');
const cacheFolder = path.join(this.config.cacheFolder, scope ? 'npm-' + scope : '');

const files = await this.config.getCache('cachedPackages', async (): Promise<Array<string>> => {
const files = await fs.readdir(cacheFolder);
Expand Down

0 comments on commit cab0c1d

Please sign in to comment.