add nice error message

This commit is contained in:
Simon Knott 2024-07-17 14:02:22 +02:00
parent 5cb24de3ac
commit 5b758230d4
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
2 changed files with 32 additions and 2 deletions

View file

@ -82,7 +82,29 @@ export type TestFileFilter = {
};
export async function detectChangedFiles(baseCommit: string): Promise<string[]> {
const gitFileList = (command: string) => childProcess.execSync(`git ${command}`, { encoding: 'utf-8' }).split('\n').filter(Boolean).map(file => path.resolve(file));
function gitFileList(command: string) {
try {
return childProcess.execSync(
`git ${command}`,
{ encoding: 'utf-8', stdio: 'pipe' }
).split('\n').filter(Boolean).map(file => path.resolve(file));
} catch (_error) {
const error = _error as childProcess.SpawnSyncReturns<string>;
throw new Error([
`Encountered error while detecting changed files.`,
`--only-changed only works with Git repositories.`,
`Make sure that:`,
` - You are running the test in a Git repository.`,
` - The Git binary is in your PATH.`,
` - The passed Git Ref exists in the repository. You passed '${baseCommit}'.`,
``,
`Command Output:`,
``,
...error.output,
].join('\n'));
}
}
const untrackedFiles = gitFileList(`ls-files --others --exclude-standard`);
const trackedFilesWithChanges = gitFileList(`diff ${baseCommit} --name-only`);

View file

@ -155,4 +155,12 @@ test('should support watch mode', async ({ setupRepository, writeFiles, runWatch
await testProcess.waitForOutput('b.spec.ts:3:13 fails');
expect(testProcess.output).not.toContain('a.spec');
});
});
test('should throw nice error message if git doesnt work', async ({ setupRepository, runInlineTest }) => {
await setupRepository();
const result = await runInlineTest({}, { 'only-changed': `this-commit-does-not-exist` });
expect(result.exitCode).toBe(1);
expect(result.output).toContain('only works with Git repositories');
});