Skip to content
This repository has been archived by the owner on Sep 15, 2019. It is now read-only.

Commit

Permalink
feat: .calcver.yml parsing
Browse files Browse the repository at this point in the history
includes a few unit tests

resolves #12
  • Loading branch information
sanisoclem committed Feb 1, 2018
1 parent c8b646f commit 2489aa2
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 18 deletions.
12 changes: 12 additions & 0 deletions .calcver.yml
@@ -0,0 +1,12 @@

# -- uncomment to override defaults
#root: .
#repository_type: git
#prerelease_prefix: alpha
#tag_regex: \d+\.\d+\.\d+
#major_regex: "BREAKING CHANGE:"
#minor_regex: ^feat
#patch_regex: ^fix
cargo: .\cargo.toml
actions:
- .\bin\update-version.cmd #dummy update version for testing only
37 changes: 36 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Expand Up @@ -3,7 +3,9 @@ clap = '2.29.2'
git2 = '0.6'
glob = '0.2.11'
libcalcver = '0.2.0-beta.3'
serde = '1.0'
serde_derive = '1.0'
serde_yaml = '0.7'
toml = '0.4'

[package]
Expand All @@ -12,4 +14,4 @@ authors = [
'Joelle Ortiz <contact@joelleortiz.me>',
]
name = 'calcver'
version = '0.1.0-alpha.12'
version = '0.1.0-alpha.14'
137 changes: 125 additions & 12 deletions src/config.rs
@@ -1,24 +1,137 @@
use libcalcver;
use serde_yaml;
use repo;
use release;
use std::fs::{File};
use std::io::prelude::*;

pub struct CalcverConfig {
pub project: libcalcver::config::ProjectConfig,
pub repository: repo::RepositoryConfig,
pub release: Vec<release::ReleaseConfig>
}

