Skip to main content

nextest_runner/config/overrides/
imp.rs

1// Copyright (c) The nextest Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use crate::{
5    config::{
6        core::{
7            EvaluatableProfile, FinalConfig, NextestConfig, NextestConfigImpl, PreBuildPlatform,
8        },
9        elements::{
10            FlakyResult, JunitFlakyFailStatus, LeakTimeout, RetryPolicy, SlowTimeout, TestGroup,
11            TestPriority, ThreadsRequired,
12        },
13        scripts::{
14            CompiledProfileScripts, DeserializedProfileScriptConfig, ScriptId, WrapperScriptConfig,
15        },
16    },
17    errors::{
18        ConfigCompileError, ConfigCompileErrorKind, ConfigCompileSection, ConfigParseErrorKind,
19    },
20    platform::BuildPlatforms,
21    reporter::TestOutputDisplay,
22    run_mode::NextestRunMode,
23};
24use guppy::graph::cargo::BuildPlatform;
25use nextest_filtering::{
26    BinaryQuery, CompiledExpr, Filterset, FiltersetKind, KnownGroups, ParseContext, TestQuery,
27};
28use owo_colors::{OwoColorize, Style};
29use serde::{Deserialize, Deserializer};
30use smol_str::SmolStr;
31use std::collections::HashMap;
32use target_spec::{Platform, TargetSpec};
33
34/// Settings for a test binary.
35#[derive(Clone, Debug)]
36pub struct ListSettings<'p, Source = ()> {
37    list_wrapper: Option<(&'p WrapperScriptConfig, Source)>,
38}
39
40impl<'p, Source: Copy> ListSettings<'p, Source> {
41    pub(in crate::config) fn new(
42        profile: &'p EvaluatableProfile<'_>,
43        query: &BinaryQuery<'_>,
44    ) -> Self
45    where
46        Source: TrackSource<'p>,
47    {
48        let ecx = profile.filterset_ecx();
49
50        let mut list_wrapper = None;
51
52        for override_ in &profile.compiled_data.scripts {
53            if let Some(wrapper) = &override_.list_wrapper
54                && list_wrapper.is_none()
55            {
56                let (wrapper, source) =
57                    map_wrapper_script(profile, Source::track_script(wrapper.clone(), override_));
58
59                if !override_
60                    .is_enabled_binary(query, &ecx)
61                    .expect("test() in list-time scripts should have been rejected")
62                {
63                    continue;
64                }
65
66                list_wrapper = Some((wrapper, source));
67            }
68        }
69
70        Self { list_wrapper }
71    }
72}
73
74impl<'p> ListSettings<'p> {
75    /// Returns a default list-settings without a wrapper script.
76    ///
77    /// Debug command used for testing.
78    pub fn debug_empty() -> Self {
79        Self { list_wrapper: None }
80    }
81
82    /// Sets the wrapper to use for list-time scripts.
83    ///
84    /// Debug command used for testing.
85    pub fn debug_set_list_wrapper(&mut self, wrapper: &'p WrapperScriptConfig) -> &mut Self {
86        self.list_wrapper = Some((wrapper, ()));
87        self
88    }
89
90    /// Returns the list-time wrapper script.
91    pub fn list_wrapper(&self) -> Option<&'p WrapperScriptConfig> {
92        self.list_wrapper.as_ref().map(|(wrapper, _)| *wrapper)
93    }
94}
95
96/// Settings for individual tests.
97///
98/// Returned by [`EvaluatableProfile::settings_for`].
99///
100/// The `Source` parameter tracks an optional source; this isn't used by any public APIs at the
101/// moment.
102#[derive(Clone, Debug)]
103pub struct TestSettings<'p, Source = ()> {
104    priority: (TestPriority, Source),
105    threads_required: (ThreadsRequired, Source),
106    run_wrapper: Option<(&'p WrapperScriptConfig, Source)>,
107    run_extra_args: (&'p [String], Source),
108    retries: (RetryPolicy, Source),
109    flaky_result: (FlakyResult, Source),
110    slow_timeout: (SlowTimeout, Source),
111    leak_timeout: (LeakTimeout, Source),
112    test_group: (TestGroup, Source),
113    success_output: (TestOutputDisplay, Source),
114    failure_output: (TestOutputDisplay, Source),
115    junit_store_success_output: (bool, Source),
116    junit_store_failure_output: (bool, Source),
117    junit_flaky_fail_status: (JunitFlakyFailStatus, Source),
118}
119
120pub(crate) trait TrackSource<'p>: Sized {
121    fn track_default<T>(value: T) -> (T, Self);
122    fn track_profile<T>(value: T) -> (T, Self);
123    fn track_override<T>(value: T, source: &'p CompiledOverride<FinalConfig>) -> (T, Self);
124    fn track_script<T>(value: T, source: &'p CompiledProfileScripts<FinalConfig>) -> (T, Self);
125}
126
127impl<'p> TrackSource<'p> for () {
128    fn track_default<T>(value: T) -> (T, Self) {
129        (value, ())
130    }
131
132    fn track_profile<T>(value: T) -> (T, Self) {
133        (value, ())
134    }
135
136    fn track_override<T>(value: T, _source: &'p CompiledOverride<FinalConfig>) -> (T, Self) {
137        (value, ())
138    }
139
140    fn track_script<T>(value: T, _source: &'p CompiledProfileScripts<FinalConfig>) -> (T, Self) {
141        (value, ())
142    }
143}
144
145#[derive(Copy, Clone, Debug)]
146pub(crate) enum SettingSource<'p> {
147    /// A default configuration not specified in, or possible to override from,
148    /// a profile.
149    Default,
150
151    /// A configuration specified in a profile.
152    Profile,
153
154    /// An override specified in a profile.
155    Override(&'p CompiledOverride<FinalConfig>),
156
157    /// An override specified in the `scripts` section.
158    #[expect(dead_code)]
159    Script(&'p CompiledProfileScripts<FinalConfig>),
160}
161
162impl<'p> TrackSource<'p> for SettingSource<'p> {
163    fn track_default<T>(value: T) -> (T, Self) {
164        (value, SettingSource::Default)
165    }
166
167    fn track_profile<T>(value: T) -> (T, Self) {
168        (value, SettingSource::Profile)
169    }
170
171    fn track_override<T>(value: T, source: &'p CompiledOverride<FinalConfig>) -> (T, Self) {
172        (value, SettingSource::Override(source))
173    }
174
175    fn track_script<T>(value: T, source: &'p CompiledProfileScripts<FinalConfig>) -> (T, Self) {
176        (value, SettingSource::Script(source))
177    }
178}
179
180impl<'p> TestSettings<'p> {
181    /// Returns the test's priority.
182    pub fn priority(&self) -> TestPriority {
183        self.priority.0
184    }
185
186    /// Returns the number of threads required for this test.
187    pub fn threads_required(&self) -> ThreadsRequired {
188        self.threads_required.0
189    }
190
191    /// Returns the run-time wrapper script for this test.
192    pub fn run_wrapper(&self) -> Option<&'p WrapperScriptConfig> {
193        self.run_wrapper.map(|(script, _)| script)
194    }
195
196    /// Returns extra arguments to pass at runtime for this test.
197    pub fn run_extra_args(&self) -> &'p [String] {
198        self.run_extra_args.0
199    }
200
201    /// Returns the number of retries for this test.
202    pub fn retries(&self) -> RetryPolicy {
203        self.retries.0
204    }
205
206    /// Returns the flaky result behavior for this test.
207    pub fn flaky_result(&self) -> FlakyResult {
208        self.flaky_result.0
209    }
210
211    /// Returns the slow timeout for this test.
212    pub fn slow_timeout(&self) -> SlowTimeout {
213        self.slow_timeout.0
214    }
215
216    /// Returns the leak timeout for this test.
217    pub fn leak_timeout(&self) -> LeakTimeout {
218        self.leak_timeout.0
219    }
220
221    /// Returns the test group for this test.
222    pub fn test_group(&self) -> &TestGroup {
223        &self.test_group.0
224    }
225
226    /// Returns the success output setting for this test.
227    pub fn success_output(&self) -> TestOutputDisplay {
228        self.success_output.0
229    }
230
231    /// Returns the failure output setting for this test.
232    pub fn failure_output(&self) -> TestOutputDisplay {
233        self.failure_output.0
234    }
235
236    /// Returns whether success output should be stored in JUnit.
237    pub fn junit_store_success_output(&self) -> bool {
238        self.junit_store_success_output.0
239    }
240
241    /// Returns whether failure output should be stored in JUnit.
242    pub fn junit_store_failure_output(&self) -> bool {
243        self.junit_store_failure_output.0
244    }
245
246    /// Returns the JUnit flaky-fail status for this test.
247    pub fn junit_flaky_fail_status(&self) -> JunitFlakyFailStatus {
248        self.junit_flaky_fail_status.0
249    }
250}
251
252#[expect(dead_code)]
253impl<'p, Source: Copy> TestSettings<'p, Source> {
254    pub(in crate::config) fn new(
255        profile: &'p EvaluatableProfile<'_>,
256        run_mode: NextestRunMode,
257        query: &TestQuery<'_>,
258    ) -> Self
259    where
260        Source: TrackSource<'p>,
261    {
262        let ecx = profile.filterset_ecx();
263
264        let mut priority = None;
265        let mut threads_required = None;
266        let mut run_wrapper = None;
267        let mut run_extra_args = None;
268        let mut retries = None;
269        let mut flaky_result = None;
270        let mut slow_timeout = None;
271        let mut leak_timeout = None;
272        let mut test_group = None;
273        let mut success_output = None;
274        let mut failure_output = None;
275        let mut junit_store_success_output = None;
276        let mut junit_store_failure_output = None;
277        let mut junit_flaky_fail_status = None;
278
279        for override_ in &profile.compiled_data.overrides {
280            if !override_.matches_test_query(query, &ecx) {
281                continue;
282            }
283
284            if priority.is_none()
285                && let Some(p) = override_.data.priority
286            {
287                priority = Some(Source::track_override(p, override_));
288            }
289            if threads_required.is_none()
290                && let Some(t) = override_.data.threads_required
291            {
292                threads_required = Some(Source::track_override(t, override_));
293            }
294            if run_extra_args.is_none()
295                && let Some(r) = override_.data.run_extra_args.as_deref()
296            {
297                run_extra_args = Some(Source::track_override(r, override_));
298            }
299            if retries.is_none()
300                && let Some(r) = override_.data.retries
301            {
302                retries = Some(Source::track_override(r, override_));
303            }
304            if flaky_result.is_none()
305                && let Some(fr) = override_.data.flaky_result
306            {
307                flaky_result = Some(Source::track_override(fr, override_));
308            }
309            if slow_timeout.is_none() {
310                // Use the appropriate slow timeout based on run mode. Note that
311                // there's no fallback from bench to test timeout.
312                let timeout_for_mode = match run_mode {
313                    NextestRunMode::Test => override_.data.slow_timeout,
314                    NextestRunMode::Benchmark => override_.data.bench_slow_timeout,
315                };
316                if let Some(s) = timeout_for_mode {
317                    slow_timeout = Some(Source::track_override(s, override_));
318                }
319            }
320            if leak_timeout.is_none()
321                && let Some(l) = override_.data.leak_timeout
322            {
323                leak_timeout = Some(Source::track_override(l, override_));
324            }
325            if test_group.is_none()
326                && let Some(t) = &override_.data.test_group
327            {
328                test_group = Some(Source::track_override(t.clone(), override_));
329            }
330            if success_output.is_none()
331                && let Some(s) = override_.data.success_output
332            {
333                success_output = Some(Source::track_override(s, override_));
334            }
335            if failure_output.is_none()
336                && let Some(f) = override_.data.failure_output
337            {
338                failure_output = Some(Source::track_override(f, override_));
339            }
340            if junit_store_success_output.is_none()
341                && let Some(s) = override_.data.junit.store_success_output
342            {
343                junit_store_success_output = Some(Source::track_override(s, override_));
344            }
345            if junit_store_failure_output.is_none()
346                && let Some(f) = override_.data.junit.store_failure_output
347            {
348                junit_store_failure_output = Some(Source::track_override(f, override_));
349            }
350            if junit_flaky_fail_status.is_none()
351                && let Some(s) = override_.data.junit.flaky_fail_status
352            {
353                junit_flaky_fail_status = Some(Source::track_override(s, override_));
354            }
355        }
356
357        for override_ in &profile.compiled_data.scripts {
358            if !override_.is_enabled(query, &ecx) {
359                continue;
360            }
361
362            if run_wrapper.is_none()
363                && let Some(wrapper) = &override_.run_wrapper
364            {
365                run_wrapper = Some(Source::track_script(wrapper.clone(), override_));
366            }
367        }
368
369        // If no overrides were found, use the profile defaults.
370        let priority = priority.unwrap_or_else(|| Source::track_default(TestPriority::default()));
371        let threads_required =
372            threads_required.unwrap_or_else(|| Source::track_profile(profile.threads_required()));
373        let run_wrapper = run_wrapper.map(|wrapper| map_wrapper_script(profile, wrapper));
374        let run_extra_args =
375            run_extra_args.unwrap_or_else(|| Source::track_profile(profile.run_extra_args()));
376        let retries = retries.unwrap_or_else(|| Source::track_profile(profile.retries()));
377        let flaky_result =
378            flaky_result.unwrap_or_else(|| Source::track_profile(profile.flaky_result()));
379        let slow_timeout =
380            slow_timeout.unwrap_or_else(|| Source::track_profile(profile.slow_timeout(run_mode)));
381        let leak_timeout =
382            leak_timeout.unwrap_or_else(|| Source::track_profile(profile.leak_timeout()));
383        let test_group = test_group.unwrap_or_else(|| Source::track_profile(TestGroup::Global));
384        let success_output =
385            success_output.unwrap_or_else(|| Source::track_profile(profile.success_output()));
386        let failure_output =
387            failure_output.unwrap_or_else(|| Source::track_profile(profile.failure_output()));
388        let junit_store_success_output = junit_store_success_output.unwrap_or_else(|| {
389            // If the profile doesn't have JUnit enabled, success output can just be false.
390            Source::track_profile(profile.junit().is_some_and(|j| j.store_success_output()))
391        });
392        let junit_store_failure_output = junit_store_failure_output.unwrap_or_else(|| {
393            // If the profile doesn't have JUnit enabled, failure output can just be false.
394            Source::track_profile(profile.junit().is_some_and(|j| j.store_failure_output()))
395        });
396        let junit_flaky_fail_status = junit_flaky_fail_status.unwrap_or_else(|| {
397            Source::track_profile(
398                profile
399                    .junit()
400                    .map_or(JunitFlakyFailStatus::default(), |j| j.flaky_fail_status()),
401            )
402        });
403
404        TestSettings {
405            threads_required,
406            run_extra_args,
407            run_wrapper,
408            retries,
409            flaky_result,
410            priority,
411            slow_timeout,
412            leak_timeout,
413            test_group,
414            success_output,
415            failure_output,
416            junit_store_success_output,
417            junit_store_failure_output,
418            junit_flaky_fail_status,
419        }
420    }
421
422    /// Returns the number of threads required for this test, with the source attached.
423    pub(crate) fn threads_required_with_source(&self) -> (ThreadsRequired, Source) {
424        self.threads_required
425    }
426
427    /// Returns the number of retries for this test, with the source attached.
428    pub(crate) fn retries_with_source(&self) -> (RetryPolicy, Source) {
429        self.retries
430    }
431
432    /// Returns the slow timeout for this test, with the source attached.
433    pub(crate) fn slow_timeout_with_source(&self) -> (SlowTimeout, Source) {
434        self.slow_timeout
435    }
436
437    /// Returns the leak timeout for this test, with the source attached.
438    pub(crate) fn leak_timeout_with_source(&self) -> (LeakTimeout, Source) {
439        self.leak_timeout
440    }
441
442    /// Returns the test group for this test, with the source attached.
443    pub(crate) fn test_group_with_source(&self) -> &(TestGroup, Source) {
444        &self.test_group
445    }
446}
447
448fn map_wrapper_script<'p, Source>(
449    profile: &'p EvaluatableProfile<'_>,
450    (script, source): (ScriptId, Source),
451) -> (&'p WrapperScriptConfig, Source)
452where
453    Source: TrackSource<'p>,
454{
455    let wrapper_config = profile
456        .script_config()
457        .wrapper
458        .get(&script)
459        .unwrap_or_else(|| {
460            panic!(
461                "wrapper script {script} not found \
462                 (should have been checked while reading config)"
463            )
464        });
465    (wrapper_config, source)
466}
467
468#[derive(Clone, Debug)]
469pub(in crate::config) struct CompiledByProfile {
470    pub(in crate::config) default: CompiledData<PreBuildPlatform>,
471    pub(in crate::config) other: HashMap<String, CompiledData<PreBuildPlatform>>,
472}
473
474impl CompiledByProfile {
475    pub(in crate::config) fn new(
476        pcx: &ParseContext<'_>,
477        config: &NextestConfigImpl,
478    ) -> Result<Self, ConfigParseErrorKind> {
479        let mut errors = vec![];
480        let default = CompiledData::new(
481            pcx,
482            "default",
483            Some(config.default_profile().default_filter()),
484            config.default_profile().overrides(),
485            config.default_profile().setup_scripts(),
486            &mut errors,
487        );
488        let other: HashMap<_, _> = config
489            .other_profiles()
490            .map(|(profile_name, profile)| {
491                (
492                    profile_name.to_owned(),
493                    CompiledData::new(
494                        pcx,
495                        profile_name,
496                        profile.default_filter(),
497                        profile.overrides(),
498                        profile.scripts(),
499                        &mut errors,
500                    ),
501                )
502            })
503            .collect();
504
505        if errors.is_empty() {
506            Ok(Self { default, other })
507        } else {
508            Err(ConfigParseErrorKind::CompileErrors(errors))
509        }
510    }
511
512    /// Returns the compiled data for the default config.
513    ///
514    /// The default config does not depend on the package graph, so we create it separately here.
515    /// But we don't implement `Default` to make sure that the value is for the default _config_,
516    /// not the default _profile_ (which repo config can customize).
517    pub(in crate::config) fn for_default_config() -> Self {
518        Self {
519            default: CompiledData {
520                profile_default_filter: Some(CompiledDefaultFilter::for_default_config()),
521                overrides: vec![],
522                scripts: vec![],
523            },
524            other: HashMap::new(),
525        }
526    }
527}
528
529/// A compiled form of the default filter for a profile.
530///
531/// Returned by [`EvaluatableProfile::default_filter`].
532#[derive(Clone, Debug)]
533pub struct CompiledDefaultFilter {
534    /// The compiled expression.
535    ///
536    /// This is a bit tricky -- in some cases, the default config is constructed without a
537    /// `PackageGraph` being available. But parsing filtersets requires a `PackageGraph`. So we hack
538    /// around it by only storing the compiled expression here, and by setting it to `all()` (which
539    /// matches the config).
540    ///
541    /// This does make the default-filter defined in default-config.toml a bit
542    /// of a lie (since we don't use it directly, but instead replicate it in
543    /// code). But it's not too bad.
544    pub expr: CompiledExpr,
545
546    /// The profile name the default filter originates from.
547    pub profile: String,
548
549    /// The section of the config that the default filter comes from.
550    pub section: CompiledDefaultFilterSection,
551}
552
553impl CompiledDefaultFilter {
554    pub(crate) fn for_default_config() -> Self {
555        Self {
556            expr: CompiledExpr::ALL,
557            profile: NextestConfig::DEFAULT_PROFILE.to_owned(),
558            section: CompiledDefaultFilterSection::Profile,
559        }
560    }
561
562    /// Displays a configuration string for the default filter.
563    pub fn display_config(&self, bold_style: Style) -> String {
564        match &self.section {
565            CompiledDefaultFilterSection::Profile => {
566                format!("profile.{}.default-filter", self.profile)
567                    .style(bold_style)
568                    .to_string()
569            }
570            CompiledDefaultFilterSection::Override(_) => {
571                format!(
572                    "default-filter in {}",
573                    format!("profile.{}.overrides", self.profile).style(bold_style)
574                )
575            }
576        }
577    }
578}
579
580/// Within [`CompiledDefaultFilter`], the part of the config that the default
581/// filter comes from.
582#[derive(Clone, Copy, Debug)]
583pub enum CompiledDefaultFilterSection {
584    /// The config comes from the top-level `profile.<profile-name>.default-filter`.
585    Profile,
586
587    /// The config comes from the override at the given index.
588    Override(usize),
589}
590
591#[derive(Clone, Debug)]
592pub(in crate::config) struct CompiledData<State> {
593    // The default filter specified at the profile level.
594    //
595    // Overrides might also specify their own filters, and in that case the
596    // overrides take priority.
597    pub(in crate::config) profile_default_filter: Option<CompiledDefaultFilter>,
598    pub(in crate::config) overrides: Vec<CompiledOverride<State>>,
599    pub(in crate::config) scripts: Vec<CompiledProfileScripts<State>>,
600}
601
602impl CompiledData<PreBuildPlatform> {
603    fn new(
604        pcx: &ParseContext<'_>,
605        profile_name: &str,
606        profile_default_filter: Option<&str>,
607        overrides: &[DeserializedOverride],
608        scripts: &[DeserializedProfileScriptConfig],
609        errors: &mut Vec<ConfigCompileError>,
610    ) -> Self {
611        let profile_default_filter =
612            profile_default_filter.and_then(|filter| {
613                match Filterset::parse(
614                    filter.to_owned(),
615                    pcx,
616                    FiltersetKind::DefaultFilter,
617                    &KnownGroups::Unavailable,
618                ) {
619                    Ok(expr) => Some(CompiledDefaultFilter {
620                        expr: expr.compiled,
621                        profile: profile_name.to_owned(),
622                        section: CompiledDefaultFilterSection::Profile,
623                    }),
624                    Err(err) => {
625                        errors.push(ConfigCompileError {
626                            profile_name: profile_name.to_owned(),
627                            section: ConfigCompileSection::DefaultFilter,
628                            kind: ConfigCompileErrorKind::Parse {
629                                host_parse_error: None,
630                                target_parse_error: None,
631                                filter_parse_errors: vec![err],
632                            },
633                        });
634                        None
635                    }
636                }
637            });
638
639        let overrides = overrides
640            .iter()
641            .enumerate()
642            .filter_map(|(index, source)| {
643                CompiledOverride::new(pcx, profile_name, index, source, errors)
644            })
645            .collect();
646        let scripts = scripts
647            .iter()
648            .enumerate()
649            .filter_map(|(index, source)| {
650                CompiledProfileScripts::new(pcx, profile_name, index, source, errors)
651            })
652            .collect();
653        Self {
654            profile_default_filter,
655            overrides,
656            scripts,
657        }
658    }
659
660    pub(in crate::config) fn extend_reverse(&mut self, other: Self) {
661        // For the default filter, other wins (it is last, and after reversing, it will be first).
662        if other.profile_default_filter.is_some() {
663            self.profile_default_filter = other.profile_default_filter;
664        }
665        self.overrides.extend(other.overrides.into_iter().rev());
666        self.scripts.extend(other.scripts.into_iter().rev());
667    }
668
669    pub(in crate::config) fn reverse(&mut self) {
670        self.overrides.reverse();
671        self.scripts.reverse();
672    }
673
674    /// Chains this data with another set of data, treating `other` as lower-priority than `self`.
675    pub(in crate::config) fn chain(self, other: Self) -> Self {
676        let profile_default_filter = self.profile_default_filter.or(other.profile_default_filter);
677        let mut overrides = self.overrides;
678        let mut scripts = self.scripts;
679        overrides.extend(other.overrides);
680        scripts.extend(other.scripts);
681        Self {
682            profile_default_filter,
683            overrides,
684            scripts,
685        }
686    }
687
688    pub(in crate::config) fn apply_build_platforms(
689        self,
690        build_platforms: &BuildPlatforms,
691    ) -> CompiledData<FinalConfig> {
692        let profile_default_filter = self.profile_default_filter;
693        let overrides = self
694            .overrides
695            .into_iter()
696            .map(|override_| override_.apply_build_platforms(build_platforms))
697            .collect();
698        let setup_scripts = self
699            .scripts
700            .into_iter()
701            .map(|setup_script| setup_script.apply_build_platforms(build_platforms))
702            .collect();
703        CompiledData {
704            profile_default_filter,
705            overrides,
706            scripts: setup_scripts,
707        }
708    }
709}
710
711#[derive(Clone, Debug)]
712pub(crate) struct CompiledOverride<State> {
713    id: OverrideId,
714    state: State,
715    pub(in crate::config) data: ProfileOverrideData,
716}
717
718impl<State> CompiledOverride<State> {
719    pub(crate) fn id(&self) -> &OverrideId {
720        &self.id
721    }
722}
723
724#[derive(Clone, Debug, Eq, Hash, PartialEq)]
725pub(crate) struct OverrideId {
726    pub(crate) profile_name: SmolStr,
727    index: usize,
728}
729
730#[derive(Clone, Debug)]
731pub(in crate::config) struct ProfileOverrideData {
732    host_spec: MaybeTargetSpec,
733    target_spec: MaybeTargetSpec,
734    filter: Option<FilterOrDefaultFilter>,
735    priority: Option<TestPriority>,
736    threads_required: Option<ThreadsRequired>,
737    run_extra_args: Option<Vec<String>>,
738    retries: Option<RetryPolicy>,
739    flaky_result: Option<FlakyResult>,
740    slow_timeout: Option<SlowTimeout>,
741    bench_slow_timeout: Option<SlowTimeout>,
742    leak_timeout: Option<LeakTimeout>,
743    pub(in crate::config) test_group: Option<TestGroup>,
744    success_output: Option<TestOutputDisplay>,
745    failure_output: Option<TestOutputDisplay>,
746    junit: DeserializedJunitOutput,
747}
748
749impl CompiledOverride<PreBuildPlatform> {
750    fn new(
751        pcx: &ParseContext<'_>,
752        profile_name: &str,
753        index: usize,
754        source: &DeserializedOverride,
755        errors: &mut Vec<ConfigCompileError>,
756    ) -> Option<Self> {
757        if source.platform.host.is_none()
758            && source.platform.target.is_none()
759            && source.filter.is_none()
760        {
761            errors.push(ConfigCompileError {
762                profile_name: profile_name.to_owned(),
763                section: ConfigCompileSection::Override(index),
764                kind: ConfigCompileErrorKind::ConstraintsNotSpecified {
765                    default_filter_specified: source.default_filter.is_some(),
766                },
767            });
768            return None;
769        }
770
771        let host_spec = MaybeTargetSpec::new(source.platform.host.as_deref());
772        let target_spec = MaybeTargetSpec::new(source.platform.target.as_deref());
773        let filter = source.filter.as_ref().map_or(Ok(None), |filter| {
774            Some(Filterset::parse(
775                filter.clone(),
776                pcx,
777                FiltersetKind::OverrideFilter,
778                &KnownGroups::Unavailable,
779            ))
780            .transpose()
781        });
782        let default_filter = source.default_filter.as_ref().map_or(Ok(None), |filter| {
783            Some(Filterset::parse(
784                filter.clone(),
785                pcx,
786                FiltersetKind::DefaultFilter,
787                &KnownGroups::Unavailable,
788            ))
789            .transpose()
790        });
791
792        match (host_spec, target_spec, filter, default_filter) {
793            (Ok(host_spec), Ok(target_spec), Ok(filter), Ok(default_filter)) => {
794                // At most one of filter and default-filter can be specified.
795                let filter = match (filter, default_filter) {
796                    (Some(_), Some(_)) => {
797                        errors.push(ConfigCompileError {
798                            profile_name: profile_name.to_owned(),
799                            section: ConfigCompileSection::Override(index),
800                            kind: ConfigCompileErrorKind::FilterAndDefaultFilterSpecified,
801                        });
802                        return None;
803                    }
804                    (Some(filter), None) => Some(FilterOrDefaultFilter::Filter(filter)),
805                    (None, Some(default_filter)) => {
806                        let compiled = CompiledDefaultFilter {
807                            expr: default_filter.compiled,
808                            profile: profile_name.to_owned(),
809                            section: CompiledDefaultFilterSection::Override(index),
810                        };
811                        Some(FilterOrDefaultFilter::DefaultFilter(compiled))
812                    }
813                    (None, None) => None,
814                };
815
816                Some(Self {
817                    id: OverrideId {
818                        profile_name: profile_name.into(),
819                        index,
820                    },
821                    state: PreBuildPlatform {},
822                    data: ProfileOverrideData {
823                        host_spec,
824                        target_spec,
825                        filter,
826                        priority: source.priority,
827                        threads_required: source.threads_required,
828                        run_extra_args: source.run_extra_args.clone(),
829                        retries: source.retries,
830                        flaky_result: source.flaky_result,
831                        slow_timeout: source.slow_timeout,
832                        bench_slow_timeout: source.bench.slow_timeout,
833                        leak_timeout: source.leak_timeout,
834                        test_group: source.test_group.clone(),
835                        success_output: source.success_output,
836                        failure_output: source.failure_output,
837                        junit: source.junit,
838                    },
839                })
840            }
841            (maybe_host_err, maybe_target_err, maybe_filter_err, maybe_default_filter_err) => {
842                let host_parse_error = maybe_host_err.err();
843                let target_parse_error = maybe_target_err.err();
844                let filter_parse_errors = maybe_filter_err
845                    .err()
846                    .into_iter()
847                    .chain(maybe_default_filter_err.err())
848                    .collect();
849
850                errors.push(ConfigCompileError {
851                    profile_name: profile_name.to_owned(),
852                    section: ConfigCompileSection::Override(index),
853                    kind: ConfigCompileErrorKind::Parse {
854                        host_parse_error,
855                        target_parse_error,
856                        filter_parse_errors,
857                    },
858                });
859                None
860            }
861        }
862    }
863
864    pub(in crate::config) fn apply_build_platforms(
865        self,
866        build_platforms: &BuildPlatforms,
867    ) -> CompiledOverride<FinalConfig> {
868        let host_eval = self.data.host_spec.eval(&build_platforms.host.platform);
869        let host_test_eval = self.data.target_spec.eval(&build_platforms.host.platform);
870        let target_eval = build_platforms
871            .target
872            .as_ref()
873            .map_or(host_test_eval, |target| {
874                self.data.target_spec.eval(&target.triple.platform)
875            });
876
877        CompiledOverride {
878            id: self.id,
879            state: FinalConfig {
880                host_eval,
881                host_test_eval,
882                target_eval,
883            },
884            data: self.data,
885        }
886    }
887}
888
889impl CompiledOverride<FinalConfig> {
890    /// Returns the target spec.
891    pub(crate) fn target_spec(&self) -> &MaybeTargetSpec {
892        &self.data.target_spec
893    }
894
895    /// Returns the filter to apply to overrides, if any.
896    pub(crate) fn filter(&self) -> Option<&Filterset> {
897        match self.data.filter.as_ref() {
898            Some(FilterOrDefaultFilter::Filter(filter)) => Some(filter),
899            _ => None,
900        }
901    }
902
903    /// Returns true if this override's platform and filter constraints
904    /// match the given test query.
905    pub(in crate::config) fn matches_test_query(
906        &self,
907        query: &TestQuery<'_>,
908        ecx: &nextest_filtering::EvalContext<'_>,
909    ) -> bool {
910        if !self.state.host_eval {
911            return false;
912        }
913        if query.binary_query.platform == BuildPlatform::Host && !self.state.host_test_eval {
914            return false;
915        }
916        if query.binary_query.platform == BuildPlatform::Target && !self.state.target_eval {
917            return false;
918        }
919        // If no expression is present, it's equivalent to "all()".
920        if let Some(expr) = self.filter()
921            && !expr.matches_test(query, ecx)
922        {
923            return false;
924        }
925        true
926    }
927
928    /// Returns the default filter if it matches the platform.
929    pub(crate) fn default_filter_if_matches_platform(&self) -> Option<&CompiledDefaultFilter> {
930        match self.data.filter.as_ref() {
931            Some(FilterOrDefaultFilter::DefaultFilter(filter)) => {
932                // Which kind of evaluation to assume: matching the *target*
933                // filter against the *target* platform (host_eval +
934                // target_eval), or matching the *target* filter against the
935                // *host* platform (host_eval + host_test_eval)? The former
936                // makes much more sense, since in a cross-compile scenario you
937                // want to match a (host, target) pair.
938                (self.state.host_eval && self.state.target_eval).then_some(filter)
939            }
940            _ => None,
941        }
942    }
943}
944
945/// Represents a [`TargetSpec`] that might have been provided.
946#[derive(Clone, Debug, Default)]
947pub(crate) enum MaybeTargetSpec {
948    Provided(TargetSpec),
949    #[default]
950    Any,
951}
952
953impl MaybeTargetSpec {
954    pub(in crate::config) fn new(platform_str: Option<&str>) -> Result<Self, target_spec::Error> {
955        Ok(match platform_str {
956            Some(platform_str) => {
957                MaybeTargetSpec::Provided(TargetSpec::new(platform_str.to_owned())?)
958            }
959            None => MaybeTargetSpec::Any,
960        })
961    }
962
963    pub(in crate::config) fn eval(&self, platform: &Platform) -> bool {
964        match self {
965            MaybeTargetSpec::Provided(spec) => spec
966                .eval(platform)
967                .unwrap_or(/* unknown results are mapped to true */ true),
968            MaybeTargetSpec::Any => true,
969        }
970    }
971}
972
973/// Either a filter override or a default filter specified for a platform.
974///
975/// At most one of these can be specified.
976#[derive(Clone, Debug)]
977pub(crate) enum FilterOrDefaultFilter {
978    Filter(Filterset),
979    DefaultFilter(CompiledDefaultFilter),
980}
981
982/// Deserialized form of profile overrides before compilation.
983#[derive(Clone, Debug, Deserialize)]
984#[cfg_attr(feature = "config-schema", derive(schemars::JsonSchema))]
985#[cfg_attr(feature = "config-schema", schemars(deny_unknown_fields))]
986#[serde(rename_all = "kebab-case")]
987pub(in crate::config) struct DeserializedOverride {
988    /// The host and/or target platforms to match against.
989    #[serde(default)]
990    platform: PlatformStrings,
991    /// The filterset to match against.
992    #[serde(default)]
993    filter: Option<String>,
994    // Overrides start here.
995    //
996    // (This used to use serde(flatten) but that has issues:
997    // https://github.com/serde-rs/serde/issues/2312.)
998    // ---
999    /// Test priority.
1000    #[serde(default)]
1001    priority: Option<TestPriority>,
1002    /// The default filter for tests. Only supported if platform is specified
1003    /// but filter is not.
1004    #[serde(default)]
1005    default_filter: Option<String>,
1006    /// The number of threads required for matching tests.
1007    #[serde(default)]
1008    threads_required: Option<ThreadsRequired>,
1009    /// Extra arguments to pass to the test runner.
1010    #[serde(default)]
1011    run_extra_args: Option<Vec<String>>,
1012    /// Retry policy for matching tests.
1013    #[serde(
1014        default,
1015        deserialize_with = "crate::config::elements::deserialize_retry_policy"
1016    )]
1017    retries: Option<RetryPolicy>,
1018    /// Whether to treat flaky tests as passing or failing.
1019    #[serde(default)]
1020    flaky_result: Option<FlakyResult>,
1021    /// Slow timeout for matching tests.
1022    #[serde(
1023        default,
1024        deserialize_with = "crate::config::elements::deserialize_slow_timeout"
1025    )]
1026    slow_timeout: Option<SlowTimeout>,
1027    /// Leak timeout for matching tests.
1028    #[serde(
1029        default,
1030        deserialize_with = "crate::config::elements::deserialize_leak_timeout"
1031    )]
1032    leak_timeout: Option<LeakTimeout>,
1033    /// Test group to put matching tests in.
1034    #[serde(default)]
1035    test_group: Option<TestGroup>,
1036    /// Success output display for matching tests.
1037    #[serde(default)]
1038    success_output: Option<TestOutputDisplay>,
1039    /// Failure output display for matching tests.
1040    #[serde(default)]
1041    failure_output: Option<TestOutputDisplay>,
1042    /// JUnit output configuration for matching tests.
1043    #[serde(default)]
1044    junit: DeserializedJunitOutput,
1045    /// Benchmark-specific overrides.
1046    #[serde(default)]
1047    bench: DeserializedOverrideBench,
1048}
1049
1050#[derive(Copy, Clone, Debug, Default, Deserialize)]
1051#[cfg_attr(feature = "config-schema", derive(schemars::JsonSchema))]
1052#[cfg_attr(feature = "config-schema", schemars(deny_unknown_fields))]
1053#[serde(rename_all = "kebab-case")]
1054pub(in crate::config) struct DeserializedJunitOutput {
1055    /// Whether to store successful test output in the JUnit XML report.
1056    store_success_output: Option<bool>,
1057    /// Whether to store failed test output in the JUnit XML report.
1058    store_failure_output: Option<bool>,
1059    /// How flaky-fail tests are reported in the JUnit XML report.
1060    flaky_fail_status: Option<JunitFlakyFailStatus>,
1061}
1062
1063/// Deserialized form of benchmark-specific overrides.
1064#[derive(Clone, Debug, Default, Deserialize)]
1065#[cfg_attr(feature = "config-schema", derive(schemars::JsonSchema))]
1066#[cfg_attr(feature = "config-schema", schemars(deny_unknown_fields))]
1067#[serde(rename_all = "kebab-case")]
1068pub(in crate::config) struct DeserializedOverrideBench {
1069    /// Slow timeout for matching benchmarks.
1070    #[serde(
1071        default,
1072        deserialize_with = "crate::config::elements::deserialize_slow_timeout"
1073    )]
1074    slow_timeout: Option<SlowTimeout>,
1075}
1076
1077#[derive(Clone, Debug, Default)]
1078pub(in crate::config) struct PlatformStrings {
1079    pub(in crate::config) host: Option<String>,
1080    pub(in crate::config) target: Option<String>,
1081}
1082
1083#[cfg(feature = "config-schema")]
1084impl schemars::JsonSchema for PlatformStrings {
1085    fn schema_name() -> std::borrow::Cow<'static, str> {
1086        "PlatformStrings".into()
1087    }
1088
1089    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
1090        schemars::json_schema!({
1091            "oneOf": [
1092                generator.subschema_for::<String>(),
1093                {
1094                    "type": "object",
1095                    "properties": {
1096                        "host": {
1097                            "type": ["string", "null"],
1098                        },
1099                        "target": {
1100                            "type": ["string", "null"],
1101                        },
1102                    },
1103                    "additionalProperties": false,
1104                }
1105            ]
1106        })
1107    }
1108}
1109
1110impl<'de> Deserialize<'de> for PlatformStrings {
1111    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1112        struct V;
1113
1114        impl<'de2> serde::de::Visitor<'de2> for V {
1115            type Value = PlatformStrings;
1116
1117            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
1118                formatter.write_str(
1119                    "a table ({ host = \"x86_64-apple-darwin\", \
1120                        target = \"cfg(windows)\" }) \
1121                        or a string (\"x86_64-unknown-gnu-linux\")",
1122                )
1123            }
1124
1125            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
1126            where
1127                E: serde::de::Error,
1128            {
1129                Ok(PlatformStrings {
1130                    host: None,
1131                    target: Some(v.to_owned()),
1132                })
1133            }
1134
1135            fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
1136            where
1137                A: serde::de::MapAccess<'de2>,
1138            {
1139                #[derive(Deserialize)]
1140                struct PlatformStringsInner {
1141                    #[serde(default)]
1142                    host: Option<String>,
1143                    #[serde(default)]
1144                    target: Option<String>,
1145                }
1146
1147                let inner = PlatformStringsInner::deserialize(
1148                    serde::de::value::MapAccessDeserializer::new(map),
1149                )?;
1150                Ok(PlatformStrings {
1151                    host: inner.host,
1152                    target: inner.target,
1153                })
1154            }
1155        }
1156
1157        deserializer.deserialize_any(V)
1158    }
1159}
1160
1161#[cfg(test)]
1162mod tests {
1163    use super::*;
1164    use crate::config::{
1165        core::NextestConfig,
1166        elements::{LeakTimeoutResult, SlowTimeoutResult},
1167        utils::test_helpers::*,
1168    };
1169    use camino_tempfile::tempdir;
1170    use indoc::indoc;
1171    use nextest_metadata::TestCaseName;
1172    use std::{num::NonZeroUsize, time::Duration};
1173    use test_case::test_case;
1174
1175    /// Basic test to ensure overrides work. Add new override parameters to this test.
1176    #[test]
1177    fn test_overrides_basic() {
1178        let config_contents = indoc! {r#"
1179            # Override 1
1180            [[profile.default.overrides]]
1181            platform = 'aarch64-apple-darwin'  # this is the target platform
1182            filter = "test(test)"
1183            retries = { backoff = "exponential", count = 20, delay = "1s", max-delay = "20s" }
1184            slow-timeout = { period = "120s", terminate-after = 1, grace-period = "0s" }
1185            success-output = "immediate-final"
1186            junit = { store-success-output = true }
1187
1188            # Override 2
1189            [[profile.default.overrides]]
1190            filter = "test(test)"
1191            threads-required = 8
1192            retries = 3
1193            slow-timeout = "60s"
1194            leak-timeout = "300ms"
1195            test-group = "my-group"
1196            failure-output = "final"
1197            junit = { store-failure-output = false }
1198
1199            # Override 3
1200            [[profile.default.overrides]]
1201            platform = { host = "cfg(unix)" }
1202            filter = "test(override3)"
1203            retries = 5
1204
1205            # Override 4 -- host not matched
1206            [[profile.default.overrides]]
1207            platform = { host = 'aarch64-apple-darwin' }
1208            retries = 10
1209
1210            # Override 5 -- no filter provided, just platform
1211            [[profile.default.overrides]]
1212            platform = { host = 'cfg(target_os = "linux")', target = 'aarch64-apple-darwin' }
1213            filter = "test(override5)"
1214            retries = 8
1215
1216            # Override 6 -- timeout result success
1217            [[profile.default.overrides]]
1218            filter = "test(timeout_success)"
1219            slow-timeout = { period = "30s", on-timeout = "pass" }
1220
1221            [profile.default.junit]
1222            path = "my-path.xml"
1223
1224            [test-groups.my-group]
1225            max-threads = 20
1226        "#};
1227
1228        let workspace_dir = tempdir().unwrap();
1229
1230        let graph = temp_workspace(&workspace_dir, config_contents);
1231        let package_id = graph.workspace().iter().next().unwrap().id();
1232
1233        let pcx = ParseContext::new(&graph);
1234
1235        let nextest_config_result = NextestConfig::from_sources(
1236            graph.workspace().root(),
1237            &pcx,
1238            None,
1239            &[][..],
1240            &Default::default(),
1241        )
1242        .expect("config is valid");
1243        let profile = nextest_config_result
1244            .profile("default")
1245            .expect("valid profile name")
1246            .apply_build_platforms(&build_platforms());
1247
1248        // This query matches override 2.
1249        let host_binary_query =
1250            binary_query(&graph, package_id, "lib", "my-binary", BuildPlatform::Host);
1251        let test_name = TestCaseName::new("test");
1252        let query = TestQuery {
1253            binary_query: host_binary_query.to_query(),
1254            test_name: &test_name,
1255        };
1256        let overrides = profile.settings_for(NextestRunMode::Test, &query);
1257
1258        assert_eq!(overrides.threads_required(), ThreadsRequired::Count(8));
1259        assert_eq!(overrides.retries(), RetryPolicy::new_without_delay(3));
1260        assert_eq!(
1261            overrides.slow_timeout(),
1262            SlowTimeout {
1263                period: Duration::from_secs(60),
1264                on_timeout: SlowTimeoutResult::default(),
1265                terminate_after: None,
1266                grace_period: Duration::from_secs(10),
1267            }
1268        );
1269        assert_eq!(
1270            overrides.leak_timeout(),
1271            LeakTimeout {
1272                period: Duration::from_millis(300),
1273                result: LeakTimeoutResult::Pass,
1274            }
1275        );
1276        assert_eq!(overrides.test_group(), &test_group("my-group"));
1277        assert_eq!(overrides.success_output(), TestOutputDisplay::Never);
1278        assert_eq!(overrides.failure_output(), TestOutputDisplay::Final);
1279        // For clarity.
1280        #[expect(clippy::bool_assert_comparison)]
1281        {
1282            assert_eq!(overrides.junit_store_success_output(), false);
1283            assert_eq!(overrides.junit_store_failure_output(), false);
1284        }
1285
1286        // This query matches override 1 and 2.
1287        let target_binary_query = binary_query(
1288            &graph,
1289            package_id,
1290            "lib",
1291            "my-binary",
1292            BuildPlatform::Target,
1293        );
1294        let test_name = TestCaseName::new("test");
1295        let query = TestQuery {
1296            binary_query: target_binary_query.to_query(),
1297            test_name: &test_name,
1298        };
1299        let overrides = profile.settings_for(NextestRunMode::Test, &query);
1300
1301        assert_eq!(overrides.threads_required(), ThreadsRequired::Count(8));
1302        assert_eq!(
1303            overrides.retries(),
1304            RetryPolicy::Exponential {
1305                count: 20,
1306                delay: Duration::from_secs(1),
1307                jitter: false,
1308                max_delay: Some(Duration::from_secs(20)),
1309            }
1310        );
1311        assert_eq!(
1312            overrides.slow_timeout(),
1313            SlowTimeout {
1314                period: Duration::from_secs(120),
1315                terminate_after: Some(NonZeroUsize::new(1).unwrap()),
1316                grace_period: Duration::ZERO,
1317                on_timeout: SlowTimeoutResult::default(),
1318            }
1319        );
1320        assert_eq!(
1321            overrides.leak_timeout(),
1322            LeakTimeout {
1323                period: Duration::from_millis(300),
1324                result: LeakTimeoutResult::Pass,
1325            }
1326        );
1327        assert_eq!(overrides.test_group(), &test_group("my-group"));
1328        assert_eq!(
1329            overrides.success_output(),
1330            TestOutputDisplay::ImmediateFinal
1331        );
1332        assert_eq!(overrides.failure_output(), TestOutputDisplay::Final);
1333        // For clarity.
1334        #[expect(clippy::bool_assert_comparison)]
1335        {
1336            assert_eq!(overrides.junit_store_success_output(), true);
1337            assert_eq!(overrides.junit_store_failure_output(), false);
1338        }
1339
1340        // This query matches override 3.
1341        let test_name = TestCaseName::new("override3");
1342        let query = TestQuery {
1343            binary_query: target_binary_query.to_query(),
1344            test_name: &test_name,
1345        };
1346        let overrides = profile.settings_for(NextestRunMode::Test, &query);
1347        assert_eq!(overrides.retries(), RetryPolicy::new_without_delay(5));
1348
1349        // This query matches override 5.
1350        let test_name = TestCaseName::new("override5");
1351        let query = TestQuery {
1352            binary_query: target_binary_query.to_query(),
1353            test_name: &test_name,
1354        };
1355        let overrides = profile.settings_for(NextestRunMode::Test, &query);
1356        assert_eq!(overrides.retries(), RetryPolicy::new_without_delay(8));
1357
1358        // This query matches override 6.
1359        let test_name = TestCaseName::new("timeout_success");
1360        let query = TestQuery {
1361            binary_query: target_binary_query.to_query(),
1362            test_name: &test_name,
1363        };
1364        let overrides = profile.settings_for(NextestRunMode::Test, &query);
1365        assert_eq!(
1366            overrides.slow_timeout(),
1367            SlowTimeout {
1368                period: Duration::from_secs(30),
1369                on_timeout: SlowTimeoutResult::Pass,
1370                terminate_after: None,
1371                grace_period: Duration::from_secs(10),
1372            }
1373        );
1374
1375        // This query does not match any overrides.
1376        let test_name = TestCaseName::new("no_match");
1377        let query = TestQuery {
1378            binary_query: target_binary_query.to_query(),
1379            test_name: &test_name,
1380        };
1381        let overrides = profile.settings_for(NextestRunMode::Test, &query);
1382        assert_eq!(overrides.retries(), RetryPolicy::new_without_delay(0));
1383    }
1384
1385    /// Test that bench.slow-timeout works correctly in overrides.
1386    #[test]
1387    fn test_overrides_bench_slow_timeout() {
1388        let config_contents = indoc! {r#"
1389            # Profile-level benchmark slow-timeout (used as fallback).
1390            [profile.default]
1391            bench.slow-timeout = { period = "30y" }
1392
1393            # Override 1: Both test and bench slow-timeout specified.
1394            [[profile.default.overrides]]
1395            filter = "test(both_specified)"
1396            slow-timeout = "60s"
1397            bench.slow-timeout = { period = "5m", terminate-after = 2 }
1398
1399            # Override 2: Only test slow-timeout specified.
1400            [[profile.default.overrides]]
1401            filter = "test(test_only)"
1402            slow-timeout = "90s"
1403
1404            # Override 3: Only bench slow-timeout specified.
1405            [[profile.default.overrides]]
1406            filter = "test(bench_only)"
1407            bench.slow-timeout = "10m"
1408        "#};
1409
1410        let workspace_dir = tempdir().unwrap();
1411        let graph = temp_workspace(&workspace_dir, config_contents);
1412        let package_id = graph.workspace().iter().next().unwrap().id();
1413        let pcx = ParseContext::new(&graph);
1414
1415        let nextest_config_result = NextestConfig::from_sources(
1416            graph.workspace().root(),
1417            &pcx,
1418            None,
1419            &[][..],
1420            &Default::default(),
1421        )
1422        .expect("config is valid");
1423        let profile = nextest_config_result
1424            .profile("default")
1425            .expect("valid profile name")
1426            .apply_build_platforms(&build_platforms());
1427
1428        let host_binary_query =
1429            binary_query(&graph, package_id, "lib", "my-binary", BuildPlatform::Host);
1430
1431        // Test "both_specified": tests get slow-timeout, benchmarks get
1432        // bench.slow-timeout.
1433        let test_name = TestCaseName::new("both_specified");
1434        let query = TestQuery {
1435            binary_query: host_binary_query.to_query(),
1436            test_name: &test_name,
1437        };
1438
1439        let test_settings = profile.settings_for(NextestRunMode::Test, &query);
1440        assert_eq!(test_settings.slow_timeout().period, Duration::from_secs(60));
1441
1442        let bench_settings = profile.settings_for(NextestRunMode::Benchmark, &query);
1443        assert_eq!(
1444            bench_settings.slow_timeout(),
1445            SlowTimeout {
1446                period: Duration::from_secs(5 * 60),
1447                terminate_after: Some(NonZeroUsize::new(2).unwrap()),
1448                grace_period: Duration::from_secs(10),
1449                on_timeout: SlowTimeoutResult::default(),
1450            }
1451        );
1452
1453        // Test "test_only": tests get the override, benchmarks fall back to
1454        // profile default (no fallback from slow-timeout to
1455        // bench.slow-timeout).
1456        let test_name = TestCaseName::new("test_only");
1457        let query = TestQuery {
1458            binary_query: host_binary_query.to_query(),
1459            test_name: &test_name,
1460        };
1461
1462        let test_settings = profile.settings_for(NextestRunMode::Test, &query);
1463        assert_eq!(test_settings.slow_timeout().period, Duration::from_secs(90));
1464
1465        let bench_settings = profile.settings_for(NextestRunMode::Benchmark, &query);
1466        // Should use profile-level bench.slow-timeout (30 years), not the
1467        // override's slow-timeout. humantime parses "30y" accounting for leap
1468        // years, so we check >= VERY_LARGE rather than an exact value.
1469        assert!(
1470            bench_settings.slow_timeout().period >= SlowTimeout::VERY_LARGE.period,
1471            "should be >= VERY_LARGE, got {:?}",
1472            bench_settings.slow_timeout().period
1473        );
1474
1475        // Test "bench_only": tests get profile default, benchmarks get the
1476        // override.
1477        let test_name = TestCaseName::new("bench_only");
1478        let query = TestQuery {
1479            binary_query: host_binary_query.to_query(),
1480            test_name: &test_name,
1481        };
1482
1483        let test_settings = profile.settings_for(NextestRunMode::Test, &query);
1484        // Tests use the default slow-timeout (60s from default-config.toml).
1485        assert_eq!(test_settings.slow_timeout().period, Duration::from_secs(60));
1486
1487        let bench_settings = profile.settings_for(NextestRunMode::Benchmark, &query);
1488        assert_eq!(
1489            bench_settings.slow_timeout().period,
1490            Duration::from_secs(10 * 60)
1491        );
1492    }
1493
1494    #[test_case(
1495        indoc! {r#"
1496            [[profile.default.overrides]]
1497            retries = 2
1498        "#},
1499        "default",
1500        &[MietteJsonReport {
1501            message: "at least one of `platform` and `filter` must be specified".to_owned(),
1502            labels: vec![],
1503        }]
1504
1505        ; "neither platform nor filter specified"
1506    )]
1507    #[test_case(
1508        indoc! {r#"
1509            [[profile.default.overrides]]
1510            default-filter = "test(test1)"
1511            retries = 2
1512        "#},
1513        "default",
1514        &[MietteJsonReport {
1515            message: "for override with `default-filter`, `platform` must also be specified".to_owned(),
1516            labels: vec![],
1517        }]
1518
1519        ; "default-filter without platform"
1520    )]
1521    #[test_case(
1522        indoc! {r#"
1523            [[profile.default.overrides]]
1524            platform = 'cfg(unix)'
1525            default-filter = "not default()"
1526            retries = 2
1527        "#},
1528        "default",
1529        &[MietteJsonReport {
1530            message: "predicate not allowed in `default-filter` expressions".to_owned(),
1531            labels: vec![
1532                MietteJsonLabel {
1533                    label: "default() causes infinite recursion".to_owned(),
1534                    span: MietteJsonSpan { offset: 4, length: 9 },
1535                },
1536            ],
1537        }]
1538
1539        ; "default filterset in default-filter"
1540    )]
1541    #[test_case(
1542        indoc! {r#"
1543            [[profile.default.overrides]]
1544            filter = 'test(test1)'
1545            default-filter = "test(test2)"
1546            retries = 2
1547        "#},
1548        "default",
1549        &[MietteJsonReport {
1550            message: "at most one of `filter` and `default-filter` must be specified".to_owned(),
1551            labels: vec![],
1552        }]
1553
1554        ; "both filter and default-filter specified"
1555    )]
1556    #[test_case(
1557        indoc! {r#"
1558            [[profile.default.overrides]]
1559            filter = 'test(test1)'
1560            platform = 'cfg(unix)'
1561            default-filter = "test(test2)"
1562            retries = 2
1563        "#},
1564        "default",
1565        &[MietteJsonReport {
1566            message: "at most one of `filter` and `default-filter` must be specified".to_owned(),
1567            labels: vec![],
1568        }]
1569
1570        ; "both filter and default-filter specified with platform"
1571    )]
1572    #[test_case(
1573        indoc! {r#"
1574            [[profile.default.overrides]]
1575            platform = {}
1576            retries = 2
1577        "#},
1578        "default",
1579        &[MietteJsonReport {
1580            message: "at least one of `platform` and `filter` must be specified".to_owned(),
1581            labels: vec![],
1582        }]
1583
1584        ; "empty platform map"
1585    )]
1586    #[test_case(
1587        indoc! {r#"
1588            [[profile.ci.overrides]]
1589            platform = 'cfg(target_os = "macos)'
1590            retries = 2
1591        "#},
1592        "ci",
1593        &[MietteJsonReport {
1594            message: "error parsing cfg() expression".to_owned(),
1595            labels: vec![
1596                MietteJsonLabel { label: "unclosed quotes".to_owned(), span: MietteJsonSpan { offset: 16, length: 6 } }
1597            ]
1598        }]
1599
1600        ; "invalid platform expression"
1601    )]
1602    #[test_case(
1603        indoc! {r#"
1604            [[profile.ci.overrides]]
1605            filter = 'test(/foo)'
1606            retries = 2
1607        "#},
1608        "ci",
1609        &[MietteJsonReport {
1610            message: "expected close regex".to_owned(),
1611            labels: vec![
1612                MietteJsonLabel { label: "missing `/`".to_owned(), span: MietteJsonSpan { offset: 9, length: 0 } }
1613            ]
1614        }]
1615
1616        ; "invalid filterset"
1617    )]
1618    #[test_case(
1619        // Not strictly an override error, but convenient to put here.
1620        indoc! {r#"
1621            [profile.ci]
1622            default-filter = "test(foo) or default()"
1623        "#},
1624        "ci",
1625        &[MietteJsonReport {
1626            message: "predicate not allowed in `default-filter` expressions".to_owned(),
1627            labels: vec![
1628                MietteJsonLabel { label: "default() causes infinite recursion".to_owned(), span: MietteJsonSpan { offset: 13, length: 9 } }
1629            ]
1630        }]
1631
1632        ; "default-filter with default"
1633    )]
1634    fn parse_overrides_invalid(
1635        config_contents: &str,
1636        faulty_profile: &str,
1637        expected_reports: &[MietteJsonReport],
1638    ) {
1639        let workspace_dir = tempdir().unwrap();
1640
1641        let graph = temp_workspace(&workspace_dir, config_contents);
1642        let pcx = ParseContext::new(&graph);
1643
1644        let err = NextestConfig::from_sources(
1645            graph.workspace().root(),
1646            &pcx,
1647            None,
1648            [],
1649            &Default::default(),
1650        )
1651        .expect_err("config is invalid");
1652        match err.kind() {
1653            ConfigParseErrorKind::CompileErrors(compile_errors) => {
1654                assert_eq!(
1655                    compile_errors.len(),
1656                    1,
1657                    "exactly one override error must be produced"
1658                );
1659                let error = compile_errors.first().unwrap();
1660                assert_eq!(
1661                    error.profile_name, faulty_profile,
1662                    "compile error profile matches"
1663                );
1664                let handler = miette::JSONReportHandler::new();
1665                let reports = error
1666                    .kind
1667                    .reports()
1668                    .map(|report| {
1669                        let mut out = String::new();
1670                        handler.render_report(&mut out, report.as_ref()).unwrap();
1671
1672                        let json_report: MietteJsonReport = serde_json::from_str(&out)
1673                            .unwrap_or_else(|err| {
1674                                panic!(
1675                                    "failed to deserialize JSON message produced by miette: {err}"
1676                                )
1677                            });
1678                        json_report
1679                    })
1680                    .collect::<Vec<_>>();
1681                assert_eq!(&reports, expected_reports, "reports match");
1682            }
1683            other => {
1684                panic!(
1685                    "for config error {other:?}, expected ConfigParseErrorKind::FiltersetOrCfgParseError"
1686                );
1687            }
1688        };
1689    }
1690
1691    /// Test that `cfg(unix)` works with a custom platform.
1692    ///
1693    /// This was broken with older versions of target-spec.
1694    #[test]
1695    fn cfg_unix_with_custom_platform() {
1696        let config_contents = indoc! {r#"
1697            [[profile.default.overrides]]
1698            platform = { host = "cfg(unix)" }
1699            filter = "test(test)"
1700            retries = 5
1701        "#};
1702
1703        let workspace_dir = tempdir().unwrap();
1704
1705        let graph = temp_workspace(&workspace_dir, config_contents);
1706        let package_id = graph.workspace().iter().next().unwrap().id();
1707        let pcx = ParseContext::new(&graph);
1708
1709        let nextest_config = NextestConfig::from_sources(
1710            graph.workspace().root(),
1711            &pcx,
1712            None,
1713            &[][..],
1714            &Default::default(),
1715        )
1716        .expect("config is valid");
1717
1718        let build_platforms = custom_build_platforms(workspace_dir.path());
1719
1720        let profile = nextest_config
1721            .profile("default")
1722            .expect("valid profile name")
1723            .apply_build_platforms(&build_platforms);
1724
1725        // Check that the override is correctly applied.
1726        let target_binary_query = binary_query(
1727            &graph,
1728            package_id,
1729            "lib",
1730            "my-binary",
1731            BuildPlatform::Target,
1732        );
1733        let test_name = TestCaseName::new("test");
1734        let query = TestQuery {
1735            binary_query: target_binary_query.to_query(),
1736            test_name: &test_name,
1737        };
1738        let overrides = profile.settings_for(NextestRunMode::Test, &query);
1739        assert_eq!(
1740            overrides.retries(),
1741            RetryPolicy::new_without_delay(5),
1742            "retries applied to custom platform"
1743        );
1744    }
1745}