Skip to content

Commit

Permalink
fix(semver): add bump test and fix version auto bump
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Sep 27, 2020
1 parent 3b26be9 commit eeb917e
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl ChangelogWriter {
}

fn insert(&mut self) -> Result<()> {
let mut changelog_content = fs::read_to_string(&self.path)
.unwrap_or(Changelog::changelog_template());
let mut changelog_content =
fs::read_to_string(&self.path).unwrap_or_else(|_err| Changelog::changelog_template());

let separator_idx = match self.mode {
Append => changelog_content.rfind("- - -"),
Expand Down Expand Up @@ -90,7 +90,6 @@ impl Changelog {
out.push_str(&format!("\n### {}\n\n", metadata.changelog_title));
commits.iter().for_each(|commit| {
out.push_str(&commit.to_markdown(colored));
out.push('\n');
});
}
};
Expand All @@ -114,7 +113,8 @@ impl Changelog {
}

pub(crate) fn default_footer() -> String {
"\nThis changelog was generated by [cocogitto](https://github.com/oknozor/cocogitto).".to_string()
"\nThis changelog was generated by [cocogitto](https://github.com/oknozor/cocogitto)."
.to_string()
}

fn changelog_template() -> String {
Expand Down
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ impl CocoGitto {
pub fn create_version(&self, increment: VersionIncrement, mode: WriterMode) -> Result<()> {
let statuses = self.repository.get_statuses()?;

// Fail if repo contains un-staged or un-committed changes
if !statuses.is_empty() {
let statuses = statuses
.iter()
Expand All @@ -326,8 +327,9 @@ impl CocoGitto {

let current_version = Version::parse(&current_tag)?;

// Error on unstaged changes
let next_version = increment.bump(&current_version)?;
let next_version = increment
.bump(&current_version)
.map_err(|err| anyhow!("Cannot bump version : {}", err))?;

if next_version.le(&current_version) || next_version.eq(&current_version) {
let comparison = format!("{} <= {}", current_version, next_version).red();
Expand Down Expand Up @@ -362,7 +364,8 @@ impl CocoGitto {
mode,
};

writter.write()
writter
.write()
.map_err(|err| anyhow!("Unable to write CHANGELOG.md : {}", err))?;

self.repository.add_all()?;
Expand Down
8 changes: 4 additions & 4 deletions src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ impl Repository {
));
}

let head = self.get_head().unwrap();
let head = self.get_head_commit().unwrap();
self.0
.tag_lightweight(name, &head, false)
.tag_lightweight(name, &head.into_object(), false)
.map(|_| ())
.map_err(|err| {
let cause_key = "cause:".red();
Expand All @@ -172,8 +172,8 @@ impl Repository {
self.0.find_commit(to)?;

let mut revwalk = self.0.revwalk()?;
revwalk.push(to)?;
revwalk.push(from)?;

revwalk.push_range(&format!("{}..{}", from, to))?;

let mut commits: Vec<Git2Commit> = vec![];

Expand Down
4 changes: 2 additions & 2 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ impl VersionIncrement {
#[cfg(test)]
mod test {
use crate::version::VersionIncrement;
use semver::Version;
use anyhow::Result;
use semver::Version;

// Auto version tests resides in test/ dir since it rely on git log
// To generate the version
Expand All @@ -133,4 +133,4 @@ mod test {
assert_eq!(version, Version::new(1, 0, 1));
Ok(())
}
}
}
16 changes: 10 additions & 6 deletions tests/coco_commit_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use std::process::Command;
use assert_cmd::prelude::*;
use std::process::Command;
use temp_testdir::TempDir;

mod helper;
Expand All @@ -16,7 +16,8 @@ fn commit_ok() -> Result<()> {
std::fs::write(temp_dir.join("test_file"), "content")?;
helper::git_add()?;

command.arg("feat")
command
.arg("feat")
.arg("this is a commit message")
.arg("scope")
.arg("this is the body")
Expand All @@ -37,7 +38,8 @@ fn unstaged_changes_commit_err() -> Result<()> {
helper::git_init(".")?;
std::fs::write(temp_dir.join("test_file"), "content")?;

command.arg("feat")
command
.arg("feat")
.arg("this is a commit message")
.arg("scope")
.arg("this is the body")
Expand All @@ -61,7 +63,8 @@ fn untracked_changes_commit_ok() -> Result<()> {

std::fs::write(temp_dir.join("untracked"), "content")?;

command.arg("feat")
command
.arg("feat")
.arg("this is a commit message")
.arg("scope")
.arg("this is the body")
Expand All @@ -82,7 +85,8 @@ fn empty_commit_err() -> Result<()> {
std::env::set_current_dir(&temp_dir.to_path_buf())?;
helper::git_init(".")?;

command.arg("feat")
command
.arg("feat")
.arg("this is a commit message")
.arg("scope")
.arg("this is the body")
Expand All @@ -91,4 +95,4 @@ fn empty_commit_err() -> Result<()> {
command.assert().failure();

Ok(std::env::set_current_dir(current_dir)?)
}
}
14 changes: 7 additions & 7 deletions tests/cog_bump_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use assert_cmd::prelude::*;
use std::process::Command;
use temp_testdir::TempDir;
use assert_cmd::prelude::*;

mod helper;

Expand All @@ -11,7 +11,7 @@ fn auto_bump_from_start_ok() -> Result<()> {
let current_dir = std::env::current_dir()?;
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--auto");
let temp_dir = TempDir::default().permanent();
let temp_dir = TempDir::default();
std::env::set_current_dir(&temp_dir)?;
helper::git_init(".")?;
helper::git_commit("chore: init")?;
Expand All @@ -32,7 +32,7 @@ fn auto_bump_frowm_latest_tag() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--auto");

let temp_dir = TempDir::default().permanent();
let temp_dir = TempDir::default();
std::env::set_current_dir(&temp_dir)?;
helper::git_init(".")?;
helper::git_commit("chore: init")?;
Expand All @@ -57,7 +57,7 @@ fn minor_bump() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--minor");

let temp_dir = TempDir::default().permanent();
let temp_dir = TempDir::default();
std::env::set_current_dir(&temp_dir)?;
helper::git_init(".")?;
helper::git_commit("chore: init")?;
Expand All @@ -78,7 +78,7 @@ fn major_bump() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--major");

let temp_dir = TempDir::default().permanent();
let temp_dir = TempDir::default();
std::env::set_current_dir(&temp_dir)?;
helper::git_init(".")?;
helper::git_commit("chore: init")?;
Expand All @@ -99,7 +99,7 @@ fn patch_bump() -> Result<()> {
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--patch");

let temp_dir = TempDir::default().permanent();
let temp_dir = TempDir::default();
std::env::set_current_dir(&temp_dir)?;
helper::git_init(".")?;
helper::git_commit("chore: init")?;
Expand All @@ -111,4 +111,4 @@ fn patch_bump() -> Result<()> {
helper::assert_tag("1.0.1")?;

Ok(std::env::set_current_dir(current_dir)?)
}
}
2 changes: 1 addition & 1 deletion tests/cog_changelog_test.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// TODO
// TODO
2 changes: 1 addition & 1 deletion tests/cog_check_test.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// TODO
// TODO
2 changes: 1 addition & 1 deletion tests/cog_init_test_it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn init_empty_repo_in_target_dir() -> Result<()> {
command.arg("init").arg("test_repo");

let temp_dir = TempDir::default();
std::env::set_current_dir(temp_dir.as_ref())?;
std::env::set_current_dir(&temp_dir)?;

command.assert().success();
Ok(std::env::set_current_dir(current_dir)?)
Expand Down
2 changes: 1 addition & 1 deletion tests/cog_verify_test.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// TODO
// TODO
1 change: 0 additions & 1 deletion tests/commit_test.rs

This file was deleted.

7 changes: 2 additions & 5 deletions tests/helper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use std::process::{Command, Stdio};
use rand::Rng;
use std::process::{Command, Stdio};

// Why those are picked as dead code by rustc ?

Expand Down Expand Up @@ -28,7 +28,6 @@ pub fn git_add() -> Result<()> {
Ok(())
}


#[allow(dead_code)]
pub fn git_commit(message: &str) -> Result<()> {
let mut rng = rand::thread_rng();
Expand Down Expand Up @@ -67,9 +66,7 @@ pub fn git_tag(tag: &str) -> Result<()> {

#[allow(dead_code)]
pub fn assert_tag(tag: &str) -> Result<()> {
let out = Command::new("ls")
.arg(".git/refs/tags")
.output()?.stdout;
let out = Command::new("ls").arg(".git/refs/tags").output()?.stdout;

let out = String::from_utf8(out)?;
let tags: Vec<&str> = out.split('\n').collect();
Expand Down

0 comments on commit eeb917e

Please sign in to comment.