Skip to content

Commit

Permalink
Improve type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
Serock3 committed Nov 21, 2024
1 parent 604feef commit 73fbdd2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
6 changes: 4 additions & 2 deletions test/test-manager/src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt},
};

use crate::tests::should_run_on_os;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Failed to open log file {1:?}")]
Expand Down Expand Up @@ -118,7 +120,7 @@ pub struct Summary {
impl Summary {
/// Read test summary from `path`.
pub async fn parse_log<P: AsRef<Path>>(
all_tests: &[crate::tests::TestMetadata],
all_tests: &[crate::tests::TestDesciption],
path: P,
) -> Result<Summary, Error> {
let file = fs::OpenOptions::new()
Expand Down Expand Up @@ -155,7 +157,7 @@ impl Summary {
for test in all_tests {
// Add missing test results
let entry = results.entry(test.name.to_owned());
if test.should_run_on_os(os) {
if should_run_on_os(test.targets, os) {
entry.or_insert(TestResult::Unknown);
} else {
entry.or_insert(TestResult::Skip);
Expand Down
37 changes: 25 additions & 12 deletions test/test-manager/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod tunnel;
mod tunnel_state;
mod ui;

use itertools::Itertools;
pub use test_metadata::TestMetadata;

use anyhow::Context;
Expand All @@ -29,7 +30,7 @@ use config::TEST_CONFIG;
use helpers::{get_app_env, install_app};
pub use install::test_upgrade_app;
use mullvad_management_interface::MullvadProxyClient;
use test_rpc::{mullvad_daemon::MullvadClientVersion, ServiceClient};
use test_rpc::{meta::Os, ServiceClient};

const WAIT_FOR_TUNNEL_STATE_TIMEOUT: Duration = Duration::from_secs(40);

Expand Down Expand Up @@ -75,20 +76,32 @@ pub enum Error {
Other(String),
}

#[derive(Clone)]
/// An abbriviated version of [`TestMetadata`]
pub struct TestDesciption {
pub name: &'static str,
pub targets: &'static [Os],
pub priority: Option<i32>,
}

pub fn should_run_on_os(targets: &[Os], os: Os) -> bool {
targets.is_empty() || targets.contains(&os)
}

/// Get a list of all tests, sorted by priority.
pub fn get_tests() -> Vec<TestMetadata> {
let mut tests: Vec<_> = inventory::iter::<TestMetadata>().cloned().collect();
tests.sort_by_key(|test| test.priority.unwrap_or(0));
let test_upgrade_app = TestMetadata {
pub fn get_tests() -> Vec<TestDesciption> {
let tests: Vec<_> = inventory::iter::<TestMetadata>()
.map(|test| TestDesciption {
priority: test.priority,
name: test.name,
targets: test.targets,
})
.sorted_by_key(|test| test.priority)
.collect_vec();
let test_upgrade_app = TestDesciption {
priority: None,
name: "test_upgrade_app",
targets: &[],
mullvad_client_version: MullvadClientVersion::None,
func: |_, _, _| {
Box::pin(async {
unreachable!("`test_upgrade_app` should not be executed from this function pointer")
})
},
};
[vec![test_upgrade_app], tests].concat()
}
Expand All @@ -114,7 +127,7 @@ pub fn get_filtered_tests(specified_tests: &[String]) -> Result<Vec<TestMetadata
})
.collect::<Result<_, anyhow::Error>>()?
};
tests.retain(|test| test.should_run_on_os(TEST_CONFIG.os));
tests.retain(|test| should_run_on_os(test.targets, TEST_CONFIG.os));
Ok(tests)
}

Expand Down
6 changes: 0 additions & 6 deletions test/test-manager/src/tests/test_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,5 @@ pub struct TestMetadata {
pub priority: Option<i32>,
}

impl TestMetadata {
pub fn should_run_on_os(&self, os: Os) -> bool {
self.targets.is_empty() || self.targets.contains(&os)
}
}

// Register our test metadata struct with inventory to allow submitting tests of this type.
inventory::collect!(TestMetadata);

0 comments on commit 73fbdd2

Please sign in to comment.