Skip to content

Commit

Permalink
chore: bump semver to v1
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Nov 30, 2021
1 parent 3922623 commit 3d8db7d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 83 deletions.
16 changes: 2 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ itertools = "^0"
serde_derive = "^1"
serde = "^1"
tempfile = "^3"
semver = "^0"
semver = "^1"
shell-words = "^1"
which = "^4"
lazy_static = "^1"
Expand Down
70 changes: 9 additions & 61 deletions src/conventional/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Result;
use colored::*;
use conventional_commit_parser::commit::CommitType;
use git2::Commit as Git2Commit;
use semver::{Identifier, Version};
use semver::Version;

pub enum VersionIncrement {
Major,
Expand All @@ -23,17 +23,17 @@ impl VersionIncrement {
VersionIncrement::Auto => self.get_auto_version(current_version),
VersionIncrement::Major => {
let mut next = current_version.clone();
next.increment_major();
next.major += 1;
Ok(next)
}
VersionIncrement::Patch => {
let mut next = current_version.clone();
next.increment_patch();
next.patch += 1;
Ok(next)
}
VersionIncrement::Minor => {
let mut next = current_version.clone();
next.increment_minor();
next.minor += 1;
Ok(next)
}
}
Expand Down Expand Up @@ -87,11 +87,11 @@ impl VersionIncrement {

let mut next_version = current_version.clone();
if is_major_bump() {
next_version.increment_major();
next_version.major += 1;
} else if is_minor_bump() {
next_version.increment_minor();
next_version.minor += 1;
} else if is_patch_bump() {
next_version.increment_patch();
next_version.patch += 1;
} else {
bail!("No commit found to bump current version");
}
Expand Down Expand Up @@ -134,38 +134,14 @@ impl VersionIncrement {
}
}

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

ensure!(
!string.starts_with('.') && !string.contains("..") && !string.ends_with('.'),
"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::conventional::commit::Commit;
use crate::conventional::version::{parse_pre_release, VersionIncrement};
use crate::conventional::version::VersionIncrement;
use anyhow::Result;
use chrono::Utc;
use conventional_commit_parser::commit::{CommitType, ConventionalCommit};
use semver::{Identifier, Version};
use semver::Version;
use speculoos::prelude::*;

// Auto version tests resides in test/ dir since it rely on git log
Expand All @@ -192,17 +168,6 @@ mod test {
Ok(())
}

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

#[test]
fn should_get_next_auto_version_patch() {
let patch = Commit {
Expand Down Expand Up @@ -346,21 +311,4 @@ mod test {
.is_ok()
.is_equal_to(Version::new(1, 1, 0))
}

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

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

#[test]
fn parse_pre_release_empty_ident() {
assert_that!(parse_pre_release(".alpha.5")).is_err();
assert_that!(parse_pre_release("alpha..5")).is_err();
assert_that!(parse_pre_release("alpha.5.")).is_err();
}
}
6 changes: 3 additions & 3 deletions src/hook/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl HookExpr {
fn increment_major(version: Version, amt: u32) -> Version {
let mut version = version;
for _ in 0..amt {
version.increment_major()
version.major += 1
}

version
Expand All @@ -102,7 +102,7 @@ impl HookExpr {
fn increment_patch(version: Version, amt: u32) -> Version {
let mut version = version;
for _ in 0..amt {
version.increment_patch()
version.patch += 1
}

version
Expand All @@ -111,7 +111,7 @@ impl HookExpr {
fn increment_minor(version: Version, amt: u32) -> Version {
let mut version = version;
for _ in 0..amt {
version.increment_minor()
version.minor += 1
}

version
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ use colored::*;
use conventional::changelog::{Changelog, ChangelogWriter};
use conventional::commit::Commit;
use conventional::commit::CommitConfig;
use conventional::version::{parse_pre_release, VersionIncrement};
use conventional::version::VersionIncrement;
use conventional_commit_parser::commit::{CommitType, ConventionalCommit};
use conventional_commit_parser::parse_footers;
use git::repository::Repository;
use git2::{Oid, RebaseOptions};
use hook::Hook;
use itertools::Itertools;
use log::filter::CommitFilters;
use semver::Version;
use semver::{Prerelease, Version};
use settings::AuthorSetting;
use std::collections::HashMap;
use std::fmt::Display;
Expand Down Expand Up @@ -434,7 +434,7 @@ impl CocoGitto {
};

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

let version_str = next_version.to_string();
Expand Down
1 change: 0 additions & 1 deletion tests/lib_tests/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use cocogitto::CocoGitto;
use speculoos::prelude::*;

use crate::helpers::*;
use itertools::Itertools;

#[test]
fn get_unfiltered_logs() -> Result<()> {
Expand Down

0 comments on commit 3d8db7d

Please sign in to comment.