Skip to content

Commit 634c239

Browse files
kaylieEBarcanis
authored andcommitted
Selective version resolution feature (#4105)
* refactor some test helpers * first failing tests * WIP * Uses "ResolutionMap" instead of the duplicate name "Resolutions"
1 parent 50508b2 commit 634c239

File tree

24 files changed

+311
-49
lines changed

24 files changed

+311
-49
lines changed

__tests__/commands/_helpers.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,23 @@ export function explodeLockfile(lockfile: string): Array<string> {
4646
}
4747

4848
export async function getPackageVersion(config: Config, packagePath: string): Promise<string> {
49-
const loc = path.join(config.cwd, `node_modules/${packagePath.replace(/\//g, '/node_modules/')}/package.json`);
50-
const json = JSON.parse(await fs.readFile(loc));
51-
return json.version;
49+
return (await getPackageManifest(config, packagePath)).version;
50+
}
51+
52+
export function getPackageManifest(config: Config, packagePath: string): Promise<any> {
53+
return fs.readJson(getPackageManifestPath(config, packagePath));
54+
}
55+
56+
export function getPackageManifestPath(config: Config, packagePath: string): string {
57+
return path.join(getPackagePath(config, packagePath), 'package.json');
58+
}
59+
60+
export function isPackagePresent(config: Config, packagePath: string): Promise<boolean> {
61+
return fs.exists(getPackagePath(config, packagePath));
62+
}
63+
64+
export function getPackagePath(config: Config, packagePath: string): string {
65+
return path.join(config.cwd, `node_modules/${packagePath.replace(/\//g, '/node_modules/')}`);
5266
}
5367

5468
export function makeConfigFromDirectory(cwd: string, reporter: Reporter, flags: Object = {}): Promise<Config> {

__tests__/commands/install/integration-deduping.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import {getPackageVersion, runInstall} from '../_helpers.js';
3+
import {getPackageVersion, getPackageManifestPath, runInstall} from '../_helpers.js';
44
import * as fs from '../../../src/util/fs.js';
55

66
const path = require('path');
@@ -204,8 +204,8 @@ test.concurrent('install should hardlink repeated dependencies', (): Promise<voi
204204
// B@1 -> A@2
205205
// C@1 -> A@2 (this is hardlink to B@1->A@2)
206206
return runInstall({linkDuplicates: true}, 'hardlink-repeated-dependencies', async config => {
207-
const b_a = await fs.stat(path.join(config.cwd, 'node_modules/b/node_modules/a/package.json'));
208-
const c_a = await fs.stat(path.join(config.cwd, 'node_modules/c/node_modules/a/package.json'));
207+
const b_a = await fs.stat(getPackageManifestPath(config, 'b/a'));
208+
const c_a = await fs.stat(getPackageManifestPath(config, 'c/a'));
209209
expect(b_a.ino).toEqual(c_a.ino);
210210
});
211211
});
@@ -215,8 +215,8 @@ test.concurrent('install should not hardlink repeated dependencies if linkDuplic
215215
// B@1 -> A@2
216216
// C@1 -> A@2
217217
return runInstall({linkDuplicates: false}, 'hardlink-repeated-dependencies', async config => {
218-
const b_a = await fs.stat(path.join(config.cwd, 'node_modules/b/node_modules/a/package.json'));
219-
const c_a = await fs.stat(path.join(config.cwd, 'node_modules/c/node_modules/a/package.json'));
218+
const b_a = await fs.stat(getPackageManifestPath(config, 'b/a'));
219+
const c_a = await fs.stat(getPackageManifestPath(config, 'c/a'));
220220
expect(b_a.ino).not.toEqual(c_a.ino);
221221
});
222222
});

__tests__/commands/install/integration-hoisting.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* @flow */
22

