Skip to content

Commit e69f690

Browse files
feat(CSENG-116): Forward showNpmScope feature flag [CSENG-116] (#36)
1 parent cb49939 commit e69f690

File tree

9 files changed

+244
-8
lines changed

9 files changed

+244
-8
lines changed

lib/npm-modules-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { PkgTree } from 'snyk-nodejs-lockfile-parser';
99

1010
const debug = baseDebug('snyk-nodejs-plugin');
1111

12-
export async function parse(
12+
export function parse(
1313
root: string,
1414
targetFile: string,
1515
options: Options,

lib/workspaces/npm-workspaces-parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export async function processNpmWorkspaces(
6565
strictOutOfSync?: boolean;
6666
dev?: boolean;
6767
yarnWorkspaces?: boolean;
68+
showNpmScope?: boolean;
6869
},
6970
targetFiles: string[],
7071
): Promise<MultiProjectResultCustom> {
@@ -146,6 +147,7 @@ export async function processNpmWorkspaces(
146147
strictOutOfSync: settings.strictOutOfSync || false,
147148
includeOptionalDeps: false,
148149
pruneCycles: true,
150+
showNpmScope: settings.showNpmScope,
149151
},
150152
);
151153

lib/workspaces/pnpm-workspaces-parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export async function processPnpmWorkspaces(
1515
dev?: boolean;
1616
optional?: boolean;
1717
exclude?: string;
18+
showNpmScope?: boolean;
1819
},
1920
targetFiles: string[],
2021
): Promise<MultiProjectResultCustom> {
@@ -53,6 +54,7 @@ export async function processPnpmWorkspaces(
5354
? true
5455
: settings.strictOutOfSync,
5556
exclude: settings.exclude,
57+
showNpmScope: settings.showNpmScope,
5658
});
5759
result.scannedProjects = result.scannedProjects.concat(
5860
scannedProjects as ScannedProjectCustom[],

lib/workspaces/yarn-workspaces-parser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export async function processYarnWorkspaces(
6868
strictOutOfSync?: boolean;
6969
dev?: boolean;
7070
yarnWorkspaces?: boolean;
71+
showNpmScope?: boolean;
7172
},
7273
targetFiles: string[],
7374
): Promise<MultiProjectResultCustom> {
@@ -164,6 +165,7 @@ export async function processYarnWorkspaces(
164165
settings.strictOutOfSync === undefined
165166
? true
166167
: settings.strictOutOfSync,
168+
showNpmScope: settings.showNpmScope,
167169
},
168170
);
169171
break;
@@ -179,6 +181,7 @@ export async function processYarnWorkspaces(
179181
settings.strictOutOfSync === undefined
180182
? true
181183
: settings.strictOutOfSync,
184+
showNpmScope: settings.showNpmScope,
182185
},
183186
{
184187
isWorkspacePkg: true,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"lodash.isempty": "^4.4.0",
4343
"lodash.sortby": "^4.7.0",
4444
"micromatch": "4.0.8",
45-
"snyk-nodejs-lockfile-parser": "2.4.3",
45+
"snyk-nodejs-lockfile-parser": "2.5.0",
4646
"snyk-resolve-deps": "4.8.0"
4747
},
4848
"overrides": {

test/fixtures/workspace-multi-type/npm-workspace/package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
"dependencies": {
77
"express": "4.12"
88
},
9-
"workspaces": {
10-
"packages": [
11-
"packages/a",
12-
"packages/b"
13-
]
14-
}
9+
"workspaces": [
10+
"packages/a",
11+
"packages/b"
12+
]
1513
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { processNpmWorkspaces } from '../../lib';
2+
import * as path from 'path';
3+
4+
describe('process npm workspaces', () => {
5+
const originalCurrentWorkingDirectory = process.cwd();
6+
7+
afterEach(() => {
8+
process.chdir(originalCurrentWorkingDirectory);
9+
});
10+
11+
describe('showNpmScope feature flag forwarding', () => {
12+
it('should forward showNpmScope flag when set to true', async () => {
13+
const fixturePath = path.resolve(
14+
__dirname,
15+
'..',
16+
'fixtures',
17+
'workspace-multi-type',
18+
);
19+
process.chdir(fixturePath);
20+
const currentDir = process.cwd();
21+
22+
const result = await processNpmWorkspaces(
23+
currentDir,
24+
{ showNpmScope: true },
25+
[
26+
`${currentDir}/npm-workspace/package-lock.json`,
27+
`${currentDir}/npm-workspace/package.json`,
28+
`${currentDir}/npm-workspace/packages/a/package.json`,
29+
`${currentDir}/npm-workspace/packages/b/package.json`,
30+
],
31+
);
32+
33+
expect(result.plugin.name).toEqual('snyk-nodejs-npm-workspaces');
34+
expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1);
35+
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
36+
});
37+
38+
it('should forward showNpmScope flag when set to false', async () => {
39+
const fixturePath = path.resolve(
40+
__dirname,
41+
'..',
42+
'fixtures',
43+
'workspace-multi-type',
44+
);
45+
process.chdir(fixturePath);
46+
const currentDir = process.cwd();
47+
48+
const result = await processNpmWorkspaces(
49+
currentDir,
50+
{ showNpmScope: false },
51+
[
52+
`${currentDir}/npm-workspace/package-lock.json`,
53+
`${currentDir}/npm-workspace/package.json`,
54+
`${currentDir}/npm-workspace/packages/a/package.json`,
55+
`${currentDir}/npm-workspace/packages/b/package.json`,
56+
],
57+
);
58+
59+
expect(result.plugin.name).toEqual('snyk-nodejs-npm-workspaces');
60+
expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1);
61+
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
62+
});
63+
64+
it('should work correctly when showNpmScope is undefined (backward compatibility)', async () => {
65+
const fixturePath = path.resolve(
66+
__dirname,
67+
'..',
68+
'fixtures',
69+
'workspace-multi-type',
70+
);
71+
process.chdir(fixturePath);
72+
const currentDir = process.cwd();
73+
74+
const result = await processNpmWorkspaces(currentDir, {}, [
75+
`${currentDir}/npm-workspace/package-lock.json`,
76+
`${currentDir}/npm-workspace/package.json`,
77+
`${currentDir}/npm-workspace/packages/a/package.json`,
78+
`${currentDir}/npm-workspace/packages/b/package.json`,
79+
]);
80+
81+
expect(result.plugin.name).toEqual('snyk-nodejs-npm-workspaces');
82+
expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1);
83+
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
84+
});
85+
});
86+
});

test/workspaces/pnpm-workspaces-parser.spec.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,73 @@ describe('process pnpm workspaces', () => {
196196
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
197197
});
198198
});
199+
200+
describe('showNpmScope feature flag forwarding', () => {
201+
it('should forward showNpmScope flag when set to true', async () => {
202+
const fixturePath = path.resolve(
203+
__dirname,
204+
'..',
205+
'fixtures',
206+
'pnpm',
207+
'lock-v9',
208+
'workspace-with-isolated-pkgs',
209+
);
210+
process.chdir(fixturePath);
211+
const currentDir = process.cwd();
212+
213+
const result = await processPnpmWorkspaces(
214+
currentDir,
215+
{ showNpmScope: true },
216+
[`${currentDir}/pnpm-lock.yaml`],
217+
);
218+
219+
expect(result.plugin.name).toEqual('snyk-nodejs-pnpm-workspaces');
220+
expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1);
221+
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
222+
});
223+
224+
it('should forward showNpmScope flag when set to false', async () => {
225+
const fixturePath = path.resolve(
226+
__dirname,
227+
'..',
228+
'fixtures',
229+
'pnpm',
230+
'lock-v9',
231+
'workspace-with-isolated-pkgs',
232+
);
233+
process.chdir(fixturePath);
234+
const currentDir = process.cwd();
235+
236+
const result = await processPnpmWorkspaces(
237+
currentDir,
238+
{ showNpmScope: false },
239+
[`${currentDir}/pnpm-lock.yaml`],
240+
);
241+
242+
expect(result.plugin.name).toEqual('snyk-nodejs-pnpm-workspaces');
243+
expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1);
244+
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
245+
});
246+
247+
it('should work correctly when showNpmScope is undefined (backward compatibility)', async () => {
248+
const fixturePath = path.resolve(
249+
__dirname,
250+
'..',
251+
'fixtures',
252+
'pnpm',
253+
'lock-v9',
254+
'workspace-with-isolated-pkgs',
255+
);
256+
process.chdir(fixturePath);
257+
const currentDir = process.cwd();
258+
259+
const result = await processPnpmWorkspaces(currentDir, {}, [
260+
`${currentDir}/pnpm-lock.yaml`,
261+
]);
262+
263+
expect(result.plugin.name).toEqual('snyk-nodejs-pnpm-workspaces');
264+
expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1);
265+
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
266+
});
267+
});
199268
});

