Skip to content

Commit

Permalink
feat(ci): add no path dependencies test
Browse files Browse the repository at this point in the history
  • Loading branch information
nadin-Starkware committed Aug 27, 2024
1 parent 29f90d3 commit 6419750
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
70 changes: 66 additions & 4 deletions workspace_tests/toml_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::sync::LazyLock;

use serde::{Deserialize, Serialize};
Expand All @@ -8,6 +10,7 @@ use serde::{Deserialize, Serialize};
pub(crate) enum DependencyValue {
String(String),
Object { version: String, path: Option<String> },
LocalCrateObject { path: Option<String> },
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -27,6 +30,13 @@ pub(crate) struct CargoToml {
workspace: WorkspaceFields,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct CrateCargoToml {
dependencies: Option<HashMap<String, DependencyValue>>,
#[serde(rename = "dev-dependencies")]
dev_dependencies: Option<HashMap<String, DependencyValue>>,
}

#[derive(Debug)]
pub(crate) struct LocalCrate {
pub(crate) path: String,
Expand All @@ -41,6 +51,14 @@ pub(crate) static ROOT_TOML: LazyLock<CargoToml> = LazyLock::new(|| {
});

impl CargoToml {
pub(crate) fn members(&self) -> &Vec<String> {
&self.workspace.members
}

pub(crate) fn workspace_version(&self) -> &str {
&self.workspace.package.version
}

pub(crate) fn path_dependencies(&self) -> impl Iterator<Item = LocalCrate> + '_ {
self.workspace.dependencies.iter().filter_map(|(_name, value)| {
if let DependencyValue::Object { path: Some(path), version } = value {
Expand All @@ -51,11 +69,55 @@ impl CargoToml {
})
}

pub(crate) fn members(&self) -> &Vec<String> {
&self.workspace.members
pub(crate) fn member_cargo_tomls(&self) -> Vec<CrateCargoToml> {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let crates_dir = format!("{}/../", manifest_dir);

self.members()
.iter()
.map(|member| {
let cargo_toml_path = Path::new(&crates_dir).join(member).join("Cargo.toml");

let cargo_toml_content = fs::read_to_string(&cargo_toml_path)
.expect(&format!("Failed to read {:?}", cargo_toml_path));

let cargo_toml: CrateCargoToml = toml::from_str(&cargo_toml_content).unwrap();
cargo_toml
})
.collect()
}
}

pub(crate) fn workspace_version(&self) -> &str {
&self.workspace.package.version
impl CrateCargoToml {
pub(crate) fn has_dependencies(&self) -> bool {
self.dependencies.is_some() || self.dev_dependencies.is_some()
}

pub(crate) fn path_dependencies(&self) -> Box<dyn Iterator<Item = String> + '_> {
let dependencies_iter = if let Some(dependencies) = &self.dependencies {
Box::new(dependencies.iter().filter_map(|(_name, value)| {
if let DependencyValue::LocalCrateObject { path: Some(path) } = value {
Some(path.to_string())
} else {
None
}
})) as Box<dyn Iterator<Item = String>>
} else {
Box::new(::std::iter::empty()) as Box<dyn Iterator<Item = String>>
};

let dev_dependencies_iter = if let Some(dev_dependencies) = &self.dev_dependencies {
Box::new(dev_dependencies.iter().filter_map(|(_name, value)| {
if let DependencyValue::LocalCrateObject { path: Some(path) } = value {
Some(path.to_string())
} else {
None
}
})) as Box<dyn Iterator<Item = String>>
} else {
Box::new(::std::iter::empty()) as Box<dyn Iterator<Item = String>>
};

Box::new(dependencies_iter.chain(dev_dependencies_iter))
}
}
15 changes: 15 additions & 0 deletions workspace_tests/version_integrity_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ fn test_version_alignment() {
'{workspace_version}': {crates_with_incorrect_version:?}."
);
}

#[test]
fn validate_no_path_dependencies() {
let mut all_path_deps_in_crate_tomls: Vec<String> = Vec::new();
for crate_cargo_toml in ROOT_TOML.member_cargo_tomls().iter() {
if crate_cargo_toml.has_dependencies() {
let crate_paths: Vec<String> = crate_cargo_toml.path_dependencies().collect();
all_path_deps_in_crate_tomls.extend(crate_paths);
}
assert!(
all_path_deps_in_crate_tomls.is_empty(),
"The following crates have path dependency {all_path_deps_in_crate_tomls:?}."
);
}
}

0 comments on commit 6419750

Please sign in to comment.