Repository navigation
[DO NOT MERGE] SCAL-227566 Test PR for require-tests check - #747
tushardeepakts wants to merge 2 commits into
Conversation
A spec outside src/ (e.g. scripts/*.spec.js) does not test SDK code, so it must not satisfy the rule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request introduces a new script, check-tests-included.js, along with its unit tests, to ensure that any changes to the SDK source files are accompanied by corresponding test updates. It also configures Jest coverage for this script and adds a check-tests command to package.json. The code review feedback is highly constructive, focusing on improving security and robustness. Specifically, it recommends replacing execSync with execFileSync to prevent potential command injection vulnerabilities, wrapping the main function in a try-catch block for graceful error handling, and updating the unit tests to align with these changes.
I am having trouble creating individual review comments. Click here to see my feedback.
scripts/check-tests-included.js (7)
Use execFileSync instead of execSync to avoid spawning a shell, which reduces the risk of shell injection vulnerabilities when executing commands with external inputs (like baseRef).
const { execFileSync } = require('child_process');
scripts/check-tests-included.js (24-29)
Use execFileSync to safely execute the git command without shell interpolation.
function getChangedFiles(baseRef) {
const out = execFileSync('git', ['diff', '--name-only', '--diff-filter=d', 'origin/' + baseRef + '...HEAD'], {
encoding: 'utf8',
});
return out.split('\n').map((s) => s.trim()).filter(Boolean);
}
scripts/check-tests-included.js (31-41)
Wrap the main function execution in a try-catch block to handle any potential errors from getChangedFiles (such as git errors in shallow clones) gracefully, printing a clean error message and exiting with a non-zero code.
function main() {
try {
const result = check(getChangedFiles(process.env.BASE_REF || 'main'));
if (result.ok) {
console.log('✓ ' + result.reason);
return 0;
}
console.error('✗ This PR changes source code but adds or updates no tests (*.spec.ts):');
result.src.forEach((f) => console.error(' - ' + f));
console.error("Add tests, or ask a reviewer to add the 'skip-test-check' label.");
return 1;
} catch (error) {
console.error('✗ Failed to check changed files:', error.message);
return 1;
}
}
scripts/check-tests-included.spec.js (1-2)
Update the mock and import to use execFileSync instead of execSync to align with the implementation changes.
jest.mock('child_process');
const { execFileSync } = require('child_process');
scripts/check-tests-included.spec.js (30-37)
Update the test assertions to mock and verify execFileSync with the correct arguments.
test('getChangedFiles diffs against the base branch and parses output', () => {
execFileSync.mockReturnValue('src/a.ts\nsrc/a.spec.ts\n');
expect(getChangedFiles('release')).toEqual(['src/a.ts', 'src/a.spec.ts']);
expect(execFileSync).toHaveBeenCalledWith(
'git',
['diff', '--name-only', '--diff-filter=d', 'origin/release...HEAD'],
{ encoding: 'utf8' },
);
});
scripts/check-tests-included.spec.js (39-46)
Update the mock calls to use execFileSync instead of execSync.
test('main returns 1 when tests are missing and 0 when present', () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(console, 'log').mockImplementation(() => {});
execFileSync.mockReturnValue('src/a.ts\n');
expect(main()).toBe(1);
execFileSync.mockReturnValue('src/a.ts\nsrc/a.spec.ts\n');
expect(main()).toBe(0);
});
commit: |
No description provided.