test/workspaces/yarn-workspaces-parser.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,80 @@ describe('packageJsonBelongsToWorkspace Windows', () => {
182182
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
183183
});
184184
});
185+
186+
describe('showNpmScope feature flag forwarding', () => {
187+
it('should forward showNpmScope flag when set to true', async () => {
188+
const fixturePath = path.resolve(
189+
__dirname,
190+
'..',
191+
'fixtures',
192+
'workspace-multi-type',
193+
);
194+
process.chdir(fixturePath);
195+
const currentDir = process.cwd();
196+
197+
const result = await processYarnWorkspaces(
198+
currentDir,
199+
{ showNpmScope: true },
200+
[
201+
`${currentDir}/yarn-workspace/yarn.lock`,
202+
`${currentDir}/yarn-workspace/package.json`,
203+
`${currentDir}/yarn-workspace/packages/pkg-a/package.json`,
204+
`${currentDir}/yarn-workspace/packages/pkg-b/package.json`,
205+
],
206+
);
207+
208+
expect(result.plugin.name).toEqual('snyk-nodejs-yarn-workspaces');
209+
expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1);
210+
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
211+
});
212+
213+
it('should forward showNpmScope flag when set to false', async () => {
214+
const fixturePath = path.resolve(
215+
__dirname,
216+
'..',
217+
'fixtures',
218+
'workspace-multi-type',
219+
);
220+
process.chdir(fixturePath);
221+
const currentDir = process.cwd();
222+
223+
const result = await processYarnWorkspaces(
224+
currentDir,
225+
{ showNpmScope: false },
226+
[
227+
`${currentDir}/yarn-workspace/yarn.lock`,
228+
`${currentDir}/yarn-workspace/package.json`,
229+
`${currentDir}/yarn-workspace/packages/pkg-a/package.json`,
230+
`${currentDir}/yarn-workspace/packages/pkg-b/package.json`,
231+
],
232+
);
233+
234+
expect(result.plugin.name).toEqual('snyk-nodejs-yarn-workspaces');
235+
expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1);
236+
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
237+
});
238+
239+
it('should work correctly when showNpmScope is undefined (backward compatibility)', async () => {
240+
const fixturePath = path.resolve(
241+
__dirname,
242+
'..',
243+
'fixtures',
244+
'workspace-multi-type',
245+
);
246+
process.chdir(fixturePath);
247+
const currentDir = process.cwd();
248+
249+
const result = await processYarnWorkspaces(currentDir, {}, [
250+
`${currentDir}/yarn-workspace/yarn.lock`,
251+
`${currentDir}/yarn-workspace/package.json`,
252+
`${currentDir}/yarn-workspace/packages/pkg-a/package.json`,
253+
`${currentDir}/yarn-workspace/packages/pkg-b/package.json`,
254+
]);
255+
256+
expect(result.plugin.name).toEqual('snyk-nodejs-yarn-workspaces');
257+
expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1);
258+
expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({});
259+
});
260+
});
185261
});

0 commit comments

Comments
 (0)