Skip to content

Commit

Permalink
chore(ci): split workspace tests into modules
Browse files Browse the repository at this point in the history
Signed-off-by: Dori Medini <[email protected]>
  • Loading branch information
dorimedini-starkware committed Aug 25, 2024
1 parent ab97616 commit b721a56
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 62 deletions.
4 changes: 2 additions & 2 deletions workspace_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions workspace_tests/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![cfg(test)]

pub mod toml_utils;
pub mod version_integrity_test;
61 changes: 61 additions & 0 deletions workspace_tests/toml_utils.rs
Original file line number Diff line number Diff line change
@@ -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<String> },
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct Package {
version: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct WorkspaceFields {
package: Package,
members: Vec<String>,
dependencies: HashMap<String, DependencyValue>,
}

#[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<CargoToml> = 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<Item = LocalCrate> + '_ {
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<String> {
&self.workspace.members
}

pub(crate) fn workspace_version(&self) -> &str {
&self.workspace.package.version
}
}
62 changes: 2 additions & 60 deletions workspace_tests/version_integrity_test.rs
Original file line number Diff line number Diff line change
@@ -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<String> },
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct Package {
version: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct WorkspaceFields {
package: Package,
members: Vec<String>,
dependencies: HashMap<String, DependencyValue>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
struct CargoToml {
workspace: WorkspaceFields,
}

#[derive(Debug)]
struct LocalCrate {
path: String,
version: String,
}

static ROOT_TOML: LazyLock<CargoToml> = 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<Item = LocalCrate> + '_ {
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(),
Expand Down

0 comments on commit b721a56

Please sign in to comment.