Skip to content

Commit

Permalink
Merge pull request #1098 from Green-Software-Foundation/fix-if-check
Browse files Browse the repository at this point in the history
Fix the if-check when running it in global
  • Loading branch information
narekhovhannisyan authored Dec 12, 2024
2 parents dd3f360 + ba17c13 commit abe95f9
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
101 changes: 101 additions & 0 deletions src/__tests__/if-check/util/npm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,51 @@ jest.mock('child_process', () => {
Array.isArray(args) ? `${file} ${args.join(' ')}` : file.trim()
)
).toBeTruthy();
break;
case 'if-check-prefix':
expect(
[
'npm run if-env -- --prefix=.. -m ./src/__mocks__/mock-manifest.yaml',
'npm run if-run -- --prefix=.. -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest',
'npm run if-diff -- --prefix=.. -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml',
'node -p Boolean(process.stdout.isTTY)',
].includes(
Array.isArray(args) ? `${file} ${args.join(' ')}` : file.trim()
)
).toBeTruthy();
break;
case 'if-check-global':
expect(
[
'if-env -m ./src/__mocks__/mock-manifest.yaml',
'if-run -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest',
'if-diff -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml',
'node -p Boolean(process.stdout.isTTY)',
].includes(
Array.isArray(args) ? `${file} ${args.join(' ')}` : file.trim()
)
).toBeTruthy();

break;
case 'if-check-tty':
expect(
[
'if-env -m ./src/__mocks__/mock-manifest.yaml',
'if-run -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest',
'if-diff -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml',
'tty | if-diff -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml',
'node -p Boolean(process.stdout.isTTY)',
].includes(
Array.isArray(args) ? `${file} ${args.join(' ')}` : file.trim()
)
).toBeTruthy();

break;
}

if (process.env.NPM_INSTALL === 'if-check-tty') {
return true;
}
return;
},
};
Expand All @@ -44,7 +87,16 @@ jest.mock('child_process', () => {
import {executeCommands} from '../../../if-check/util/npm';

describe('if-check/util/npm: ', () => {
const originalEnv = process.env;
describe('executeCommands(): ', () => {
beforeEach(() => {
jest.clearAllMocks();
});

afterEach(() => {
process.env = originalEnv;
});

it('successfully executes with correct commands.', async () => {
process.env.NPM_INSTALL = 'if-check';
const manifest = './src/__mocks__/mock-manifest.yaml';
Expand All @@ -57,5 +109,54 @@ describe('if-check/util/npm: ', () => {
'✔ if-check successfully verified mock-manifest.yaml\n'
);
});

it('successfully executes with prefix.', async () => {
process.env.CURRENT_DIR = 'mock-dir';
process.env.NPM_INSTALL = 'if-check-prefix';
const manifest = './src/__mocks__/mock-manifest.yaml';
const logSpy = jest.spyOn(global.console, 'log');

await executeCommands(manifest, false);

expect.assertions(6);
expect(logSpy).toHaveBeenCalledWith(
'✔ if-check successfully verified mock-manifest.yaml\n'
);
delete process.env.CURRENT_DIR;
});

it('successfully executes when it runs from the global.', async () => {
process.env.NPM_INSTALL = 'if-check-global';
process.env.npm_config_global = 'true';

const manifest = './src/__mocks__/mock-manifest.yaml';
const logSpy = jest.spyOn(global.console, 'log');

await executeCommands(manifest, false);

expect.assertions(6);
expect(logSpy).toHaveBeenCalledWith(
'✔ if-check successfully verified mock-manifest.yaml\n'
);
});

it('successfully executes when the `tty` is true.', async () => {
const originalIsTTY = process.stdin.isTTY;
process.stdin.isTTY = true;
process.env.NPM_INSTALL = 'if-check-tty';
process.env.npm_config_global = 'true';

const manifest = './src/__mocks__/mock-manifest.yaml';
const logSpy = jest.spyOn(global.console, 'log');

await executeCommands(manifest, false);

expect.assertions(6);
expect(logSpy).toHaveBeenCalledWith(
'✔ if-check successfully verified mock-manifest.yaml\n'
);

process.stdin.isTTY = originalIsTTY;
});
});
});
6 changes: 3 additions & 3 deletions src/if-check/util/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const executeCommands = async (manifest: string, cwd: boolean) => {
const ifEnvCommand = [
isGlobal ? 'if-env' : 'npm',
...(isGlobal ? [] : ['run', 'if-env']),
'--',
isGlobal ? '' : '--',
...(prefixFlag === '' ? [] : [prefixFlag]),
'-m',
sanitizedManifest,
Expand All @@ -39,7 +39,7 @@ export const executeCommands = async (manifest: string, cwd: boolean) => {
const ifRunCommand = [
isGlobal ? 'if-run' : 'npm',
...(isGlobal ? [] : ['run', 'if-run']),
'--',
isGlobal ? '' : '--',
...(prefixFlag === '' ? [] : [prefixFlag]),
'-m',
sanitizedManifest,
Expand All @@ -51,7 +51,7 @@ export const executeCommands = async (manifest: string, cwd: boolean) => {
const ifDiffCommand = [
isGlobal ? 'if-diff' : 'npm',
...(isGlobal ? [] : ['run', 'if-diff']),
'--',
isGlobal ? '' : '--',
...(prefixFlag === '' ? [] : [prefixFlag]),
'-s',
`${sanitizedExecutedManifest}.yaml`,
Expand Down

0 comments on commit abe95f9

Please sign in to comment.