From 5b758230d403c122fc8dfb5085735690e3619e39 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Wed, 17 Jul 2024 14:02:22 +0200 Subject: [PATCH] add nice error message --- packages/playwright/src/util.ts | 24 +++++++++++++++++++++- tests/playwright-test/only-changed.spec.ts | 10 ++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/playwright/src/util.ts b/packages/playwright/src/util.ts index 9d076e0731..4102f446b8 100644 --- a/packages/playwright/src/util.ts +++ b/packages/playwright/src/util.ts @@ -82,7 +82,29 @@ export type TestFileFilter = { }; export async function detectChangedFiles(baseCommit: string): Promise { - 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; + 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`); diff --git a/tests/playwright-test/only-changed.spec.ts b/tests/playwright-test/only-changed.spec.ts index d204562b44..529eee2b06 100644 --- a/tests/playwright-test/only-changed.spec.ts +++ b/tests/playwright-test/only-changed.spec.ts @@ -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'); -}); \ No newline at end of file +}); + +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'); +});