Skip to content

Commit

Permalink
feat(cli): bump --pre flag to set the pre-release version
Browse files Browse the repository at this point in the history
  • Loading branch information
mersinvald authored and oknozor committed Oct 5, 2020
1 parent f97a6f3 commit 05a487a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/bin/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ fn main() -> Result<()> {
.long("minor")
.required_unless_one(&["version", "auto", "patch", "major"]),
)
.arg(
Arg::with_name("pre")
.help("Set the pre-release version")
.long("pre")
.takes_value(true),
)
.display_order(6);

let init_subcommand = SubCommand::with_name(INIT)
Expand Down Expand Up @@ -186,8 +192,10 @@ fn main() -> Result<()> {
unreachable!()
};

let pre = subcommand.value_of("pre");

// TODO mode to cli
cocogitto.create_version(increment, WriterMode::Prepend)?
cocogitto.create_version(increment, WriterMode::Prepend, pre)?
}
VERIFY => {
let subcommand = matches.subcommand_matches(VERIFY).unwrap();
Expand Down
15 changes: 12 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::error::ErrorKind::Semver;
use crate::filter::CommitFilters;
use crate::repository::Repository;
use crate::settings::Settings;
use crate::version::VersionIncrement;
use crate::version::{parse_pre_release, VersionIncrement};
use anyhow::Result;
use chrono::Utc;
use colored::*;
Expand Down Expand Up @@ -323,7 +323,12 @@ impl CocoGitto {
Ok(())
}

pub fn create_version(&self, increment: VersionIncrement, mode: WriterMode) -> Result<()> {
pub fn create_version(
&self,
increment: VersionIncrement,
mode: WriterMode,
pre_release: Option<&str>,
) -> Result<()> {
let statuses = self.repository.get_statuses()?;

// Fail if repo contains un-staged or un-committed changes
Expand All @@ -342,7 +347,7 @@ impl CocoGitto {

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

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

Expand All @@ -360,6 +365,10 @@ impl CocoGitto {
}));
};

if let Some(pre_release) = pre_release {
next_version.pre = parse_pre_release(pre_release)?;
}

let version_str = next_version.to_string();

let origin = if current_tag.as_str() == "0.0.0" {
Expand Down
62 changes: 59 additions & 3 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::repository::Repository;
use anyhow::Result;
use colored::*;
use git2::Commit as Git2Commit;
use semver::Version;
use semver::{Identifier, Version};

pub enum VersionIncrement {
Major,
Expand Down Expand Up @@ -104,11 +104,36 @@ impl VersionIncrement {
}
}

pub fn parse_pre_release(string: &str) -> Result<Vec<Identifier>> {
if !string
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-')
{
return Err(anyhow!("Pre-release string must be a dot-separated list of identifiers comprised of ASCII alphanumerics and hyphens [0-9A-Za-z-]"));
}

if string.starts_with('.') || string.contains("..") || string.ends_with('.') {
return Err(anyhow!(
"Dot-separated identifiers in the pre-release string must not be empty"
));
}

let idents = string
.split('.')
.map(|s| match s.parse::<u64>() {
Ok(n) => Identifier::Numeric(n),
Err(_) => Identifier::AlphaNumeric(s.to_string()),
})
.collect();

Ok(idents)
}

#[cfg(test)]
mod test {
use crate::version::VersionIncrement;
use crate::version::{parse_pre_release, VersionIncrement};
use anyhow::Result;
use semver::Version;
use semver::{Identifier, Version};

// Auto version tests resides in test/ dir since it rely on git log
// To generate the version
Expand All @@ -133,4 +158,35 @@ mod test {
assert_eq!(version, Version::new(1, 0, 1));
Ok(())
}

#[test]
fn parse_pre_release_valid() -> Result<()> {
let idents = parse_pre_release("alpha.0-dev.1")?;
assert_eq!(
&idents,
&[
Identifier::AlphaNumeric("alpha".into()),
Identifier::AlphaNumeric("0-dev".into()),
Identifier::Numeric(1),
]
);
Ok(())
}

#[test]
fn parse_pre_release_non_ascii() {
assert!(parse_pre_release("РАСТ").is_err());
}

#[test]
fn parse_pre_release_illegal_ascii() {
assert!(parse_pre_release("alpha$5").is_err());
}

#[test]
fn parse_pre_release_empty_ident() {
assert!(parse_pre_release(".alpha.5").is_err());
assert!(parse_pre_release("alpha..5").is_err());
assert!(parse_pre_release("alpha.5.").is_err());
}
}
21 changes: 21 additions & 0 deletions tests/cog_bump_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,24 @@ fn patch_bump() -> Result<()> {

Ok(std::env::set_current_dir(current_dir)?)
}

#[test]
#[cfg(not(tarpaulin))]
fn pre_release_bump() -> Result<()> {
let current_dir = std::env::current_dir()?;
let mut command = Command::cargo_bin("cog")?;
command.arg("bump").arg("--major").arg("--pre").arg("alpha");

let temp_dir = TempDir::default();
std::env::set_current_dir(&temp_dir)?;
helper::git_init(".")?;
helper::git_commit("chore: init")?;
helper::git_tag("1.0.0")?;
helper::git_commit("feat: feature")?;

command.assert().success();
assert!(temp_dir.join("CHANGELOG.md").exists());
helper::assert_tag("2.0.0-alpha")?;

Ok(std::env::set_current_dir(current_dir)?)
}

0 comments on commit 05a487a

Please sign in to comment.