Skip to content

Commit

Permalink
Adds versioning to the cache path (#2768)
Browse files Browse the repository at this point in the history
* Adds versioning to the cache path

* Adds a test to check that the cache path is correctly updated

* Renames "cacheFolder" into "versionedCacheFolder"

* Adds a test to check that the cache is correctly busted when bumping the cache version

* Fixes tests on Windows

* Changes variable names
  • Loading branch information
arcanis authored and bestander committed Mar 2, 2017
1 parent 0ad2bf9 commit ed6b9b9
Show file tree
Hide file tree
Showing 9 changed files with 545 additions and 89 deletions.
75 changes: 73 additions & 2 deletions __tests__/commands/install/integration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* @flow */

import type Config from '../../../src/config';
import PackageResolver from '../../../src/package-resolver.js';
import {run as cache} from '../../../src/cli/commands/cache.js';
import {run as check} from '../../../src/cli/commands/check.js';
import * as constants from '../../../src/constants.js';
import * as reporters from '../../../src/reporters/index.js';
Expand All @@ -11,15 +14,41 @@ import {promisify} from '../../../src/util/promise';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000;

const request = require('request');
let request = require('request');
const assert = require('assert');
const semver = require('semver');
const fsNode = require('fs');
const path = require('path');
const stream = require('stream');
const os = require('os');

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 = {};

opts.binLinks = base.binLinks;
opts.cwd = base.cwd;
opts.globalFolder = base.globalFolder;
opts.linkFolder = base.linkFolder;
opts.production = base.production;
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(opts, base.reporter));
jest.unmock('../../../src/constants');
}

beforeEach(request.__resetAuthedRequests);
// $FlowFixMe
afterEach(request.__resetAuthedRequests);

test.concurrent('properly find and save build artifacts', async () => {
Expand All @@ -37,6 +66,48 @@ test.concurrent('properly find and save build artifacts', async () => {
});
});

test('changes the cache path when bumping the cache version', async () => {
await runInstall({}, 'install-github', async (config): Promise<void> => {
const inOut = new stream.PassThrough();
const reporter = new reporters.JSONReporter({stdout: inOut});

await cache(config, reporter, {}, ['dir']);
assert.ok(!!(JSON.parse(String(inOut.read())) : any).data.match(/[\\\/]v1[\\\/]?$/));

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[\\\/]?$/));
});
});
});

test('changes the cache directory when bumping the cache version', async () => {
await runInstall({}, 'install-production', async (config, reporter): Promise<void> => {
const lockfile = await Lockfile.fromDirectory(config.cwd);

const resolver = new PackageResolver(config, lockfile);
await resolver.init([{pattern: 'is-array', registry: 'npm'}]);

const ref = resolver.getPackageReferences()[0];
const cachePath = config.generateHardModulePath(ref, true);

await fs.writeFile(path.join(cachePath, 'yarn.test'), 'YARN TEST');
await fs.unlink(path.join(config.cwd, 'node_modules'));

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(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')));
});
});
});

test.concurrent("removes extraneous files that aren't in module or artifacts", async () => {
async function check(cwd: string): Promise<void> {
// retains artifact
Expand Down
13 changes: 8 additions & 5 deletions __tests__/package-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@ function addTest(pattern, registry = 'npm', init: ?(cacheFolder: string) => Prom
const reporter = new reporters.NoopReporter({});

const loc = await makeTemp();
await fs.mkdirp(path.join(loc, 'node_modules'));
const cacheFolder = path.join(loc, 'cache');
await fs.mkdirp(cacheFolder);
if (init) {
await init(cacheFolder);
}

const config = await Config.create({
cwd: loc,
offline,
cacheFolder,
}, reporter);

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

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

const resolver = new PackageResolver(config, lockfile);
await resolver.init([{pattern, registry}]);

Expand Down
73 changes: 0 additions & 73 deletions flow-typed/npm/jest_v14.0.x.js

This file was deleted.

Loading

0 comments on commit ed6b9b9

Please sign in to comment.