From a088403046420a25242eacbd96f41a351e4d9170 Mon Sep 17 00:00:00 2001 From: manushak Date: Tue, 10 Dec 2024 19:49:24 +0400 Subject: [PATCH 1/2] fix(util): fix if-check command when run it in global --- src/if-check/util/npm.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/if-check/util/npm.ts b/src/if-check/util/npm.ts index 7688e839..763aff97 100644 --- a/src/if-check/util/npm.ts +++ b/src/if-check/util/npm.ts @@ -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, @@ -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, @@ -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`, From ba17c13bc5f57d1bc1b9ae86724c6defc89e82bc Mon Sep 17 00:00:00 2001 From: manushak Date: Tue, 10 Dec 2024 19:50:01 +0400 Subject: [PATCH 2/2] test(util): add test coverage --- src/__tests__/if-check/util/npm.test.ts | 101 ++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/src/__tests__/if-check/util/npm.test.ts b/src/__tests__/if-check/util/npm.test.ts index d7b4d798..af85af98 100644 --- a/src/__tests__/if-check/util/npm.test.ts +++ b/src/__tests__/if-check/util/npm.test.ts @@ -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; }, }; @@ -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'; @@ -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; + }); }); });