Skip to content

Commit

Permalink
Merge pull request #1088 from Green-Software-Foundation/code-ql
Browse files Browse the repository at this point in the history
Provide codeql fixes
  • Loading branch information
narekhovhannisyan authored Dec 6, 2024
2 parents 77edf4a + 4447955 commit 52bcb53
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
25 changes: 16 additions & 9 deletions src/__tests__/if-check/util/npm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@ jest.mock('../../../common/util/logger', () => ({
},
}));

jest.mock('../../../common/util/helpers', () => {
const originalModule = jest.requireActual('../../../common/util/helpers');
jest.mock('child_process', () => {
const originalModule = jest.requireActual('child_process');
return {
...originalModule,
execPromise: async (param: any) => {
execFileSync: (file: any, args: any) => {
switch (process.env.NPM_INSTALL) {
case 'true':
expect(param).toEqual('npm install @grnsft/[email protected]');
expect(file).toEqual('npm install @grnsft/[email protected]');
break;
case 'npm init -y':
expect(param).toEqual('npm init -y');
expect(file).toEqual('npm init -y');
break;
case 'if-check':
expect(param).toEqual(
"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"
);
expect(
[
'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',
].includes(
Array.isArray(args) ? `${file} ${args.join(' ')}` : file.trim()
)
).toBeTruthy();
break;
case 'if-check-cwd':
expect(param).toEqual(
Expand Down Expand Up @@ -60,7 +67,7 @@ describe('if-check/util/npm: ', () => {

await executeCommands(manifest, false);

expect.assertions(2);
expect.assertions(6);
expect(logSpy).toHaveBeenCalledWith(
'✔ if-check successfully verified mock-manifest.yaml\n'
);
Expand Down
56 changes: 36 additions & 20 deletions src/if-check/util/npm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'node:path';
import {execPromise} from '../../common/util/helpers';
import {execFileSync} from 'child_process';
import {getFileName, removeFileIfExists} from '../../common/util/fs';
import {STRINGS} from '../config';

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

const ifEnvCommand = [
isGlobal ? 'if-env' : 'npm run if-env',
isGlobal ? 'if-env' : 'npm',
...(isGlobal ? [] : ['run', 'if-env']),
'--',
...(prefixFlag === '' ? [] : prefixFlag),
...(prefixFlag === '' ? [] : [prefixFlag]),
'-m',
sanitizedManifest,
];

const ifRunCommand = [
isGlobal ? 'if-run' : 'npm run if-run',
isGlobal ? 'if-run' : 'npm',
...(isGlobal ? [] : ['run', 'if-run']),
'--',
...(prefixFlag === '' ? [] : prefixFlag),
...(prefixFlag === '' ? [] : [prefixFlag]),
'-m',
sanitizedManifest,
'-o',
sanitizedExecutedManifest,
];

const ttyCommand = ['node', '-p', "'Boolean(process.stdout.isTTY)'"];
const ttyCommand = ['node', '-p', 'Boolean(process.stdout.isTTY)'];
const ifDiffCommand = [
isGlobal ? 'if-diff' : 'npm run if-diff',
isGlobal ? 'if-diff' : 'npm',
...(isGlobal ? [] : ['run', 'if-diff']),
'--',
...(prefixFlag === '' ? [] : prefixFlag),
...(prefixFlag === '' ? [] : [prefixFlag]),
'-s',
`${sanitizedExecutedManifest}.yaml`,
'-t',
sanitizedManifest,
];

const fullCommand = [
...ifEnvCommand,
'&&',
...ifRunCommand,
'&&',
...ttyCommand,
'|',
...ifDiffCommand,
].join(' ');

// Execute the full command
await execPromise(fullCommand, {
execFileSync(ifEnvCommand[0], ifEnvCommand.slice(1), {
cwd: process.env.CURRENT_DIR || process.cwd(),
shell: true,
});

execFileSync(ifRunCommand[0], ifRunCommand.slice(1), {
cwd: process.env.CURRENT_DIR || process.cwd(),
});

execFileSync(ttyCommand[0], ttyCommand.slice(1), {
cwd: process.env.CURRENT_DIR || process.cwd(),
});

const ttyResult = execFileSync(ttyCommand[0], ttyCommand.slice(1), {
cwd: process.env.CURRENT_DIR || process.cwd(),
});

const tty = ttyResult && ttyResult.toString().trim();
const fullCommand = `${tty === 'true' ? 'tty |' : ''} ${ifDiffCommand.join(
' '
)}`;

execFileSync(fullCommand, {
cwd: process.env.CURRENT_DIR || process.cwd(),
stdio: 'inherit',
shell: true,
});

if (!cwd) {
Expand Down

0 comments on commit 52bcb53

Please sign in to comment.