// -- hard coded for now
pub fn from_config(_config: &str) -> CalcverConfig {
CalcverConfig {
project: libcalcver::config::ProjectConfig::from_defaults(),
repository: repo::RepositoryConfig {
scm_type: "git".to_string(),
path: ".".to_string()
},
release: vec![
release::ReleaseConfig::CargoToml("cargo.toml".to_string()),
release::ReleaseConfig::RunScript(".\\bin\\update-version.cmd".to_string())
]
#[derive(Debug, Deserialize)]
pub struct CalcverFileConfig {
pub repository_type: Option<String>,
pub root: Option<String>,
pub prerelease_prefix: Option<String>,
pub tag_regex: Option<String>,
pub major_regex: Option<String>,
pub minor_regex: Option<String>,
pub patch_regex: Option<String>,
pub cargo: Option<String>,
pub actions: Option<Vec<String>>
}

impl CalcverFileConfig {
pub fn convert(self) -> CalcverConfig {
let project_defaults = libcalcver::config::ProjectConfig::from_defaults();
let mut actions: Vec<release::ReleaseConfig> = vec![];
if let Some(a) = self.actions {
for item in a {
actions.push(release::ReleaseConfig::RunScript(item))
}
}
if let Some(c) = self.cargo {
actions.push(release::ReleaseConfig::CargoToml(c));
}
CalcverConfig {
project: libcalcver::config::ProjectConfig {
commit_template: project_defaults.commit_template, // -- not needed anymore
prerelease_prefix: self.prerelease_prefix.unwrap_or(project_defaults.prerelease_prefix),
tag_regex: self.tag_regex.unwrap_or(project_defaults.tag_regex),
major_regex: self.major_regex.unwrap_or(project_defaults.major_regex),
minor_regex: self.minor_regex.unwrap_or(project_defaults.minor_regex),
patch_regex: self.patch_regex.unwrap_or(project_defaults.patch_regex),
},
repository: repo::RepositoryConfig {
scm_type: self.repository_type.unwrap_or("git".to_owned()),
path: self.root.unwrap_or(".".to_owned())
},
release: actions
}
}
}


pub fn from_config(config_path: &str) -> CalcverConfig {
let mut buffer = String::new();
{
let mut f = File::open(&config_path).unwrap();
f.read_to_string(&mut buffer).unwrap();
}
parse_config(&buffer)
}

fn parse_config(config_yaml: &str) -> CalcverConfig {
let retval: CalcverFileConfig = serde_yaml::from_str(&config_yaml).unwrap();
retval.convert()
}



#[cfg(test)]
mod tests {
use super::*;
const NO_ACTIONS_CONFIG: &'static str = "repository_type: git";
const MIN_CONFIG: &'static str = "cargo: .\\cargo.toml";
const SCRIPTS_CONFIG: &'static str = r###"
actions:
- .\bin\update-version.cmd
- .\bin\publish.cmd
"###;
const OVERRIDE_CONFIG: &'static str = r###"cargo: .\\cargo.toml
root: ..\src
repository_type: hg
"###;

#[test]
fn default_repo_is_git(){
assert_eq!(parse_config(MIN_CONFIG).repository.scm_type,"git");
}
#[test]
fn default_repo_path_is_current(){
assert_eq!(parse_config(MIN_CONFIG).repository.path,".");
}

#[test]
fn repository_type_is_configurable(){
assert_eq!(parse_config(OVERRIDE_CONFIG).repository.scm_type,"hg");
}
#[test]
fn repo_path_is_configurable(){
assert_eq!(parse_config(OVERRIDE_CONFIG).repository.path,"..\\src");
}

#[test]
fn cargo_toml_is_added_if_present(){
let parsed = parse_config(MIN_CONFIG);
let cargo = parsed.release.into_iter().find(|i| match i {
&release::ReleaseConfig::CargoToml(ref t) => {
assert_eq!(t,".\\cargo.toml");
true
},
_=> false
});

assert!(cargo.is_some());
}

#[test]
fn cargo_toml_is_not_added_if_not_defined(){
let parsed = parse_config(SCRIPTS_CONFIG);
let has_cargo = parsed.release.into_iter().any(|i| match i {
release::ReleaseConfig::CargoToml(_) => {true},
_=> false
});

assert!(!has_cargo);
}

#[test]
fn no_actions_by_default(){
let parsed = parse_config(NO_ACTIONS_CONFIG);
assert!(parsed.release.len() == 0);
}
}
5 changes: 3 additions & 2 deletions src/main.rs
Expand Up @@ -3,8 +3,9 @@ extern crate clap;
extern crate libcalcver;
extern crate git2;
extern crate toml;
//#[macro_use]
//extern crate serde_derive;
extern crate serde_yaml;
#[macro_use]
extern crate serde_derive;

use clap::{App, Arg};
use libcalcver::{VersionBumpBehavior};
Expand Down
5 changes: 3 additions & 2 deletions src/release.rs
Expand Up @@ -27,7 +27,7 @@ impl ReleaseConfig {
let (old_version,newtoml) = update_toml(&buffer,version);
let mut file = OpenOptions::new().write(true).truncate(true).open(&full_path).unwrap();
file.write_all(newtoml.as_bytes()).unwrap();
println!("Patched '{}'\nOld version: {}\nNew version: {}",full_path.to_str().unwrap(),old_version,version);
println!("Patched '{}': {} => {}",full_path.to_str().unwrap(),old_version,version);
}
},
&ReleaseConfig::RunScript (ref script) => {
Expand Down Expand Up @@ -94,7 +94,8 @@ git2 = "0.6"
toml = "0.4"
serde_derive = "1.0"
"###;
let parsed = update_toml(toml, "1.2.3").parse::<Value>().unwrap();
let (_,updated) = update_toml(toml, "1.2.3");
let parsed = updated.parse::<Value>().unwrap();
let updated = parsed.as_table().unwrap()
.get("package").unwrap().as_table().unwrap()
.get("version").unwrap().as_str().unwrap();
Expand Down

0 comments on commit 2489aa2

Please sign in to comment.