From 6807c44f0903b1a56372eb7ee0fc4087c324e3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Fri, 26 May 2017 12:38:03 +0200 Subject: [PATCH 1/2] refactor some test helpers --- __tests__/commands/_helpers.js | 20 ++++++++++++++++--- .../commands/install/integration-deduping.js | 10 +++++----- .../commands/install/integration-hoisting.js | 9 +++------ __tests__/commands/install/lockfiles.js | 4 ++-- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/__tests__/commands/_helpers.js b/__tests__/commands/_helpers.js index 92ea22e41b..7d496de79f 100644 --- a/__tests__/commands/_helpers.js +++ b/__tests__/commands/_helpers.js @@ -46,9 +46,23 @@ export function explodeLockfile(lockfile: string): Array { } export async function getPackageVersion(config: Config, packagePath: string): Promise { - const loc = path.join(config.cwd, `node_modules/${packagePath.replace(/\//g, '/node_modules/')}/package.json`); - const json = JSON.parse(await fs.readFile(loc)); - return json.version; + return (await getPackageManifest(config, packagePath)).version; +} + +export function getPackageManifest(config: Config, packagePath: string): Promise { + return fs.readJson(getPackageManifestPath(config, packagePath)); +} + +export function getPackageManifestPath(config: Config, packagePath: string): string { + return path.join(getPackagePath(config, packagePath), 'package.json'); +} + +export function isPackagePresent(config: Config, packagePath: string): Promise { + return fs.exists(getPackagePath(config, packagePath)); +} + +export function getPackagePath(config: Config, packagePath: string): string { + return path.join(config.cwd, `node_modules/${packagePath.replace(/\//g, '/node_modules/')}`); } export function makeConfigFromDirectory(cwd: string, reporter: Reporter, flags: Object = {}): Promise { diff --git a/__tests__/commands/install/integration-deduping.js b/__tests__/commands/install/integration-deduping.js index 51055c229d..06a9d2b93b 100644 --- a/__tests__/commands/install/integration-deduping.js +++ b/__tests__/commands/install/integration-deduping.js @@ -1,6 +1,6 @@ /* @flow */ -import {getPackageVersion, runInstall} from '../_helpers.js'; +import {getPackageVersion, getPackageManifestPath, runInstall} from '../_helpers.js'; import * as fs from '../../../src/util/fs.js'; const path = require('path'); @@ -204,8 +204,8 @@ test.concurrent('install should hardlink repeated dependencies', (): Promise A@2 // C@1 -> A@2 (this is hardlink to B@1->A@2) return runInstall({linkDuplicates: true}, 'hardlink-repeated-dependencies', async config => { - const b_a = await fs.stat(path.join(config.cwd, 'node_modules/b/node_modules/a/package.json')); - const c_a = await fs.stat(path.join(config.cwd, 'node_modules/c/node_modules/a/package.json')); + const b_a = await fs.stat(getPackageManifestPath(config, 'b/a')); + const c_a = await fs.stat(getPackageManifestPath(config, 'c/a')); expect(b_a.ino).toEqual(c_a.ino); }); }); @@ -215,8 +215,8 @@ test.concurrent('install should not hardlink repeated dependencies if linkDuplic // B@1 -> A@2 // C@1 -> A@2 return runInstall({linkDuplicates: false}, 'hardlink-repeated-dependencies', async config => { - const b_a = await fs.stat(path.join(config.cwd, 'node_modules/b/node_modules/a/package.json')); - const c_a = await fs.stat(path.join(config.cwd, 'node_modules/c/node_modules/a/package.json')); + const b_a = await fs.stat(getPackageManifestPath(config, 'b/a')); + const c_a = await fs.stat(getPackageManifestPath(config, 'c/a')); expect(b_a.ino).not.toEqual(c_a.ino); }); }); diff --git a/__tests__/commands/install/integration-hoisting.js b/__tests__/commands/install/integration-hoisting.js index de07a0903a..b9623b1959 100644 --- a/__tests__/commands/install/integration-hoisting.js +++ b/__tests__/commands/install/integration-hoisting.js @@ -1,9 +1,6 @@ /* @flow */ -import {getPackageVersion, runInstall} from '../_helpers.js'; -import * as fs from '../../../src/util/fs.js'; - -const path = require('path'); +import {getPackageVersion, isPackagePresent, runInstall} from '../_helpers.js'; jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000; @@ -21,8 +18,8 @@ test.concurrent( 'install hoister should not install prioritised popular transitive devDependencies in --prod mode', (): Promise => { return runInstall({production: true}, 'install-prod-prioritized-popular-transitive-dev-dep', async config => { - expect(await fs.exists(path.join(config.cwd, 'node_modules', 'a'))).toEqual(false); - expect(await fs.exists(path.join(config.cwd, 'node_modules', 'b'))).toEqual(false); + expect(await isPackagePresent(config, 'a')).toEqual(false); + expect(await isPackagePresent(config, 'b')).toEqual(false); }); }, ); diff --git a/__tests__/commands/install/lockfiles.js b/__tests__/commands/install/lockfiles.js index dfa63fd517..7302560cce 100644 --- a/__tests__/commands/install/lockfiles.js +++ b/__tests__/commands/install/lockfiles.js @@ -6,7 +6,7 @@ import * as reporters from '../../../src/reporters/index.js'; import {Install} from '../../../src/cli/commands/install.js'; import Lockfile from '../../../src/lockfile/wrapper.js'; import * as fs from '../../../src/util/fs.js'; -import {getPackageVersion, runInstall} from '../_helpers.js'; +import {getPackageVersion, isPackagePresent, runInstall} from '../_helpers.js'; import {promisify} from '../../../src/util/promise'; jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000; @@ -152,7 +152,7 @@ test.concurrent('install have a clean node_modules after lockfile update (branch await reinstall.init(); expect(await getPackageVersion(config, 'dep-a')).toEqual('1.2.0'); - expect(await fs.exists(path.join(config.cwd, 'node_modules/dep-b'))).toEqual(false); + expect(await isPackagePresent(config, 'dep-b')).toEqual(false); }); }); From bca802daf57d1bc8f983ae7db893705192700c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sun, 9 Jul 2017 11:58:20 +0200 Subject: [PATCH 2/2] first failing tests --- __tests__/commands/install/resolutions.js | 29 +++++++++++++++++++ .../install/resolutions/a-1/package.json | 7 +++++ .../install/resolutions/a-2/package.json | 7 +++++ .../install/resolutions/b-1/package.json | 7 +++++ .../install/resolutions/c-1/package.json | 7 +++++ .../install/resolutions/d1-1/package.json | 7 +++++ .../install/resolutions/d1-2/package.json | 7 +++++ .../install/resolutions/d1-3/package.json | 7 +++++ .../install/resolutions/d2-1/package.json | 4 +++ .../resolutions/simple-exact/package.json | 11 +++++++ .../resolutions/subtree-exact/package.json | 11 +++++++ 11 files changed, 104 insertions(+) create mode 100644 __tests__/commands/install/resolutions.js create mode 100644 __tests__/fixtures/install/resolutions/a-1/package.json create mode 100644 __tests__/fixtures/install/resolutions/a-2/package.json create mode 100644 __tests__/fixtures/install/resolutions/b-1/package.json create mode 100644 __tests__/fixtures/install/resolutions/c-1/package.json create mode 100644 __tests__/fixtures/install/resolutions/d1-1/package.json create mode 100644 __tests__/fixtures/install/resolutions/d1-2/package.json create mode 100644 __tests__/fixtures/install/resolutions/d1-3/package.json create mode 100644 __tests__/fixtures/install/resolutions/d2-1/package.json create mode 100644 __tests__/fixtures/install/resolutions/simple-exact/package.json create mode 100644 __tests__/fixtures/install/resolutions/subtree-exact/package.json diff --git a/__tests__/commands/install/resolutions.js b/__tests__/commands/install/resolutions.js new file mode 100644 index 0000000000..ee3aface82 --- /dev/null +++ b/__tests__/commands/install/resolutions.js @@ -0,0 +1,29 @@ +/* @flow */ + +import {getPackageVersion, isPackagePresent, runInstall} from '../_helpers.js'; + +test.concurrent('install with simple exact resolutions should override all versions', (): Promise => { + return runInstall({}, {source: 'resolutions', cwd: 'simple-exact'}, async config => { + expect(await getPackageVersion(config, 'a')).toEqual('1.0.0'); + expect(await getPackageVersion(config, 'b')).toEqual('1.0.0'); + expect(await getPackageVersion(config, 'd1')).toEqual('2.0.0'); + expect(await getPackageVersion(config, 'd2')).toEqual('1.0.0'); + expect(await isPackagePresent(config, 'a/d1')).toEqual(false); + expect(await isPackagePresent(config, 'a/d2')).toEqual(false); + expect(await isPackagePresent(config, 'b/d1')).toEqual(false); + expect(await isPackagePresent(config, 'b/d2')).toEqual(false); + }); +}); + +test.concurrent('install with subtree exact resolutions should override subtree versions', (): Promise => { + return runInstall({}, {source: 'resolutions', cwd: 'subtree-exact'}, async config => { + expect(await getPackageVersion(config, 'a')).toEqual('1.0.0'); + expect(await getPackageVersion(config, 'b')).toEqual('1.0.0'); + expect(await getPackageVersion(config, 'd1')).toEqual('3.0.0'); + expect(await getPackageVersion(config, 'b/d1')).toEqual('2.0.0'); + expect(await getPackageVersion(config, 'd2')).toEqual('1.0.0'); + expect(await isPackagePresent(config, 'a/d1')).toEqual(false); + expect(await isPackagePresent(config, 'a/d2')).toEqual(false); + expect(await isPackagePresent(config, 'b/d2')).toEqual(false); + }); +}); diff --git a/__tests__/fixtures/install/resolutions/a-1/package.json b/__tests__/fixtures/install/resolutions/a-1/package.json new file mode 100644 index 0000000000..645f410dd2 --- /dev/null +++ b/__tests__/fixtures/install/resolutions/a-1/package.json @@ -0,0 +1,7 @@ +{ + "name": "a", + "version": "1.0.0", + "dependencies": { + "d1": "file:../d1-1" + } +} diff --git a/__tests__/fixtures/install/resolutions/a-2/package.json b/__tests__/fixtures/install/resolutions/a-2/package.json new file mode 100644 index 0000000000..9ac781883c --- /dev/null +++ b/__tests__/fixtures/install/resolutions/a-2/package.json @@ -0,0 +1,7 @@ +{ + "name": "a", + "version": "2.0.0", + "dependencies": { + "d1": "file:../d1-2" + } +} diff --git a/__tests__/fixtures/install/resolutions/b-1/package.json b/__tests__/fixtures/install/resolutions/b-1/package.json new file mode 100644 index 0000000000..2b020f6f2a --- /dev/null +++ b/__tests__/fixtures/install/resolutions/b-1/package.json @@ -0,0 +1,7 @@ +{ + "name": "b", + "version": "1.0.0", + "dependencies": { + "d1": "file:../d1-2" + } +} diff --git a/__tests__/fixtures/install/resolutions/c-1/package.json b/__tests__/fixtures/install/resolutions/c-1/package.json new file mode 100644 index 0000000000..f630919d51 --- /dev/null +++ b/__tests__/fixtures/install/resolutions/c-1/package.json @@ -0,0 +1,7 @@ +{ + "name": "c", + "version": "1.0.0", + "dependencies": { + "a": "file:../a-2" + } +} diff --git a/__tests__/fixtures/install/resolutions/d1-1/package.json b/__tests__/fixtures/install/resolutions/d1-1/package.json new file mode 100644 index 0000000000..d59317b249 --- /dev/null +++ b/__tests__/fixtures/install/resolutions/d1-1/package.json @@ -0,0 +1,7 @@ +{ + "name": "d1", + "version": "1.0.0", + "dependencies": { + "d2": "file:../d2-1" + } +} diff --git a/__tests__/fixtures/install/resolutions/d1-2/package.json b/__tests__/fixtures/install/resolutions/d1-2/package.json new file mode 100644 index 0000000000..4f31cfe9d0 --- /dev/null +++ b/__tests__/fixtures/install/resolutions/d1-2/package.json @@ -0,0 +1,7 @@ +{ + "name": "d1", + "version": "2.0.0", + "dependencies": { + "d2": "file:../d2-1" + } +} diff --git a/__tests__/fixtures/install/resolutions/d1-3/package.json b/__tests__/fixtures/install/resolutions/d1-3/package.json new file mode 100644 index 0000000000..4ef2d38824 --- /dev/null +++ b/__tests__/fixtures/install/resolutions/d1-3/package.json @@ -0,0 +1,7 @@ +{ + "name": "d1", + "version": "3.0.0", + "dependencies": { + "d2": "file:../d2-1" + } +} diff --git a/__tests__/fixtures/install/resolutions/d2-1/package.json b/__tests__/fixtures/install/resolutions/d2-1/package.json new file mode 100644 index 0000000000..4f7cc7df78 --- /dev/null +++ b/__tests__/fixtures/install/resolutions/d2-1/package.json @@ -0,0 +1,4 @@ +{ + "name": "d2", + "version": "1.0.0" +} diff --git a/__tests__/fixtures/install/resolutions/simple-exact/package.json b/__tests__/fixtures/install/resolutions/simple-exact/package.json new file mode 100644 index 0000000000..d47addfbf3 --- /dev/null +++ b/__tests__/fixtures/install/resolutions/simple-exact/package.json @@ -0,0 +1,11 @@ +{ + "name": "project", + "version": "1.0.0", + "dependencies": { + "a": "file:../a-1", + "b": "file:../b-1" + }, + "resolutions": { + "**/d1": "file:../d1-2" + } +} diff --git a/__tests__/fixtures/install/resolutions/subtree-exact/package.json b/__tests__/fixtures/install/resolutions/subtree-exact/package.json new file mode 100644 index 0000000000..3dc0f01e72 --- /dev/null +++ b/__tests__/fixtures/install/resolutions/subtree-exact/package.json @@ -0,0 +1,11 @@ +{ + "name": "project", + "version": "1.0.0", + "dependencies": { + "a": "file:../a-1", + "b": "file:../b-1" + }, + "resolutions": { + "a/d1": "file:../d1-3" + } +}