From b721a5663c0fced2af9840ddef52b9e278d24c65 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Sat, 24 Aug 2024 20:28:18 +0300 Subject: [PATCH] chore(ci): split workspace tests into modules Signed-off-by: Dori Medini --- workspace_tests/Cargo.toml | 4 +- workspace_tests/main.rs | 4 ++ workspace_tests/toml_utils.rs | 61 ++++++++++++++++++++++ workspace_tests/version_integrity_test.rs | 62 +---------------------- 4 files changed, 69 insertions(+), 62 deletions(-) create mode 100644 workspace_tests/main.rs create mode 100644 workspace_tests/toml_utils.rs diff --git a/workspace_tests/Cargo.toml b/workspace_tests/Cargo.toml index 641b108586..31b23ffe40 100644 --- a/workspace_tests/Cargo.toml +++ b/workspace_tests/Cargo.toml @@ -11,5 +11,5 @@ serde = { workspace = true, features = ["derive"] } toml.workspace = true [[test]] -name = "version_integrity_test" -path = "version_integrity_test.rs" +name = "workspace_tests" +path = "main.rs" diff --git a/workspace_tests/main.rs b/workspace_tests/main.rs new file mode 100644 index 0000000000..9376af2c29 --- /dev/null +++ b/workspace_tests/main.rs @@ -0,0 +1,4 @@ +#![cfg(test)] + +pub mod toml_utils; +pub mod version_integrity_test; diff --git a/workspace_tests/toml_utils.rs b/workspace_tests/toml_utils.rs new file mode 100644 index 0000000000..396be022c7 --- /dev/null +++ b/workspace_tests/toml_utils.rs @@ -0,0 +1,61 @@ +use std::collections::HashMap; +use std::sync::LazyLock; + +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize)] +#[serde(untagged)] +pub(crate) enum DependencyValue { + String(String), + Object { version: String, path: Option }, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub(crate) struct Package { + version: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub(crate) struct WorkspaceFields { + package: Package, + members: Vec, + dependencies: HashMap, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub(crate) struct CargoToml { + workspace: WorkspaceFields, +} + +#[derive(Debug)] +pub(crate) struct LocalCrate { + pub(crate) path: String, + pub(crate) version: String, +} + +pub(crate) static ROOT_TOML: LazyLock = LazyLock::new(|| { + let root_toml: CargoToml = + toml::from_str(include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../Cargo.toml"))) + .unwrap(); + root_toml +}); + +impl CargoToml { + pub(crate) fn path_dependencies(&self) -> impl Iterator + '_ { + self.workspace.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 + } + }) + } + + pub(crate) fn members(&self) -> &Vec { + &self.workspace.members + } + + pub(crate) fn workspace_version(&self) -> &str { + &self.workspace.package.version + } +} diff --git a/workspace_tests/version_integrity_test.rs b/workspace_tests/version_integrity_test.rs index acef17e247..d3133f2589 100644 --- a/workspace_tests/version_integrity_test.rs +++ b/workspace_tests/version_integrity_test.rs @@ -1,68 +1,10 @@ -use std::collections::HashMap; -use std::sync::LazyLock; - -use serde::{Deserialize, Serialize}; - -#[derive(Clone, Debug, Serialize, Deserialize)] -#[serde(untagged)] -enum DependencyValue { - String(String), - Object { version: String, path: Option }, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -struct Package { - version: String, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -struct WorkspaceFields { - package: Package, - members: Vec, - dependencies: HashMap, -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -struct CargoToml { - workspace: WorkspaceFields, -} - -#[derive(Debug)] -struct LocalCrate { - path: String, - version: String, -} - -static ROOT_TOML: LazyLock = LazyLock::new(|| { - let root_toml: CargoToml = - toml::from_str(include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../Cargo.toml"))) - .unwrap(); - root_toml -}); - -impl CargoToml { - fn path_dependencies(&self) -> impl Iterator + '_ { - self.workspace.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 - } - }) - } - - fn workspace_version(&self) -> &str { - &self.workspace.package.version - } -} - -// Tests. +use crate::toml_utils::{LocalCrate, ROOT_TOML}; #[test] fn test_path_dependencies_are_members() { let non_member_path_crates: Vec<_> = ROOT_TOML .path_dependencies() - .filter(|LocalCrate { path, .. }| !ROOT_TOML.workspace.members.contains(&path)) + .filter(|LocalCrate { path, .. }| !ROOT_TOML.members().contains(path)) .collect(); assert!( non_member_path_crates.is_empty(),