Skip to content

Commit

Permalink
Write template names into .gitignore files
Browse files Browse the repository at this point in the history
The names of the templates that are used to generate a .gitignore file
are now being written into the file as well. Before pasting the contents
of each template, the template's name is written into the file as a
comment. This makes it possible to see which lines were copied from
which template.
  • Loading branch information
jdno committed Aug 7, 2020
1 parent f2b24c6 commit 7a27d9d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and [Rust](https://github.com/rust-lang/rfcs/blob/master/text/1105-api-evolution

## [Unreleased]

### Added

- Add template names as section headings to `.gitignore` file

## [2.0.1] - 2020-05-10

### Fixed
Expand Down
30 changes: 21 additions & 9 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ impl Builder {
let destination = temp_dir().join(self.file_name());
let mut file = File::create(&destination)?;

for template in &self.templates {
let template_path = self.repository.path().join(template.file_name());
let content = read_to_string(template_path)?;

file.write_all(&content.as_bytes())?;
for i in 0..self.templates.len() {
if let Some(template) = self.templates.get(i) {
let template_path = self.repository.path().join(template.file_name());
let content = read_to_string(template_path)?;

file.write_all(format!("### {}\n", template.file_name()).as_bytes())?;
file.write_all(&content.as_bytes())?;

if i < self.templates.len() - 1 {
file.write_all(b"\n")?;
}
}
}

file.sync_all()?;
Expand Down Expand Up @@ -89,11 +96,13 @@ mod tests {
let builder = Builder::new(repository, &query);
let path = builder.build().unwrap();

let mut expected = String::new();
expected.push_str("### apples.gitignore\n");
expected.push_str(include_str!("../tests/files/repository/apples.gitignore"));

let content = read_to_string(path).unwrap();
assert_eq!(
include_str!("../tests/files/repository/apples.gitignore"),
content
);

assert_eq!(expected, content);
}

#[test]
Expand All @@ -106,7 +115,10 @@ mod tests {
let path = builder.build().unwrap();

let mut expected = String::new();
expected.push_str("### oranges.gitignore\n");
expected.push_str(include_str!("../tests/files/repository/oranges.gitignore"));

expected.push_str("\n### apples.gitignore\n");
expected.push_str(include_str!("../tests/files/repository/apples.gitignore"));

let content = read_to_string(path).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ fn build() {
command
.assert()
.success()
.stdout(predicate::str::contains("### apples.gitignore"));
.stdout(predicate::str::contains("### Apples.gitignore"));
}
2 changes: 0 additions & 2 deletions tests/files/repository/apples.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
### apples.gitignore

# This file ignores apples
/apples
2 changes: 0 additions & 2 deletions tests/files/repository/oranges.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
### oranges.gitignore

# This file ignores oranges
/oranges

0 comments on commit 7a27d9d

Please sign in to comment.