Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the if-check when running it in global #1098

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading