diff --git a/src/generator.ts b/src/generator.ts index 16ee959..d4227e9 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -271,12 +271,18 @@ export class Generator { let gitIgnore = ''; if (fs.existsSync(gitIgnorePath)) gitIgnore = fs.readFileSync(gitIgnorePath, 'utf-8').trimEnd() + '\n'; - if (!gitIgnore.includes('node_modules')) - gitIgnore += 'node_modules/\n'; - gitIgnore += '/test-results/\n'; - gitIgnore += '/playwright-report/\n'; - gitIgnore += '/blob-report/\n'; - gitIgnore += '/playwright/.cache/\n'; + const valuesToAdd = { + 'node_modules/': /^node_modules\/?/m, + '/test-results/': /^\/?test-results\/?$/m, + '/playwright-report/': /^\/playwright-report\/?$/m, + '/blob-report/': /^\/blob-report\/?$/m, + '/playwright/.cache/': /^\/playwright\/\.cache\/?$/m + }; + Object.entries(valuesToAdd).forEach(([value, regex]) => { + if (!gitIgnore.match(regex)) { + gitIgnore += `${value}\n`; + } + }); fs.writeFileSync(gitIgnorePath, gitIgnore); } diff --git a/tests/integration.spec.ts b/tests/integration.spec.ts index 81b4013..a10c1f7 100644 --- a/tests/integration.spec.ts +++ b/tests/integration.spec.ts @@ -18,6 +18,14 @@ import path from 'path'; import fs from 'fs'; import childProcess from 'child_process'; +const validGitignore = [ + 'node_modules/', + '/test-results/', + '/playwright-report/', + '/blob-report/', + '/playwright/.cache/', +].join('\n'); + test('should generate a project in the current directory', async ({ run, dir, packageManager }) => { test.slow(); const { stdout } = await run([], { installGitHubActions: true, testDir: 'tests', language: 'TypeScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: true }); @@ -29,6 +37,7 @@ test('should generate a project in the current directory', async ({ run, dir, pa expect(playwrightConfigContent).toContain('tests'); expect(fs.existsSync(path.join(dir, '.github/workflows/playwright.yml'))).toBeTruthy(); expect(fs.existsSync(path.join(dir, '.gitignore'))).toBeTruthy(); + expect(fs.readFileSync(path.join(dir, '.gitignore'), { encoding: 'utf8' }).trim()).toBe(validGitignore); if (packageManager === 'npm') { expect(stdout).toContain('Initializing NPM project (npm init -y)…'); expect(stdout).toContain('Installing Playwright Test (npm install --save-dev @playwright/test)…'); @@ -102,6 +111,13 @@ test('should generate in the root of pnpm workspace', async ({ run, packageManag expect(fs.existsSync(path.join(dir, 'playwright.config.ts'))).toBeTruthy(); }); +test('should not duplicate gitignore entries', async ({ run, dir }) => { + fs.writeFileSync(path.join(dir, '.gitignore'), validGitignore); + + await run([], { installGitHubActions: false, testDir: 'tests', language: 'TypeScript', installPlaywrightDependencies: false, installPlaywrightBrowsers: false }); + expect(fs.readFileSync(path.join(dir, '.gitignore'), { encoding: 'utf8' }).trim()).toBe(validGitignore); +}) + test('should install with "npm ci" in GHA when using npm with package-lock enabled', async ({ dir, run, packageManager }) => { test.skip(packageManager !== 'npm');