Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nextest-runner] simplify target-spec code #965

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 42 additions & 32 deletions nextest-runner/src/config/overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use nextest_filtering::{FilteringExpr, TestQuery};
use serde::{Deserialize, Deserializer};
use smol_str::SmolStr;
use std::{collections::HashMap, time::Duration};
use target_spec::TargetSpec;
use target_spec::{Platform, TargetSpec};

/// Settings for individual tests.
///
Expand Down Expand Up @@ -330,8 +330,8 @@ pub(crate) struct OverrideId {

#[derive(Clone, Debug)]
pub(super) struct ProfileOverrideData {
host_spec: Option<TargetSpec>,
target_spec: Option<TargetSpec>,
host_spec: MaybeTargetSpec,
target_spec: MaybeTargetSpec,
expr: Option<FilteringExpr>,
threads_required: Option<ThreadsRequired>,
retries: Option<RetryPolicy>,
Expand Down Expand Up @@ -365,18 +365,8 @@ impl CompiledOverride<PreBuildPlatform> {
return None;
}

let host_spec = source
.platform
.host
.as_ref()
.map(|platform_str| TargetSpec::new(platform_str.to_owned()))
.transpose();
let target_spec = source
.platform
.target
.as_ref()
.map(|platform_str| TargetSpec::new(platform_str.to_owned()))
.transpose();
let host_spec = MaybeTargetSpec::new(source.platform.host.as_deref());
let target_spec = MaybeTargetSpec::new(source.platform.target.as_deref());
let filter_expr = source.filter.as_ref().map_or(Ok(None), |filter| {
Some(FilteringExpr::parse(filter.clone(), graph)).transpose()
});
Expand Down Expand Up @@ -423,22 +413,14 @@ impl CompiledOverride<PreBuildPlatform> {
self,
build_platforms: &BuildPlatforms,
) -> CompiledOverride<FinalConfig> {
let host_eval = if let Some(spec) = &self.data.host_spec {
// unknown (None) gets unwrapped to true.
spec.eval(&build_platforms.host).unwrap_or(true)
} else {
true
};
let (host_test_eval, target_eval) = if let Some(spec) = &self.data.target_spec {
// unknown (None) gets unwrapped to true.
let host_eval = spec.eval(&build_platforms.host).unwrap_or(true);
let target_eval = build_platforms.target.as_ref().map_or(host_eval, |triple| {
spec.eval(&triple.platform).unwrap_or(true)
let host_eval = self.data.host_spec.eval(&build_platforms.host);
let host_test_eval = self.data.target_spec.eval(&build_platforms.host);
let target_eval = build_platforms
.target
.as_ref()
.map_or(host_test_eval, |triple| {
self.data.target_spec.eval(&triple.platform)
});
(host_eval, target_eval)
} else {
(true, true)
};

CompiledOverride {
id: self.id,
Expand All @@ -454,8 +436,8 @@ impl CompiledOverride<PreBuildPlatform> {

impl CompiledOverride<FinalConfig> {
/// Returns the target spec.
pub(crate) fn target_spec(&self) -> Option<&TargetSpec> {
self.data.target_spec.as_ref()
pub(crate) fn target_spec(&self) -> &MaybeTargetSpec {
&self.data.target_spec
}

/// Returns the filter expression, if any.
Expand All @@ -464,6 +446,34 @@ impl CompiledOverride<FinalConfig> {
}
}

/// Represents a [`TargetSpec`] that might have been provided.
#[derive(Clone, Debug, Default)]
pub(crate) enum MaybeTargetSpec {
Provided(TargetSpec),
#[default]
Any,
}

impl MaybeTargetSpec {
fn new(platform_str: Option<&str>) -> Result<Self, target_spec::Error> {
Ok(match platform_str {
Some(platform_str) => {
MaybeTargetSpec::Provided(TargetSpec::new(platform_str.to_owned())?)
}
None => MaybeTargetSpec::Any,
})
}

fn eval(&self, platform: &Platform) -> bool {
match self {
MaybeTargetSpec::Provided(spec) => spec
.eval(platform)
.unwrap_or(/* unknown results are mapped to true */ true),
MaybeTargetSpec::Any => true,
}
}
}

/// Deserialized form of profile overrides before compilation.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
Expand Down
6 changes: 3 additions & 3 deletions nextest-runner/src/show_config/test_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use crate::{
config::{
CompiledOverride, CustomTestGroup, FinalConfig, NextestProfile, OverrideId,
PreBuildPlatform, SettingSource, TestGroup, TestGroupConfig,
CompiledOverride, CustomTestGroup, FinalConfig, MaybeTargetSpec, NextestProfile,
OverrideId, PreBuildPlatform, SettingSource, TestGroup, TestGroupConfig,
},
errors::ShowTestGroupsError,
helpers::QuotedDisplay,
Expand Down Expand Up @@ -167,7 +167,7 @@ impl<'a> ShowTestGroups<'a> {
QuotedDisplay(&expr.parsed).style(styles.filter)
)?;
}
if let Some(target_spec) = data.override_.target_spec() {
if let MaybeTargetSpec::Provided(target_spec) = data.override_.target_spec() {
write!(
writer,
" on platform {}",
Expand Down
Loading