Skip to content

Commit

Permalink
fix(git-ignore): appended to the existing file if additional ignores …
Browse files Browse the repository at this point in the history
…are provided
  • Loading branch information
travi committed Dec 28, 2023
1 parent 6004f04 commit d006c32
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/vcs/git/ignore/writer.js
Original file line number Diff line number Diff line change
@@ -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')}`);
}
6 changes: 3 additions & 3 deletions src/vcs/git/ignore/writer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
8 changes: 6 additions & 2 deletions test/integration/features/git.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
7 changes: 4 additions & 3 deletions test/integration/features/step_definitions/common-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`
)
]);

Expand Down
6 changes: 6 additions & 0 deletions test/integration/features/step_definitions/language-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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
}
};
});
23 changes: 22 additions & 1 deletion test/integration/features/step_definitions/vcs/git-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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')}`
);
});

0 comments on commit d006c32

Please sign in to comment.