Skip to content

Commit

Permalink
chore(ci): enforce crate directory name match + take version from wor…
Browse files Browse the repository at this point in the history
…kspace (#2014)

Signed-off-by: Dori Medini <[email protected]>
  • Loading branch information
dorimedini-starkware authored Nov 17, 2024
1 parent 3c69ac8 commit f8e9dc1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/starknet_mempool_p2p/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "starknet_mempool_p2p"
version = "0.0.0"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_mempool_p2p_types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "starknet_mempool_p2p_types"
version = "0.0.0"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
Expand Down
1 change: 1 addition & 0 deletions workspace_tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(test)]

pub mod lints_test;
pub mod package_integrity_test;
pub mod toml_utils;
pub mod version_integrity_test;
26 changes: 26 additions & 0 deletions workspace_tests/package_integrity_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::path::PathBuf;

use crate::toml_utils::{PackageEntryValue, ROOT_TOML};

#[test]
fn test_package_names_match_directory() {
let mismatched_packages: Vec<_> = ROOT_TOML
.member_cargo_tomls()
.into_iter()
.filter_map(|(path_str, toml)| {
let path = PathBuf::from(&path_str);
let directory_name = path.file_name()?.to_str()?;
match toml.package.get("name") {
Some(PackageEntryValue::String(package_name)) if package_name == directory_name => {
None
}
_ => Some(path_str),
}
})
.collect();
assert!(
mismatched_packages.is_empty(),
"The following crates have package names that do not match their directory names, or are \
missing a name field: {mismatched_packages:?}."
);
}
9 changes: 9 additions & 0 deletions workspace_tests/toml_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ pub(crate) struct CargoToml {
workspace: WorkspaceFields,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub(crate) enum PackageEntryValue {
String(String),
Object { workspace: bool },
Other(toml::Value),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct CrateCargoToml {
pub(crate) package: HashMap<String, PackageEntryValue>,
dependencies: Option<HashMap<String, DependencyValue>>,
#[serde(rename = "dev-dependencies")]
dev_dependencies: Option<HashMap<String, DependencyValue>>,
Expand Down
34 changes: 33 additions & 1 deletion workspace_tests/version_integrity_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::toml_utils::{DependencyValue, LocalCrate, ROOT_TOML};
use crate::toml_utils::{DependencyValue, LocalCrate, PackageEntryValue, ROOT_TOML};

#[test]
fn test_path_dependencies_are_members() {
Expand Down Expand Up @@ -27,6 +27,38 @@ fn test_version_alignment() {
);
}

#[test]
fn validate_crate_version_is_workspace() {
let crates_without_workspace_version: Vec<String> = ROOT_TOML
.member_cargo_tomls()
.into_iter()
.flat_map(|(member, toml)| match toml.package.get("version") {
// No `version` field.
None => Some(member),
Some(version) => match version {
// version = "x.y.z".
PackageEntryValue::String(_) => Some(member),
// version.workspace = (true | false).
PackageEntryValue::Object { workspace } => {
if *workspace {
None
} else {
Some(member)
}
}
// Unknown version object.
PackageEntryValue::Other(_) => Some(member),
},
})
.collect();

assert!(
crates_without_workspace_version.is_empty(),
"The following crates don't have `version.workspace = true` in the [package] section: \
{crates_without_workspace_version:?}."
);
}

#[test]
fn validate_no_path_dependencies() {
let all_path_deps_in_crate_tomls: Vec<String> =
Expand Down

0 comments on commit f8e9dc1

Please sign in to comment.