Skip to content

Commit

Permalink
chore: test
Browse files Browse the repository at this point in the history
  • Loading branch information
nadin-Starkware committed Aug 27, 2024
1 parent 344d014 commit 51ab767
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
44 changes: 21 additions & 23 deletions workspace_tests/toml_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::sync::LazyLock;

use serde::{Deserialize, Serialize};

const EMPTY_STRING_SLICE: [String; 0] = [];

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub(crate) enum DependencyValue {
Expand Down Expand Up @@ -90,34 +92,30 @@ impl CargoToml {

impl CrateCargoToml {
pub(crate) fn has_dependencies(&self) -> bool {
self.dependencies.is_some() || self.dev_dependencies.is_some()
self.all_dependencies().len > 0
}

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())
pub(crate) fn all_dependencies(
&self,
) -> impl Iterator<Item = (&String, &DependencyValue)> + '_ {
match (self.dependencies.as_ref(), self.dev_dependencies.as_ref()) {
(None, None) => std::iter::empty(), // Use an empty iterator
(Some(deps), None) => deps.iter(), // Use deps iterator
(None, Some(dev_deps)) => dev_deps.iter(), // Use dev_deps iterator
(Some(deps), Some(dev_deps)) => deps.iter().chain(dev_deps.iter()), // Chain both iterators
}
}

pub(crate) fn path_dependencies(&self) -> impl Iterator<Item = LocalCrate> + '_ {
if let Some(dependencies) = &self.dependencies {
dependencies.iter().filter_map(|(_name, value)| {
if let DependencyValue::Object { path: Some(path), version } = value {
Some(LocalCrate { path: path.to_string(), version: version.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))
}
}
12 changes: 6 additions & 6 deletions workspace_tests/version_integrity_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::toml_utils::{LocalCrate, ROOT_TOML};
#[test]
fn test_path_dependencies_are_members() {
let non_member_path_crates: Vec<_> = ROOT_TOML
.workspace_path_dependencies()
.path_dependencies()
.filter(|LocalCrate { path, .. }| !ROOT_TOML.members().contains(path))
.collect();
assert!(
Expand All @@ -17,7 +17,7 @@ fn test_path_dependencies_are_members() {
fn test_version_alignment() {
let workspace_version = ROOT_TOML.workspace_version();
let crates_with_incorrect_version: Vec<_> = ROOT_TOML
.workspace_path_dependencies()
.path_dependencies()
.filter(|LocalCrate { version, .. }| version != workspace_version)
.collect();
assert!(
Expand All @@ -35,9 +35,9 @@ fn validate_no_path_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:?}."
);
}
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 51ab767

Please sign in to comment.