Skip to content

Commit

Permalink
feat: PrepareRelease will create a changelog file if it was missi…
Browse files Browse the repository at this point in the history
…ng. (#240)

Closes #237

Co-authored-by: Dylan Anthony <[email protected]>
  • Loading branch information
dbanty and dbanty authored Aug 15, 2022
1 parent 4128e59 commit 8b31d47
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/releases/conventional_commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ pub(crate) fn update_project_from_conventional_commits(
Ok(RunType::DryRun { state, stdout })
} else {
if let Some(changelog_path) = changelog_path {
let changelog_text = std::fs::read_to_string(&changelog_path)?;
let changelog_text = if changelog_path.exists() {
std::fs::read_to_string(&changelog_path)?
} else {
String::new()
};
let changelog = add_version_to_changelog(&changelog_text, &new_changes);
std::fs::write(&changelog_path, changelog)?;
}
Expand Down
9 changes: 5 additions & 4 deletions src/releases/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ impl TryFrom<PathBuf> for Changelog {
type Error = StepError;

fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
if !path.exists() {
return Err(StepError::FileNotFound(path));
}
let content = read_to_string(&path)?;
let content = if path.exists() {
read_to_string(&path)?
} else {
String::new()
};
Ok(Self { path, content })
}
}
Expand Down
49 changes: 49 additions & 0 deletions tests/prepare_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,55 @@ fn prepare_release_invalid_versioned_files(#[case] knope_toml: &str) {
.stderr_eq_path(source_path.join(&format!("{knope_toml}_INVALID_output.txt")));
}

/// Run a `PrepareRelease` where the CHANGELOG.md file is missing and verify it's created.
#[test]
fn prepare_release_creates_missing_changelog() {
// Arrange.
let temp_dir = tempfile::tempdir().unwrap();
let temp_path = temp_dir.path();
let source_path = Path::new("tests/prepare_release/package_selection");

init(temp_path);
commit(temp_path, "feat: Existing feature");
tag(temp_path, "v1.0.0");
commit(temp_path, "feat: New feature");

copy(
source_path.join("Cargo.toml_knope.toml"),
temp_path.join("knope.toml"),
)
.unwrap();
let file = "Cargo.toml";
copy(source_path.join(file), temp_path.join(file)).unwrap();

// Act.
let dry_run_assert = Command::new(cargo_bin!("knope"))
.arg("release")
.arg("--dry-run")
.current_dir(temp_dir.path())
.assert();
let actual_assert = Command::new(cargo_bin!("knope"))
.arg("release")
.current_dir(temp_dir.path())
.assert();

// Assert.
dry_run_assert
.success()
.stdout_eq_path(source_path.join("dry_run_output.txt"));
actual_assert
.success()
.stdout_eq_path(source_path.join("output.txt"));
assert_eq_path(
source_path.join("NEW_CHANGELOG.md"),
read_to_string(temp_path.join("CHANGELOG.md")).unwrap(),
);
assert_eq_path(
source_path.join("expected_Cargo.toml"),
read_to_string(temp_path.join("Cargo.toml")).unwrap(),
);
}

/// Run a `PrepareRelease` in a repo with multiple packages set to verify error message.
#[test]
fn test_prepare_release_multiple_packages() {
Expand Down
5 changes: 5 additions & 0 deletions tests/prepare_release/package_selection/NEW_CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 1.1.0

### Features

- New feature

0 comments on commit 8b31d47

Please sign in to comment.