Skip to content

Commit 52bcb53

Browse files
Merge pull request #1088 from Green-Software-Foundation/code-ql
Provide codeql fixes
2 parents 77edf4a + 4447955 commit 52bcb53

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

src/__tests__/if-check/util/npm.test.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,29 @@ jest.mock('../../../common/util/logger', () => ({
1111
},
1212
}));
1313

14-
jest.mock('../../../common/util/helpers', () => {
15-
const originalModule = jest.requireActual('../../../common/util/helpers');
14+
jest.mock('child_process', () => {
15+
const originalModule = jest.requireActual('child_process');
1616
return {
1717
...originalModule,
18-
execPromise: async (param: any) => {
18+
execFileSync: (file: any, args: any) => {
1919
switch (process.env.NPM_INSTALL) {
2020
case 'true':
21-
expect(param).toEqual('npm install @grnsft/if@0.3.3-beta.0');
21+
expect(file).toEqual('npm install @grnsft/if@0.3.3-beta.0');
2222
break;
2323
case 'npm init -y':
24-
expect(param).toEqual('npm init -y');
24+
expect(file).toEqual('npm init -y');
2525
break;
2626
case 'if-check':
27-
expect(param).toEqual(
28-
"npm run if-env -- -m ./src/__mocks__/mock-manifest.yaml && npm run if-run -- -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest && node -p 'Boolean(process.stdout.isTTY)' | npm run if-diff -- -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml"
29-
);
27+
expect(
28+
[
29+
'npm run if-env -- -m ./src/__mocks__/mock-manifest.yaml',
30+
'npm run if-run -- -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest',
31+
'node -p Boolean(process.stdout.isTTY)',
32+
'npm run if-diff -- -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml',
33+
].includes(
34+
Array.isArray(args) ? `${file} ${args.join(' ')}` : file.trim()
35+
)
36+
).toBeTruthy();
3037
break;
3138
case 'if-check-cwd':
3239
expect(param).toEqual(
@@ -60,7 +67,7 @@ describe('if-check/util/npm: ', () => {
6067

6168
await executeCommands(manifest, false);
6269

63-
expect.assertions(2);
70+
expect.assertions(6);
6471
expect(logSpy).toHaveBeenCalledWith(
6572
'✔ if-check successfully verified mock-manifest.yaml\n'
6673
);

src/if-check/util/npm.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from 'node:path';
2-
import {execPromise} from '../../common/util/helpers';
2+
import {execFileSync} from 'child_process';
33
import {getFileName, removeFileIfExists} from '../../common/util/fs';
44
import {STRINGS} from '../config';
55

@@ -28,47 +28,63 @@ export const executeCommands = async (manifest: string, cwd: boolean) => {
2828
const sanitizedExecutedManifest = escapeShellArg(executedManifest);
2929

3030
const ifEnvCommand = [
31-
isGlobal ? 'if-env' : 'npm run if-env',
31+
isGlobal ? 'if-env' : 'npm',
32+
...(isGlobal ? [] : ['run', 'if-env']),
3233
'--',
33-
...(prefixFlag === '' ? [] : prefixFlag),
34+
...(prefixFlag === '' ? [] : [prefixFlag]),
3435
'-m',
3536
sanitizedManifest,
3637
];
3738

3839
const ifRunCommand = [
39-
isGlobal ? 'if-run' : 'npm run if-run',
40+
isGlobal ? 'if-run' : 'npm',
41+
...(isGlobal ? [] : ['run', 'if-run']),
4042
'--',
41-
...(prefixFlag === '' ? [] : prefixFlag),
43+
...(prefixFlag === '' ? [] : [prefixFlag]),
4244
'-m',
4345
sanitizedManifest,
4446
'-o',
4547
sanitizedExecutedManifest,
4648
];
4749

48-
const ttyCommand = ['node', '-p', "'Boolean(process.stdout.isTTY)'"];
50+
const ttyCommand = ['node', '-p', 'Boolean(process.stdout.isTTY)'];
4951
const ifDiffCommand = [
50-
isGlobal ? 'if-diff' : 'npm run if-diff',
52+
isGlobal ? 'if-diff' : 'npm',
53+
...(isGlobal ? [] : ['run', 'if-diff']),
5154
'--',
52-
...(prefixFlag === '' ? [] : prefixFlag),
55+
...(prefixFlag === '' ? [] : [prefixFlag]),
5356
'-s',
5457
`${sanitizedExecutedManifest}.yaml`,
5558
'-t',
5659
sanitizedManifest,
5760
];
5861

59-
const fullCommand = [
60-
...ifEnvCommand,
61-
'&&',
62-
...ifRunCommand,
63-
'&&',
64-
...ttyCommand,
65-
'|',
66-
...ifDiffCommand,
67-
].join(' ');
68-
69-
// Execute the full command
70-
await execPromise(fullCommand, {
62+
execFileSync(ifEnvCommand[0], ifEnvCommand.slice(1), {
7163
cwd: process.env.CURRENT_DIR || process.cwd(),
64+
shell: true,
65+
});
66+
67+
execFileSync(ifRunCommand[0], ifRunCommand.slice(1), {
68+
cwd: process.env.CURRENT_DIR || process.cwd(),
69+
});
70+
71+
execFileSync(ttyCommand[0], ttyCommand.slice(1), {
72+
cwd: process.env.CURRENT_DIR || process.cwd(),
73+
});
74+
75+
const ttyResult = execFileSync(ttyCommand[0], ttyCommand.slice(1), {
76+
cwd: process.env.CURRENT_DIR || process.cwd(),
77+
});
78+
79+
const tty = ttyResult && ttyResult.toString().trim();
80+
const fullCommand = `${tty === 'true' ? 'tty |' : ''} ${ifDiffCommand.join(
81+
' '
82+
)}`;
83+
84+
execFileSync(fullCommand, {
85+
cwd: process.env.CURRENT_DIR || process.cwd(),
86+
stdio: 'inherit',
87+
shell: true,
7288
});
7389

7490
if (!cwd) {

0 commit comments

Comments
 (0)