diff --git a/src/vcs/git/ignore/writer.js b/src/vcs/git/ignore/writer.js index fb9a0773..0729e3b2 100644 --- a/src/vcs/git/ignore/writer.js +++ b/src/vcs/git/ignore/writer.js @@ -1,5 +1,5 @@ import {promises as fs} from 'node:fs'; export default function ({projectRoot, files = [], directories = []}) { - return fs.writeFile(`${projectRoot}/.gitignore`, `${directories.join('\n')}\n\n${files.join('\n')}`); + return fs.appendFile(`${projectRoot}/.gitignore`, `\n${directories.join('\n')}\n\n${files.join('\n')}`); } diff --git a/src/vcs/git/ignore/writer.test.js b/src/vcs/git/ignore/writer.test.js index 4c3d2ba3..1ffdf6eb 100644 --- a/src/vcs/git/ignore/writer.test.js +++ b/src/vcs/git/ignore/writer.test.js @@ -16,15 +16,15 @@ describe('gitignore writer', () => { await write({projectRoot, directories, files}); - expect(fs.writeFile).toHaveBeenCalledWith( + expect(fs.appendFile).toHaveBeenCalledWith( `${projectRoot}/.gitignore`, - `${directories.join('\n')}\n\n${files.join('\n')}` + `\n${directories.join('\n')}\n\n${files.join('\n')}` ); }); it('should not throw an error if directories nor files are provided', async () => { await write({projectRoot}); - expect(fs.writeFile).toHaveBeenCalledWith(`${projectRoot}/.gitignore`, '\n\n'); + expect(fs.appendFile).toHaveBeenCalledWith(`${projectRoot}/.gitignore`, '\n\n\n'); }); }); diff --git a/test/integration/features/git.feature b/test/integration/features/git.feature index 2d3f6222..9486076f 100644 --- a/test/integration/features/git.feature +++ b/test/integration/features/git.feature @@ -7,9 +7,11 @@ Feature: Git Repository Scenario: to be versioned Given the project should be versioned in git + And a language scaffolder is chosen When the project is scaffolded Then the directory is initialized as a git repository And the base git files should be present + And the ignores are defined in the gitignore Scenario: to be versioned and hosted Given the project should be versioned in git @@ -19,14 +21,16 @@ Feature: Git Repository Scenario: already versioned Given the project root is already initialized as a git repository + And a language scaffolder is chosen When the project is scaffolded Then the base git files should be present - And the gitignore file is unchanged + And the additional ignores are added to the gitignore @wip Scenario: already versioned without an existing gitignore Given the project root is already initialized as a git repository + And a language scaffolder is chosen But there is no preexisting gitignore When the project is scaffolded Then the base git files should be present - And the gitignore file is unchanged + And the gitignore file is added diff --git a/test/integration/features/step_definitions/common-steps.js b/test/integration/features/step_definitions/common-steps.js index b544871b..af7477f1 100644 --- a/test/integration/features/step_definitions/common-steps.js +++ b/test/integration/features/step_definitions/common-steps.js @@ -89,13 +89,14 @@ When(/^the project is scaffolded$/, async function () { }); When('the project is lifted', async function () { + this.existingVcsIgnoredFiles = any.listOf(any.word); + this.existingVcsIgnoredDirectories = any.listOf(any.word); + await Promise.all([ fs.writeFile(`${process.cwd()}/README.md`, this.existingReadmeContent || ''), fs.writeFile( `${process.cwd()}/.gitignore`, - (this.existingVcsIgnoredFiles && this.existingVcsIgnoredDirectories) - ? `${this.existingVcsIgnoredDirectories.join('\n')}\n\n${this.existingVcsIgnoredFiles.join('\n')}` - : '' + `${this.existingVcsIgnoredDirectories.join('\n')}\n\n${this.existingVcsIgnoredFiles.join('\n')}` ) ]); diff --git a/test/integration/features/step_definitions/language-steps.js b/test/integration/features/step_definitions/language-steps.js index bb961fbe..20cb7621 100644 --- a/test/integration/features/step_definitions/language-steps.js +++ b/test/integration/features/step_definitions/language-steps.js @@ -20,6 +20,8 @@ function generateBadgeWithNoLink() { Given('a language scaffolder is chosen', async function () { this.setAnswerFor(questionNames.PROJECT_LANGUAGE, any.word()); + this.vcsIgnoreDirectories = any.listOf(any.word, {min: 1}); + this.vcsIgnoreFiles = any.listOf(any.word, {min: 1}); this.languageScaffolderResults = { badges: { @@ -35,6 +37,10 @@ Given('a language scaffolder is chosen', async function () { [any.word()]: generateFullBadge(), [any.word()]: generateBadgeWithNoLink() } + }, + vcsIgnore: { + directories: this.vcsIgnoreDirectories, + files: this.vcsIgnoreFiles } }; }); diff --git a/test/integration/features/step_definitions/vcs/git-steps.js b/test/integration/features/step_definitions/vcs/git-steps.js index 9647a686..d3b2ca91 100644 --- a/test/integration/features/step_definitions/vcs/git-steps.js +++ b/test/integration/features/step_definitions/vcs/git-steps.js @@ -84,10 +84,24 @@ Then(/^the base git files should not be present$/, async function () { assert.isFalse(await fileExists(`${process.cwd()}/.gitignore`)); }); +Then('the ignores are defined in the gitignore', async function () { + const gitIgnoreContent = await fs.readFile(`${process.cwd()}/.gitignore`, 'utf-8'); + + assert.include(gitIgnoreContent, `${this.vcsIgnoreDirectories.join('\n')}\n\n${this.vcsIgnoreFiles.join('\n')}`); +}); + Then('the additional ignores are added to the gitignore', async function () { const gitIgnoreContent = await fs.readFile(`${process.cwd()}/.gitignore`, 'utf-8'); - assert.equal(gitIgnoreContent, `${this.vcsIgnoreDirectories.join('\n')}\n\n${this.vcsIgnoreFiles.join('\n')}`); + assert.equal( + gitIgnoreContent, + `${this.existingVcsIgnoredDirectories.join('\n')} + +${this.existingVcsIgnoredFiles.join('\n')} +${this.vcsIgnoreDirectories.join('\n')} + +${this.vcsIgnoreFiles.join('\n')}` + ); }); Then('the gitignore file is unchanged', async function () { @@ -96,3 +110,10 @@ Then('the gitignore file is unchanged', async function () { `${this.existingVcsIgnoredDirectories.join('\n')}\n\n${this.existingVcsIgnoredFiles.join('\n')}` ); }); + +Then('the gitignore file is added', async function () { + assert.equal( + await fs.readFile(`${process.cwd()}/.gitignore`, 'utf-8'), + `${this.vcsIgnoreDirectories.join('\n')}\n\n${this.vcsIgnoreFiles.join('\n')}` + ); +});