3-
import {getPackageVersion, runInstall} from '../_helpers.js';
4-
import * as fs from '../../../src/util/fs.js';
5-
6-
const path = require('path');
3+
import {getPackageVersion, isPackagePresent, runInstall} from '../_helpers.js';
74

85
jasmine.DEFAULT_TIMEOUT_INTERVAL = 120000;
96

@@ -21,8 +18,8 @@ test.concurrent(
2118
'install hoister should not install prioritised popular transitive devDependencies in --prod mode',
2219
(): Promise<void> => {
2320
return runInstall({production: true}, 'install-prod-prioritized-popular-transitive-dev-dep', async config => {
24-
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'a'))).toEqual(false);
25-
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'b'))).toEqual(false);
21+
expect(await isPackagePresent(config, 'a')).toEqual(false);
22+
expect(await isPackagePresent(config, 'b')).toEqual(false);
2623
});
2724
},
2825
);

__tests__/commands/install/lockfiles.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as reporters from '../../../src/reporters/index.js';
66
import {Install} from '../../../src/cli/commands/install.js';
77
import Lockfile from '../../../src/lockfile/wrapper.js';
88
import * as fs from '../../../src/util/fs.js';
9-
import {getPackageVersion, runInstall} from '../_helpers.js';
9+
import {getPackageVersion, isPackagePresent, runInstall} from '../_helpers.js';
1010
import {promisify} from '../../../src/util/promise';
1111

1212
jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000;
@@ -152,7 +152,7 @@ test.concurrent('install have a clean node_modules after lockfile update (branch
152152
await reinstall.init();
153153

154154
expect(await getPackageVersion(config, 'dep-a')).toEqual('1.2.0');
155-
expect(await fs.exists(path.join(config.cwd, 'node_modules/dep-b'))).toEqual(false);
155+
expect(await isPackagePresent(config, 'dep-b')).toEqual(false);
156156
});
157157
});
158158

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* @flow */
2+
3+
import {getPackageVersion, isPackagePresent, runInstall} from '../_helpers.js';
4+
5+
test.concurrent('install with simple exact resolutions should override all versions', (): Promise<void> => {
6+
return runInstall({}, {source: 'resolutions', cwd: 'simple-exact'}, async config => {
7+
expect(await getPackageVersion(config, 'a')).toEqual('1.0.0');
8+
expect(await getPackageVersion(config, 'b')).toEqual('1.0.0');
9+
expect(await getPackageVersion(config, 'd1')).toEqual('2.0.0');
10+
expect(await getPackageVersion(config, 'd2')).toEqual('1.0.0');
11+
expect(await isPackagePresent(config, 'a/d1')).toEqual(false);
12+
expect(await isPackagePresent(config, 'a/d2')).toEqual(false);
13+
expect(await isPackagePresent(config, 'b/d1')).toEqual(false);
14+
expect(await isPackagePresent(config, 'b/d2')).toEqual(false);
15+
});
16+
});
17+
18+
test.concurrent('install with subtree exact resolutions should override subtree versions', (): Promise<void> => {
19+
return runInstall({}, {source: 'resolutions', cwd: 'subtree-exact'}, async config => {
20+
expect(await getPackageVersion(config, 'left-pad')).toEqual('1.0.0');
21+
expect(await getPackageVersion(config, 'd2')).toEqual('1.0.0');
22+
expect(await getPackageVersion(config, 'd2/left-pad')).toEqual('1.1.1');
23+
expect(await getPackageVersion(config, 'c')).toEqual('1.0.0');
24+
expect(await getPackageVersion(config, 'c/left-pad')).toEqual('1.1.2');
25+
});
26+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "a",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"d1": "file:../d1-1"
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "a",
3+
"version": "2.0.0",
4+
"dependencies": {
5+
"d1": "file:../d1-2"
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "b",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"d1": "file:../d1-2"
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "c",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"left-pad": "~1.1.1"
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "d1",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"d2": "file:../d2-1"
6+
}
7+
}

0 commit comments

Comments
 (0)