1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

#[macro_use(quick_error)]
extern crate quick_error;
extern crate regex;
extern crate semver;

pub mod config;
pub mod error;
pub mod repository;
mod version;


#[derive(Debug)]
#[derive(PartialEq)]
pub enum VersionBumpBehavior {
    None,
    Auto,
    Major,
    Minor,
    Patch,
}

pub fn get_version(config:  &config::ProjectConfig,repo: &repository::Repository,  bump_behavior: VersionBumpBehavior, release: bool) -> Result<String,error::CalcverError> {
    let commits = repo.get_commits_since_last_tag();
    let last_tag = repo.get_last_tag();

    version::get_next_version(&config, bump_behavior, &commits, last_tag,release)
}

#[cfg(test)]
mod tests {
    use super::*;

    struct DummyRepo {
        pub  commits: Vec<String>,
        pub last_tag : Option<String>
    }
    impl repository::Repository for DummyRepo {
        fn get_last_tag(&self) -> Option<&str> {
            match self.last_tag {
                Some(ref tag) => Some(tag),
                None=> None
            }
        }
        fn get_commits_since_last_tag(&self) -> &Vec<String> {
            &self.commits
        }
    }

    #[test]
    fn smoke_test(){
        let repo = DummyRepo { 
            commits: vec!["feat: smoke test".to_string()],
            last_tag: Some("v1.2.3".to_string())
        };
        let config =  config::ProjectConfig::from_defaults();

        assert_eq!("1.3.0-alpha.1", get_version(&config,&repo,VersionBumpBehavior::Auto,false).unwrap());
        assert_eq!("1.3.0", get_version(&config,&repo,VersionBumpBehavior::Auto,true).unwrap())
    }

    #[test]
    fn smoke_test_release(){
        let repo = DummyRepo { 
            commits: vec![],
            last_tag: Some("v1.2.3".to_string())
        };
        let config =  config::ProjectConfig::from_defaults();
        assert_eq!("1.2.3", get_version(&config,&repo,VersionBumpBehavior::Auto,true).unwrap())
    }

    #[test]
    fn error_if_no_info(){
        let repo = DummyRepo { 
            commits: vec![],
            last_tag: None
        };
        let config =  config::ProjectConfig::from_defaults();
        assert!(get_version(&config,&repo,VersionBumpBehavior::Auto,true).is_err())
    }

    #[test]
    fn error_if_invalid_regex(){
        let repo = DummyRepo { 
            commits: vec!["feat: smoke test".to_string()],
            last_tag: Some("v1.2.3".to_string())
        };
        let config =  config::ProjectConfig {
            commit_template: String::from(config::COMMIT_TEMPLATE_DEFAULT),
            prerelease_prefix: String::from(config::PRERELEASE_PREFIX_DEFAULT),
            tag_regex: String::from(config::TAG_REGEX_DEFAULT),
            major_regex: String::from(config::MAJOR_REGEX_DEFAULT),
            minor_regex: String::from(config::MINOR_REGEX_DEFAULT),
            patch_regex: String::from("invalidregex[\\t"),
        };
        assert!(get_version(&config,&repo,VersionBumpBehavior::Auto,true).is_err())
    }
}