Skip to content

Commit

Permalink
add some basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaplejic committed Sep 2, 2018
1 parent 2c62ba4 commit ec740c6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 8 deletions.
9 changes: 9 additions & 0 deletions fixture/crate1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "awesome_project"
version = "0.1.0"
authors = ["Nikola Plejic <[email protected]>"]

[dependencies]
toml = "0.4"
serde = "1.0"
serde_derive = "1.0"
11 changes: 11 additions & 0 deletions fixture/crate2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "shr"
version = "0.1.0"
authors = ["Nikola Plejic <[email protected]>"]

[dependencies]
toml = "0.3"
serde = "1.0"
serde_derive = "1.0"
unknown_project = "0.1.0"
internal_project = { path = "../internal_project" }
8 changes: 8 additions & 0 deletions fixture/workspace.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]

members = [
"awesome_project",
"shr",
"core_stuff",
"even_more_core_stuff",
]
69 changes: 61 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ fn load_toml(filename: &str) -> Result<String, std::io::Error> {
Ok(toml_str.to_string())
}

fn dependency_map(workspace: &Workspace) -> DependencyMap {
fn dependency_map(cargo_files: &Vec<String>) -> DependencyMap {
let mut dm = DependencyMap::new();

for member in &workspace.members {
let crate_str = load_toml(&format!("{}/Cargo.toml", member))
for member in cargo_files {
let crate_str = load_toml(&member)
.expect("No Cargo.toml file found.");

let member_toml : CargoCrate = match toml::from_str(&crate_str) {
Expand Down Expand Up @@ -84,7 +84,13 @@ fn dependency_map(workspace: &Workspace) -> DependencyMap {
return dm;
}

fn detect_dupes(vers: &Vec<DependencyEntry>) -> Vec<String> {
fn detect_dupes(dm: DependencyMap) -> DependencyMap {
dm.into_iter()
.filter(|(_, v)| { v.len() > 1 })
.collect()
}

fn normalize_deps(vers: &Vec<DependencyEntry>) -> Vec<String> {
let mut version_nos : Vec<String> = vers.iter()
.map(|ref de| de.version.to_string())
.collect();
Expand All @@ -104,13 +110,16 @@ fn main() -> std::io::Result<()> {
};
let workspace = tl_workspace.workspace;

let dm = dependency_map(&workspace);
let members = workspace.members
.into_iter()
.map(|m| format!("{}/Cargo.toml", m))
.collect();

let repeating_deps : DependencyMap =
dm.into_iter().filter(|(_, v)| { v.len() > 1 }).collect();
let dm = dependency_map(&members);
let repeating_deps = detect_dupes(dm);

for (dep, vers) in &repeating_deps {
let version_nos = detect_dupes(&vers);
let version_nos = normalize_deps(&vers);

if version_nos.len() > 1 {
println!("Found duplicate versions for dependency {}", dep);
Expand All @@ -125,3 +134,47 @@ fn main() -> std::io::Result<()> {

Ok(())
}

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

#[test]
fn test_workspace_parsing() {
let workspace_str = load_toml("fixture/workspace.toml").unwrap();
let tl_workspace : CargoWorkspace = toml::from_str(&workspace_str).unwrap();

assert_eq!(4, tl_workspace.workspace.members.len());
assert_eq!("awesome_project", tl_workspace.workspace.members.first().unwrap());
}

#[test]
fn test_dependency_map() {
let tomls = vec![
"fixture/crate1.toml".to_string(),
"fixture/crate2.toml".to_string(),
];
let dm = dependency_map(&tomls);

let serde = dm.get("serde").unwrap();
assert_eq!(2, serde.len());

let internal_project = dm.get("internal_project").unwrap();
assert_eq!(1, internal_project.len());
}

#[test]
fn test_dupe_detection() {
let tomls = vec![
"fixture/crate1.toml".to_string(),
"fixture/crate2.toml".to_string(),
];
let dm = dependency_map(&tomls);

let repeating_deps = detect_dupes(dm);

assert!(repeating_deps.contains_key("serde"));
assert!(repeating_deps.contains_key("toml"));
assert!(!repeating_deps.contains_key("unknown_project"));
}
}

0 comments on commit ec740c6

Please sign in to comment.