nextest_runner/config/
config_impl.rs

1// Copyright (c) The nextest Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use super::{
5    ArchiveConfig, CompiledByProfile, CompiledData, CompiledDefaultFilter, ConfigExperimental,
6    CustomTestGroup, DefaultJunitImpl, DeserializedOverride, DeserializedProfileScriptConfig,
7    GlobalTimeout, JunitConfig, JunitImpl, MaxFail, NextestVersionDeserialize, RetryPolicy,
8    ScriptConfig, ScriptId, SettingSource, SetupScripts, SlowTimeout, TestGroup, TestGroupConfig,
9    TestSettings, TestThreads, ThreadsRequired, ToolConfigFile, leak_timeout::LeakTimeout,
10};
11use crate::{
12    config::{ListSettings, ProfileScriptType, ScriptInfo, SetupScriptConfig},
13    errors::{
14        ConfigParseError, ConfigParseErrorKind, ProfileListScriptUsesRunFiltersError,
15        ProfileNotFound, ProfileScriptErrors, ProfileUnknownScriptError,
16        ProfileWrongConfigScriptTypeError, UnknownTestGroupError, provided_by_tool,
17    },
18    helpers::plural,
19    list::TestList,
20    platform::BuildPlatforms,
21    reporter::{FinalStatusLevel, StatusLevel, TestOutputDisplay},
22};
23use camino::{Utf8Path, Utf8PathBuf};
24use config::{
25    Config, ConfigBuilder, ConfigError, File, FileFormat, FileSourceFile, builder::DefaultState,
26};
27use iddqd::IdOrdMap;
28use indexmap::IndexMap;
29use nextest_filtering::{BinaryQuery, EvalContext, Filterset, ParseContext, TestQuery};
30use serde::Deserialize;
31use std::{
32    collections::{BTreeMap, BTreeSet, HashMap, hash_map},
33    sync::LazyLock,
34};
35use tracing::warn;
36
37/// Trait for handling configuration warnings.
38///
39/// This trait allows for different warning handling strategies, such as logging warnings
40/// (the default behavior) or collecting them for testing purposes.
41pub trait ConfigWarnings {
42    /// Handle unknown configuration keys found in a config file.
43    fn unknown_config_keys(
44        &mut self,
45        config_file: &Utf8Path,
46        workspace_root: &Utf8Path,
47        tool: Option<&str>,
48        unknown: &BTreeSet<String>,
49    );
50
51    /// Handle unknown profiles found in the reserved `default-` namespace.
52    fn unknown_reserved_profiles(
53        &mut self,
54        config_file: &Utf8Path,
55        workspace_root: &Utf8Path,
56        tool: Option<&str>,
57        profiles: &[&str],
58    );
59
60    /// Handle deprecated `[script.*]` configuration.
61    fn deprecated_script_config(
62        &mut self,
63        config_file: &Utf8Path,
64        workspace_root: &Utf8Path,
65        tool: Option<&str>,
66    );
67
68    /// Handle warning about empty script sections with neither setup nor
69    /// wrapper scripts.
70    fn empty_script_sections(
71        &mut self,
72        config_file: &Utf8Path,
73        workspace_root: &Utf8Path,
74        tool: Option<&str>,
75        profile_name: &str,
76        empty_count: usize,
77    );
78}
79
80/// Default implementation of ConfigWarnings that logs warnings using the tracing crate.
81pub struct DefaultConfigWarnings;
82
83impl ConfigWarnings for DefaultConfigWarnings {
84    fn unknown_config_keys(
85        &mut self,
86        config_file: &Utf8Path,
87        workspace_root: &Utf8Path,
88        tool: Option<&str>,
89        unknown: &BTreeSet<String>,
90    ) {
91        let mut unknown_str = String::new();
92        if unknown.len() == 1 {
93            // Print this on the same line.
94            unknown_str.push(' ');
95            unknown_str.push_str(unknown.iter().next().unwrap());
96        } else {
97            for ignored_key in unknown {
98                unknown_str.push('\n');
99                unknown_str.push_str("  - ");
100                unknown_str.push_str(ignored_key);
101            }
102        }
103
104        warn!(
105            "in config file {}{}, ignoring unknown configuration keys: {unknown_str}",
106            config_file
107                .strip_prefix(workspace_root)
108                .unwrap_or(config_file),
109            provided_by_tool(tool),
110        )
111    }
112
113    fn unknown_reserved_profiles(
114        &mut self,
115        config_file: &Utf8Path,
116        workspace_root: &Utf8Path,
117        tool: Option<&str>,
118        profiles: &[&str],
119    ) {
120        warn!(
121            "in config file {}{}, ignoring unknown profiles in the reserved `default-` namespace:",
122            config_file
123                .strip_prefix(workspace_root)
124                .unwrap_or(config_file),
125            provided_by_tool(tool),
126        );
127
128        for profile in profiles {
129            warn!("  {profile}");
130        }
131    }
132
133    fn deprecated_script_config(
134        &mut self,
135        config_file: &Utf8Path,
136        workspace_root: &Utf8Path,
137        tool: Option<&str>,
138    ) {
139        warn!(
140            "in config file {}{}, [script.*] is deprecated and will be removed in a \
141             future version of nextest; use the `scripts.setup` table instead",
142            config_file
143                .strip_prefix(workspace_root)
144                .unwrap_or(config_file),
145            provided_by_tool(tool),
146        );
147    }
148
149    fn empty_script_sections(
150        &mut self,
151        config_file: &Utf8Path,
152        workspace_root: &Utf8Path,
153        tool: Option<&str>,
154        profile_name: &str,
155        empty_count: usize,
156    ) {
157        warn!(
158            "in config file {}{}, [[profile.{}.scripts]] has {} {} \
159             with neither setup nor wrapper scripts",
160            config_file
161                .strip_prefix(workspace_root)
162                .unwrap_or(config_file),
163            provided_by_tool(tool),
164            profile_name,
165            empty_count,
166            plural::sections_str(empty_count),
167        );
168    }
169}
170
171/// Gets the number of available CPUs and caches the value.
172#[inline]
173pub fn get_num_cpus() -> usize {
174    static NUM_CPUS: LazyLock<usize> =
175        LazyLock::new(|| match std::thread::available_parallelism() {
176            Ok(count) => count.into(),
177            Err(err) => {
178                warn!("unable to determine num-cpus ({err}), assuming 1 logical CPU");
179                1
180            }
181        });
182
183    *NUM_CPUS
184}
185
186/// Overall configuration for nextest.
187///
188/// This is the root data structure for nextest configuration. Most runner-specific configuration is
189/// managed through [profiles](EvaluatableProfile), obtained through the [`profile`](Self::profile)
190/// method.
191///
192/// For more about configuration, see [_Configuration_](https://nexte.st/docs/configuration) in the
193/// nextest book.
194#[derive(Clone, Debug)]
195pub struct NextestConfig {
196    workspace_root: Utf8PathBuf,
197    inner: NextestConfigImpl,
198    compiled: CompiledByProfile,
199}
200
201impl NextestConfig {
202    /// The default location of the config within the path: `.config/nextest.toml`, used to read the
203    /// config from the given directory.
204    pub const CONFIG_PATH: &'static str = ".config/nextest.toml";
205
206    /// Contains the default config as a TOML file.
207    ///
208    /// Repository-specific configuration is layered on top of the default config.
209    pub const DEFAULT_CONFIG: &'static str = include_str!("../../default-config.toml");
210
211    /// Environment configuration uses this prefix, plus a _.
212    pub const ENVIRONMENT_PREFIX: &'static str = "NEXTEST";
213
214    /// The name of the default profile.
215    pub const DEFAULT_PROFILE: &'static str = "default";
216
217    /// The name of the default profile used for miri.
218    pub const DEFAULT_MIRI_PROFILE: &'static str = "default-miri";
219
220    /// A list containing the names of the Nextest defined reserved profile names.
221    pub const DEFAULT_PROFILES: &'static [&'static str] =
222        &[Self::DEFAULT_PROFILE, Self::DEFAULT_MIRI_PROFILE];
223
224    /// Reads the nextest config from the given file, or if not specified from `.config/nextest.toml`
225    /// in the workspace root.
226    ///
227    /// `tool_config_files` are lower priority than `config_file` but higher priority than the
228    /// default config. Files in `tool_config_files` that come earlier are higher priority than those
229    /// that come later.
230    ///
231    /// If no config files are specified and this file doesn't have `.config/nextest.toml`, uses the
232    /// default config options.
233    pub fn from_sources<'a, I>(
234        workspace_root: impl Into<Utf8PathBuf>,
235        pcx: &ParseContext<'_>,
236        config_file: Option<&Utf8Path>,
237        tool_config_files: impl IntoIterator<IntoIter = I>,
238        experimental: &BTreeSet<ConfigExperimental>,
239    ) -> Result<Self, ConfigParseError>
240    where
241        I: Iterator<Item = &'a ToolConfigFile> + DoubleEndedIterator,
242    {
243        Self::from_sources_with_warnings(
244            workspace_root,
245            pcx,
246            config_file,
247            tool_config_files,
248            experimental,
249            &mut DefaultConfigWarnings,
250        )
251    }
252
253    /// Load configuration from the given sources with custom warning handling.
254    pub fn from_sources_with_warnings<'a, I>(
255        workspace_root: impl Into<Utf8PathBuf>,
256        pcx: &ParseContext<'_>,
257        config_file: Option<&Utf8Path>,
258        tool_config_files: impl IntoIterator<IntoIter = I>,
259        experimental: &BTreeSet<ConfigExperimental>,
260        warnings: &mut impl ConfigWarnings,
261    ) -> Result<Self, ConfigParseError>
262    where
263        I: Iterator<Item = &'a ToolConfigFile> + DoubleEndedIterator,
264    {
265        Self::from_sources_impl(
266            workspace_root,
267            pcx,
268            config_file,
269            tool_config_files,
270            experimental,
271            warnings,
272        )
273    }
274
275    // A custom unknown_callback can be passed in while testing.
276    fn from_sources_impl<'a, I>(
277        workspace_root: impl Into<Utf8PathBuf>,
278        pcx: &ParseContext<'_>,
279        config_file: Option<&Utf8Path>,
280        tool_config_files: impl IntoIterator<IntoIter = I>,
281        experimental: &BTreeSet<ConfigExperimental>,
282        warnings: &mut impl ConfigWarnings,
283    ) -> Result<Self, ConfigParseError>
284    where
285        I: Iterator<Item = &'a ToolConfigFile> + DoubleEndedIterator,
286    {
287        let workspace_root = workspace_root.into();
288        let tool_config_files_rev = tool_config_files.into_iter().rev();
289        let (inner, compiled) = Self::read_from_sources(
290            pcx,
291            &workspace_root,
292            config_file,
293            tool_config_files_rev,
294            experimental,
295            warnings,
296        )?;
297        Ok(Self {
298            workspace_root,
299            inner,
300            compiled,
301        })
302    }
303
304    /// Returns the default nextest config.
305    #[cfg(test)]
306    pub(crate) fn default_config(workspace_root: impl Into<Utf8PathBuf>) -> Self {
307        use itertools::Itertools;
308
309        let config = Self::make_default_config()
310            .build()
311            .expect("default config is always valid");
312
313        let mut unknown = BTreeSet::new();
314        let deserialized: NextestConfigDeserialize =
315            serde_ignored::deserialize(config, |path: serde_ignored::Path| {
316                unknown.insert(path.to_string());
317            })
318            .expect("default config is always valid");
319
320        // Make sure there aren't any unknown keys in the default config, since it is
321        // embedded/shipped with this binary.
322        if !unknown.is_empty() {
323            panic!(
324                "found unknown keys in default config: {}",
325                unknown.iter().join(", ")
326            );
327        }
328
329        Self {
330            workspace_root: workspace_root.into(),
331            inner: deserialized.into_config_impl(),
332            // The default config has no overrides or special settings.
333            compiled: CompiledByProfile::for_default_config(),
334        }
335    }
336
337    /// Returns the profile with the given name, or an error if a profile was
338    /// specified but not found.
339    pub fn profile(&self, name: impl AsRef<str>) -> Result<EarlyProfile<'_>, ProfileNotFound> {
340        self.make_profile(name.as_ref())
341    }
342
343    // ---
344    // Helper methods
345    // ---
346
347    fn read_from_sources<'a>(
348        pcx: &ParseContext<'_>,
349        workspace_root: &Utf8Path,
350        file: Option<&Utf8Path>,
351        tool_config_files_rev: impl Iterator<Item = &'a ToolConfigFile>,
352        experimental: &BTreeSet<ConfigExperimental>,
353        warnings: &mut impl ConfigWarnings,
354    ) -> Result<(NextestConfigImpl, CompiledByProfile), ConfigParseError> {
355        // First, get the default config.
356        let mut composite_builder = Self::make_default_config();
357
358        // Overrides are handled additively.
359        // Note that they're stored in reverse order here, and are flipped over at the end.
360        let mut compiled = CompiledByProfile::for_default_config();
361
362        let mut known_groups = BTreeSet::new();
363        let mut known_scripts = IdOrdMap::new();
364
365        // Next, merge in tool configs.
366        for ToolConfigFile { config_file, tool } in tool_config_files_rev {
367            let source = File::new(config_file.as_str(), FileFormat::Toml);
368            Self::deserialize_individual_config(
369                pcx,
370                workspace_root,
371                config_file,
372                Some(tool),
373                source.clone(),
374                &mut compiled,
375                experimental,
376                warnings,
377                &mut known_groups,
378                &mut known_scripts,
379            )?;
380
381            // This is the final, composite builder used at the end.
382            composite_builder = composite_builder.add_source(source);
383        }
384
385        // Next, merge in the config from the given file.
386        let (config_file, source) = match file {
387            Some(file) => (file.to_owned(), File::new(file.as_str(), FileFormat::Toml)),
388            None => {
389                let config_file = workspace_root.join(Self::CONFIG_PATH);
390                let source = File::new(config_file.as_str(), FileFormat::Toml).required(false);
391                (config_file, source)
392            }
393        };
394
395        Self::deserialize_individual_config(
396            pcx,
397            workspace_root,
398            &config_file,
399            None,
400            source.clone(),
401            &mut compiled,
402            experimental,
403            warnings,
404            &mut known_groups,
405            &mut known_scripts,
406        )?;
407
408        composite_builder = composite_builder.add_source(source);
409
410        // The unknown set is ignored here because any values in it have already been reported in
411        // deserialize_individual_config.
412        let (config, _unknown) = Self::build_and_deserialize_config(&composite_builder)
413            .map_err(|kind| ConfigParseError::new(config_file, None, kind))?;
414
415        // Reverse all the compiled data at the end.
416        compiled.default.reverse();
417        for data in compiled.other.values_mut() {
418            data.reverse();
419        }
420
421        Ok((config.into_config_impl(), compiled))
422    }
423
424    #[expect(clippy::too_many_arguments)]
425    fn deserialize_individual_config(
426        pcx: &ParseContext<'_>,
427        workspace_root: &Utf8Path,
428        config_file: &Utf8Path,
429        tool: Option<&str>,
430        source: File<FileSourceFile, FileFormat>,
431        compiled_out: &mut CompiledByProfile,
432        experimental: &BTreeSet<ConfigExperimental>,
433        warnings: &mut impl ConfigWarnings,
434        known_groups: &mut BTreeSet<CustomTestGroup>,
435        known_scripts: &mut IdOrdMap<ScriptInfo>,
436    ) -> Result<(), ConfigParseError> {
437        // Try building default builder + this file to get good error attribution and handle
438        // overrides additively.
439        let default_builder = Self::make_default_config();
440        let this_builder = default_builder.add_source(source);
441        let (mut this_config, unknown) = Self::build_and_deserialize_config(&this_builder)
442            .map_err(|kind| ConfigParseError::new(config_file, tool, kind))?;
443
444        if !unknown.is_empty() {
445            warnings.unknown_config_keys(config_file, workspace_root, tool, &unknown);
446        }
447
448        // Check that test groups are named as expected.
449        let (valid_groups, invalid_groups): (BTreeSet<_>, _) =
450            this_config.test_groups.keys().cloned().partition(|group| {
451                if let Some(tool) = tool {
452                    // The first component must be the tool name.
453                    group
454                        .as_identifier()
455                        .tool_components()
456                        .is_some_and(|(tool_name, _)| tool_name == tool)
457                } else {
458                    // If a tool is not specified, it must *not* be a tool identifier.
459                    !group.as_identifier().is_tool_identifier()
460                }
461            });
462
463        if !invalid_groups.is_empty() {
464            let kind = if tool.is_some() {
465                ConfigParseErrorKind::InvalidTestGroupsDefinedByTool(invalid_groups)
466            } else {
467                ConfigParseErrorKind::InvalidTestGroupsDefined(invalid_groups)
468            };
469            return Err(ConfigParseError::new(config_file, tool, kind));
470        }
471
472        known_groups.extend(valid_groups);
473
474        // If both scripts and old_setup_scripts are present, produce an error.
475        if !this_config.scripts.is_empty() && !this_config.old_setup_scripts.is_empty() {
476            return Err(ConfigParseError::new(
477                config_file,
478                tool,
479                ConfigParseErrorKind::BothScriptAndScriptsDefined,
480            ));
481        }
482
483        // If old_setup_scripts are present, produce a warning.
484        if !this_config.old_setup_scripts.is_empty() {
485            warnings.deprecated_script_config(config_file, workspace_root, tool);
486            this_config.scripts.setup = this_config.old_setup_scripts.clone();
487        }
488
489        // Check for experimental features that are used but not enabled.
490        {
491            let mut missing_features = BTreeSet::new();
492            if !this_config.scripts.setup.is_empty()
493                && !experimental.contains(&ConfigExperimental::SetupScripts)
494            {
495                missing_features.insert(ConfigExperimental::SetupScripts);
496            }
497            if !this_config.scripts.wrapper.is_empty()
498                && !experimental.contains(&ConfigExperimental::WrapperScripts)
499            {
500                missing_features.insert(ConfigExperimental::WrapperScripts);
501            }
502            if !missing_features.is_empty() {
503                return Err(ConfigParseError::new(
504                    config_file,
505                    tool,
506                    ConfigParseErrorKind::ExperimentalFeaturesNotEnabled { missing_features },
507                ));
508            }
509        }
510
511        let duplicate_ids: BTreeSet<_> = this_config.scripts.duplicate_ids().cloned().collect();
512        if !duplicate_ids.is_empty() {
513            return Err(ConfigParseError::new(
514                config_file,
515                tool,
516                ConfigParseErrorKind::DuplicateConfigScriptNames(duplicate_ids),
517            ));
518        }
519
520        // Check that setup scripts are named as expected.
521        let (valid_scripts, invalid_scripts): (BTreeSet<_>, _) = this_config
522            .scripts
523            .all_script_ids()
524            .cloned()
525            .partition(|script| {
526                if let Some(tool) = tool {
527                    // The first component must be the tool name.
528                    script
529                        .as_identifier()
530                        .tool_components()
531                        .is_some_and(|(tool_name, _)| tool_name == tool)
532                } else {
533                    // If a tool is not specified, it must *not* be a tool identifier.
534                    !script.as_identifier().is_tool_identifier()
535                }
536            });
537
538        if !invalid_scripts.is_empty() {
539            let kind = if tool.is_some() {
540                ConfigParseErrorKind::InvalidConfigScriptsDefinedByTool(invalid_scripts)
541            } else {
542                ConfigParseErrorKind::InvalidConfigScriptsDefined(invalid_scripts)
543            };
544            return Err(ConfigParseError::new(config_file, tool, kind));
545        }
546
547        known_scripts.extend(
548            valid_scripts
549                .into_iter()
550                .map(|id| this_config.scripts.script_info(id)),
551        );
552
553        let this_config = this_config.into_config_impl();
554
555        let unknown_default_profiles: Vec<_> = this_config
556            .all_profiles()
557            .filter(|p| p.starts_with("default-") && !NextestConfig::DEFAULT_PROFILES.contains(p))
558            .collect();
559        if !unknown_default_profiles.is_empty() {
560            warnings.unknown_reserved_profiles(
561                config_file,
562                workspace_root,
563                tool,
564                &unknown_default_profiles,
565            );
566        }
567
568        // Compile the overrides for this file.
569        let this_compiled = CompiledByProfile::new(pcx, &this_config)
570            .map_err(|kind| ConfigParseError::new(config_file, tool, kind))?;
571
572        // Check that all overrides specify known test groups.
573        let mut unknown_group_errors = Vec::new();
574        let mut check_test_group = |profile_name: &str, test_group: Option<&TestGroup>| {
575            if let Some(TestGroup::Custom(group)) = test_group {
576                if !known_groups.contains(group) {
577                    unknown_group_errors.push(UnknownTestGroupError {
578                        profile_name: profile_name.to_owned(),
579                        name: TestGroup::Custom(group.clone()),
580                    });
581                }
582            }
583        };
584
585        this_compiled
586            .default
587            .overrides
588            .iter()
589            .for_each(|override_| {
590                check_test_group("default", override_.data.test_group.as_ref());
591            });
592
593        // Check that override test groups are known.
594        this_compiled.other.iter().for_each(|(profile_name, data)| {
595            data.overrides.iter().for_each(|override_| {
596                check_test_group(profile_name, override_.data.test_group.as_ref());
597            });
598        });
599
600        // If there were any unknown groups, error out.
601        if !unknown_group_errors.is_empty() {
602            let known_groups = TestGroup::make_all_groups(known_groups.iter().cloned()).collect();
603            return Err(ConfigParseError::new(
604                config_file,
605                tool,
606                ConfigParseErrorKind::UnknownTestGroups {
607                    errors: unknown_group_errors,
608                    known_groups,
609                },
610            ));
611        }
612
613        // Check that scripts are known and that there aren't any other errors
614        // with them.
615        let mut profile_script_errors = ProfileScriptErrors::default();
616        let mut check_script_ids = |profile_name: &str,
617                                    script_type: ProfileScriptType,
618                                    expr: Option<&Filterset>,
619                                    scripts: &[ScriptId]| {
620            for script in scripts {
621                if let Some(script_info) = known_scripts.get(script) {
622                    if !script_info.script_type.matches(script_type) {
623                        profile_script_errors.wrong_script_types.push(
624                            ProfileWrongConfigScriptTypeError {
625                                profile_name: profile_name.to_owned(),
626                                name: script.clone(),
627                                attempted: script_type,
628                                actual: script_info.script_type,
629                            },
630                        );
631                    }
632                    if script_type == ProfileScriptType::ListWrapper {
633                        if let Some(expr) = expr {
634                            let runtime_only_leaves = expr.parsed.runtime_only_leaves();
635                            if !runtime_only_leaves.is_empty() {
636                                let filters = runtime_only_leaves
637                                    .iter()
638                                    .map(|leaf| leaf.to_string())
639                                    .collect();
640                                profile_script_errors.list_scripts_using_run_filters.push(
641                                    ProfileListScriptUsesRunFiltersError {
642                                        profile_name: profile_name.to_owned(),
643                                        name: script.clone(),
644                                        script_type,
645                                        filters,
646                                    },
647                                );
648                            }
649                        }
650                    }
651                } else {
652                    profile_script_errors
653                        .unknown_scripts
654                        .push(ProfileUnknownScriptError {
655                            profile_name: profile_name.to_owned(),
656                            name: script.clone(),
657                        });
658                }
659            }
660        };
661
662        let mut empty_script_count = 0;
663
664        this_compiled.default.scripts.iter().for_each(|scripts| {
665            if scripts.setup.is_empty()
666                && scripts.list_wrapper.is_none()
667                && scripts.run_wrapper.is_none()
668            {
669                empty_script_count += 1;
670            }
671
672            check_script_ids(
673                "default",
674                ProfileScriptType::Setup,
675                scripts.data.expr(),
676                &scripts.setup,
677            );
678            check_script_ids(
679                "default",
680                ProfileScriptType::ListWrapper,
681                scripts.data.expr(),
682                scripts.list_wrapper.as_slice(),
683            );
684            check_script_ids(
685                "default",
686                ProfileScriptType::RunWrapper,
687                scripts.data.expr(),
688                scripts.run_wrapper.as_slice(),
689            );
690        });
691
692        if empty_script_count > 0 {
693            warnings.empty_script_sections(
694                config_file,
695                workspace_root,
696                tool,
697                "default",
698                empty_script_count,
699            );
700        }
701
702        this_compiled.other.iter().for_each(|(profile_name, data)| {
703            let mut empty_script_count = 0;
704            data.scripts.iter().for_each(|scripts| {
705                if scripts.setup.is_empty()
706                    && scripts.list_wrapper.is_none()
707                    && scripts.run_wrapper.is_none()
708                {
709                    empty_script_count += 1;
710                }
711
712                check_script_ids(
713                    profile_name,
714                    ProfileScriptType::Setup,
715                    scripts.data.expr(),
716                    &scripts.setup,
717                );
718                check_script_ids(
719                    profile_name,
720                    ProfileScriptType::ListWrapper,
721                    scripts.data.expr(),
722                    scripts.list_wrapper.as_slice(),
723                );
724                check_script_ids(
725                    profile_name,
726                    ProfileScriptType::RunWrapper,
727                    scripts.data.expr(),
728                    scripts.run_wrapper.as_slice(),
729                );
730            });
731
732            if empty_script_count > 0 {
733                warnings.empty_script_sections(
734                    config_file,
735                    workspace_root,
736                    tool,
737                    profile_name,
738                    empty_script_count,
739                );
740            }
741        });
742
743        // If there were any errors parsing profile-specific script data, error
744        // out.
745        if !profile_script_errors.is_empty() {
746            let known_scripts = known_scripts
747                .iter()
748                .map(|script| script.id.clone())
749                .collect();
750            return Err(ConfigParseError::new(
751                config_file,
752                tool,
753                ConfigParseErrorKind::ProfileScriptErrors {
754                    errors: Box::new(profile_script_errors),
755                    known_scripts,
756                },
757            ));
758        }
759
760        // Grab the compiled data (default-filter, overrides and setup scripts) for this config,
761        // adding them in reversed order (we'll flip it around at the end).
762        compiled_out.default.extend_reverse(this_compiled.default);
763        for (name, mut data) in this_compiled.other {
764            match compiled_out.other.entry(name) {
765                hash_map::Entry::Vacant(entry) => {
766                    // When inserting a new element, reverse the data.
767                    data.reverse();
768                    entry.insert(data);
769                }
770                hash_map::Entry::Occupied(mut entry) => {
771                    // When appending to an existing element, extend the data in reverse.
772                    entry.get_mut().extend_reverse(data);
773                }
774            }
775        }
776
777        Ok(())
778    }
779
780    fn make_default_config() -> ConfigBuilder<DefaultState> {
781        Config::builder().add_source(File::from_str(Self::DEFAULT_CONFIG, FileFormat::Toml))
782    }
783
784    fn make_profile(&self, name: &str) -> Result<EarlyProfile<'_>, ProfileNotFound> {
785        let custom_profile = self.inner.get_profile(name)?;
786
787        // The profile was found: construct it.
788        let mut store_dir = self.workspace_root.join(&self.inner.store.dir);
789        store_dir.push(name);
790
791        // Grab the compiled data as well.
792        let compiled_data = match self.compiled.other.get(name) {
793            Some(data) => data.clone().chain(self.compiled.default.clone()),
794            None => self.compiled.default.clone(),
795        };
796
797        Ok(EarlyProfile {
798            name: name.to_owned(),
799            store_dir,
800            default_profile: &self.inner.default_profile,
801            custom_profile,
802            test_groups: &self.inner.test_groups,
803            scripts: &self.inner.scripts,
804            compiled_data,
805        })
806    }
807
808    /// This returns a tuple of (config, ignored paths).
809    fn build_and_deserialize_config(
810        builder: &ConfigBuilder<DefaultState>,
811    ) -> Result<(NextestConfigDeserialize, BTreeSet<String>), ConfigParseErrorKind> {
812        let config = builder
813            .build_cloned()
814            .map_err(|error| ConfigParseErrorKind::BuildError(Box::new(error)))?;
815
816        let mut ignored = BTreeSet::new();
817        let mut cb = |path: serde_ignored::Path| {
818            ignored.insert(path.to_string());
819        };
820        let ignored_de = serde_ignored::Deserializer::new(config, &mut cb);
821        let config: NextestConfigDeserialize = serde_path_to_error::deserialize(ignored_de)
822            .map_err(|error| {
823                // Both serde_path_to_error and the latest versions of the
824                // config crate report the key. We drop the key from the config
825                // error for consistency.
826                let path = error.path().clone();
827                let config_error = error.into_inner();
828                let error = match config_error {
829                    ConfigError::At { error, .. } => *error,
830                    other => other,
831                };
832                ConfigParseErrorKind::DeserializeError(Box::new(serde_path_to_error::Error::new(
833                    path, error,
834                )))
835            })?;
836
837        Ok((config, ignored))
838    }
839}
840
841/// The state of nextest profiles before build platforms have been applied.
842#[derive(Clone, Debug, Default)]
843pub(super) struct PreBuildPlatform {}
844
845/// The state of nextest profiles after build platforms have been applied.
846#[derive(Clone, Debug)]
847pub(crate) struct FinalConfig {
848    // Evaluation result for host_spec on the host platform.
849    pub(super) host_eval: bool,
850    // Evaluation result for target_spec corresponding to tests that run on the host platform (e.g.
851    // proc-macro tests).
852    pub(super) host_test_eval: bool,
853    // Evaluation result for target_spec corresponding to tests that run on the target platform
854    // (most regular tests).
855    pub(super) target_eval: bool,
856}
857
858/// A nextest profile that can be obtained without identifying the host and
859/// target platforms.
860///
861/// Returned by [`NextestConfig::profile`].
862pub struct EarlyProfile<'cfg> {
863    name: String,
864    store_dir: Utf8PathBuf,
865    default_profile: &'cfg DefaultProfileImpl,
866    custom_profile: Option<&'cfg CustomProfileImpl>,
867    test_groups: &'cfg BTreeMap<CustomTestGroup, TestGroupConfig>,
868    // This is ordered because the scripts are used in the order they're defined.
869    scripts: &'cfg ScriptConfig,
870    // Invariant: `compiled_data.default_filter` is always present.
871    pub(super) compiled_data: CompiledData<PreBuildPlatform>,
872}
873
874impl<'cfg> EarlyProfile<'cfg> {
875    /// Returns the absolute profile-specific store directory.
876    pub fn store_dir(&self) -> &Utf8Path {
877        &self.store_dir
878    }
879
880    /// Returns the global test group configuration.
881    pub fn test_group_config(&self) -> &'cfg BTreeMap<CustomTestGroup, TestGroupConfig> {
882        self.test_groups
883    }
884
885    /// Applies build platforms to make the profile ready for evaluation.
886    ///
887    /// This is a separate step from parsing the config and reading a profile so that cargo-nextest
888    /// can tell users about configuration parsing errors before building the binary list.
889    pub fn apply_build_platforms(
890        self,
891        build_platforms: &BuildPlatforms,
892    ) -> EvaluatableProfile<'cfg> {
893        let compiled_data = self.compiled_data.apply_build_platforms(build_platforms);
894
895        let resolved_default_filter = {
896            // Look for the default filter in the first valid override.
897            let found_filter = compiled_data
898                .overrides
899                .iter()
900                .find_map(|override_data| override_data.default_filter_if_matches_platform());
901            found_filter.unwrap_or_else(|| {
902                // No overrides matching the default filter were found -- use
903                // the profile's default.
904                compiled_data
905                    .profile_default_filter
906                    .as_ref()
907                    .expect("compiled data always has default set")
908            })
909        }
910        .clone();
911
912        EvaluatableProfile {
913            name: self.name,
914            store_dir: self.store_dir,
915            default_profile: self.default_profile,
916            custom_profile: self.custom_profile,
917            scripts: self.scripts,
918            test_groups: self.test_groups,
919            compiled_data,
920            resolved_default_filter,
921        }
922    }
923}
924
925/// A configuration profile for nextest. Contains most configuration used by the nextest runner.
926///
927/// Returned by [`EarlyProfile::apply_build_platforms`].
928#[derive(Clone, Debug)]
929pub struct EvaluatableProfile<'cfg> {
930    name: String,
931    store_dir: Utf8PathBuf,
932    default_profile: &'cfg DefaultProfileImpl,
933    custom_profile: Option<&'cfg CustomProfileImpl>,
934    test_groups: &'cfg BTreeMap<CustomTestGroup, TestGroupConfig>,
935    // This is ordered because the scripts are used in the order they're defined.
936    scripts: &'cfg ScriptConfig,
937    // Invariant: `compiled_data.default_filter` is always present.
938    pub(super) compiled_data: CompiledData<FinalConfig>,
939    // The default filter that's been resolved after considering overrides (i.e.
940    // platforms).
941    resolved_default_filter: CompiledDefaultFilter,
942}
943
944impl<'cfg> EvaluatableProfile<'cfg> {
945    /// Returns the name of the profile.
946    pub fn name(&self) -> &str {
947        &self.name
948    }
949
950    /// Returns the absolute profile-specific store directory.
951    pub fn store_dir(&self) -> &Utf8Path {
952        &self.store_dir
953    }
954
955    /// Returns the context in which to evaluate filtersets.
956    pub fn filterset_ecx(&self) -> EvalContext<'_> {
957        EvalContext {
958            default_filter: &self.default_filter().expr,
959        }
960    }
961
962    /// Returns the default set of tests to run.
963    pub fn default_filter(&self) -> &CompiledDefaultFilter {
964        &self.resolved_default_filter
965    }
966
967    /// Returns the global test group configuration.
968    pub fn test_group_config(&self) -> &'cfg BTreeMap<CustomTestGroup, TestGroupConfig> {
969        self.test_groups
970    }
971
972    /// Returns the global script configuration.
973    pub fn script_config(&self) -> &'cfg ScriptConfig {
974        self.scripts
975    }
976
977    /// Returns the retry count for this profile.
978    pub fn retries(&self) -> RetryPolicy {
979        self.custom_profile
980            .and_then(|profile| profile.retries)
981            .unwrap_or(self.default_profile.retries)
982    }
983
984    /// Returns the number of threads to run against for this profile.
985    pub fn test_threads(&self) -> TestThreads {
986        self.custom_profile
987            .and_then(|profile| profile.test_threads)
988            .unwrap_or(self.default_profile.test_threads)
989    }
990
991    /// Returns the number of threads required for each test.
992    pub fn threads_required(&self) -> ThreadsRequired {
993        self.custom_profile
994            .and_then(|profile| profile.threads_required)
995            .unwrap_or(self.default_profile.threads_required)
996    }
997
998    /// Returns extra arguments to be passed to the test binary at runtime.
999    pub fn run_extra_args(&self) -> &'cfg [String] {
1000        self.custom_profile
1001            .and_then(|profile| profile.run_extra_args.as_deref())
1002            .unwrap_or(&self.default_profile.run_extra_args)
1003    }
1004
1005    /// Returns the time after which tests are treated as slow for this profile.
1006    pub fn slow_timeout(&self) -> SlowTimeout {
1007        self.custom_profile
1008            .and_then(|profile| profile.slow_timeout)
1009            .unwrap_or(self.default_profile.slow_timeout)
1010    }
1011
1012    /// Returns the time after which we should stop running tests.
1013    pub fn global_timeout(&self) -> GlobalTimeout {
1014        self.custom_profile
1015            .and_then(|profile| profile.global_timeout)
1016            .unwrap_or(self.default_profile.global_timeout)
1017    }
1018
1019    /// Returns the time after which a child process that hasn't closed its handles is marked as
1020    /// leaky.
1021    pub fn leak_timeout(&self) -> LeakTimeout {
1022        self.custom_profile
1023            .and_then(|profile| profile.leak_timeout)
1024            .unwrap_or(self.default_profile.leak_timeout)
1025    }
1026
1027    /// Returns the test status level.
1028    pub fn status_level(&self) -> StatusLevel {
1029        self.custom_profile
1030            .and_then(|profile| profile.status_level)
1031            .unwrap_or(self.default_profile.status_level)
1032    }
1033
1034    /// Returns the test status level at the end of the run.
1035    pub fn final_status_level(&self) -> FinalStatusLevel {
1036        self.custom_profile
1037            .and_then(|profile| profile.final_status_level)
1038            .unwrap_or(self.default_profile.final_status_level)
1039    }
1040
1041    /// Returns the failure output config for this profile.
1042    pub fn failure_output(&self) -> TestOutputDisplay {
1043        self.custom_profile
1044            .and_then(|profile| profile.failure_output)
1045            .unwrap_or(self.default_profile.failure_output)
1046    }
1047
1048    /// Returns the failure output config for this profile.
1049    pub fn success_output(&self) -> TestOutputDisplay {
1050        self.custom_profile
1051            .and_then(|profile| profile.success_output)
1052            .unwrap_or(self.default_profile.success_output)
1053    }
1054
1055    /// Returns the max-fail config for this profile.
1056    pub fn max_fail(&self) -> MaxFail {
1057        self.custom_profile
1058            .and_then(|profile| profile.max_fail)
1059            .unwrap_or(self.default_profile.max_fail)
1060    }
1061
1062    /// Returns the archive configuration for this profile.
1063    pub fn archive_config(&self) -> &'cfg ArchiveConfig {
1064        self.custom_profile
1065            .and_then(|profile| profile.archive.as_ref())
1066            .unwrap_or(&self.default_profile.archive)
1067    }
1068
1069    /// Returns the list of setup scripts.
1070    pub fn setup_scripts(&self, test_list: &TestList<'_>) -> SetupScripts<'_> {
1071        SetupScripts::new(self, test_list)
1072    }
1073
1074    /// Returns list-time settings for a test binary.
1075    pub fn list_settings_for(&self, query: &BinaryQuery<'_>) -> ListSettings<'_> {
1076        ListSettings::new(self, query)
1077    }
1078
1079    /// Returns settings for individual tests.
1080    pub fn settings_for(&self, query: &TestQuery<'_>) -> TestSettings<'_> {
1081        TestSettings::new(self, query)
1082    }
1083
1084    /// Returns override settings for individual tests, with sources attached.
1085    pub(crate) fn settings_with_source_for(
1086        &self,
1087        query: &TestQuery<'_>,
1088    ) -> TestSettings<'_, SettingSource<'_>> {
1089        TestSettings::new(self, query)
1090    }
1091
1092    /// Returns the JUnit configuration for this profile.
1093    pub fn junit(&self) -> Option<JunitConfig<'cfg>> {
1094        JunitConfig::new(
1095            self.store_dir(),
1096            self.custom_profile.map(|p| &p.junit),
1097            &self.default_profile.junit,
1098        )
1099    }
1100
1101    #[cfg(test)]
1102    pub(super) fn custom_profile(&self) -> Option<&'cfg CustomProfileImpl> {
1103        self.custom_profile
1104    }
1105}
1106
1107#[derive(Clone, Debug)]
1108pub(super) struct NextestConfigImpl {
1109    store: StoreConfigImpl,
1110    test_groups: BTreeMap<CustomTestGroup, TestGroupConfig>,
1111    scripts: ScriptConfig,
1112    default_profile: DefaultProfileImpl,
1113    other_profiles: HashMap<String, CustomProfileImpl>,
1114}
1115
1116impl NextestConfigImpl {
1117    fn get_profile(&self, profile: &str) -> Result<Option<&CustomProfileImpl>, ProfileNotFound> {
1118        let custom_profile = match profile {
1119            NextestConfig::DEFAULT_PROFILE => None,
1120            other => Some(
1121                self.other_profiles
1122                    .get(other)
1123                    .ok_or_else(|| ProfileNotFound::new(profile, self.all_profiles()))?,
1124            ),
1125        };
1126        Ok(custom_profile)
1127    }
1128
1129    fn all_profiles(&self) -> impl Iterator<Item = &str> {
1130        self.other_profiles
1131            .keys()
1132            .map(|key| key.as_str())
1133            .chain(std::iter::once(NextestConfig::DEFAULT_PROFILE))
1134    }
1135
1136    pub(super) fn default_profile(&self) -> &DefaultProfileImpl {
1137        &self.default_profile
1138    }
1139
1140    pub(super) fn other_profiles(&self) -> impl Iterator<Item = (&str, &CustomProfileImpl)> {
1141        self.other_profiles
1142            .iter()
1143            .map(|(key, value)| (key.as_str(), value))
1144    }
1145}
1146
1147// This is the form of `NextestConfig` that gets deserialized.
1148#[derive(Clone, Debug, Deserialize)]
1149#[serde(rename_all = "kebab-case")]
1150struct NextestConfigDeserialize {
1151    store: StoreConfigImpl,
1152
1153    // These are parsed as part of NextestConfigVersionOnly. They're re-parsed here to avoid
1154    // printing an "unknown key" message.
1155    #[expect(unused)]
1156    #[serde(default)]
1157    nextest_version: Option<NextestVersionDeserialize>,
1158    #[expect(unused)]
1159    #[serde(default)]
1160    experimental: BTreeSet<String>,
1161
1162    #[serde(default)]
1163    test_groups: BTreeMap<CustomTestGroup, TestGroupConfig>,
1164    // Previous version of setup scripts, stored as "script.<name of script>".
1165    #[serde(default, rename = "script")]
1166    old_setup_scripts: IndexMap<ScriptId, SetupScriptConfig>,
1167    #[serde(default)]
1168    scripts: ScriptConfig,
1169    #[serde(rename = "profile")]
1170    profiles: HashMap<String, CustomProfileImpl>,
1171}
1172
1173impl NextestConfigDeserialize {
1174    fn into_config_impl(mut self) -> NextestConfigImpl {
1175        let p = self
1176            .profiles
1177            .remove("default")
1178            .expect("default profile should exist");
1179        let default_profile = DefaultProfileImpl::new(p);
1180
1181        // XXX: This is not quite right (doesn't obey precedence) but is okay
1182        // because it's unlikely folks are using the combination of setup
1183        // scripts *and* tools *and* relying on this. If it breaks, well, this
1184        // feature isn't stable.
1185        for (script_id, script_config) in self.old_setup_scripts {
1186            if let indexmap::map::Entry::Vacant(entry) = self.scripts.setup.entry(script_id) {
1187                entry.insert(script_config);
1188            }
1189        }
1190
1191        NextestConfigImpl {
1192            store: self.store,
1193            default_profile,
1194            test_groups: self.test_groups,
1195            scripts: self.scripts,
1196            other_profiles: self.profiles,
1197        }
1198    }
1199}
1200
1201#[derive(Clone, Debug, Deserialize)]
1202#[serde(rename_all = "kebab-case")]
1203struct StoreConfigImpl {
1204    dir: Utf8PathBuf,
1205}
1206
1207#[derive(Clone, Debug)]
1208pub(super) struct DefaultProfileImpl {
1209    default_filter: String,
1210    test_threads: TestThreads,
1211    threads_required: ThreadsRequired,
1212    run_extra_args: Vec<String>,
1213    retries: RetryPolicy,
1214    status_level: StatusLevel,
1215    final_status_level: FinalStatusLevel,
1216    failure_output: TestOutputDisplay,
1217    success_output: TestOutputDisplay,
1218    max_fail: MaxFail,
1219    slow_timeout: SlowTimeout,
1220    global_timeout: GlobalTimeout,
1221    leak_timeout: LeakTimeout,
1222    overrides: Vec<DeserializedOverride>,
1223    scripts: Vec<DeserializedProfileScriptConfig>,
1224    junit: DefaultJunitImpl,
1225    archive: ArchiveConfig,
1226}
1227
1228impl DefaultProfileImpl {
1229    fn new(p: CustomProfileImpl) -> Self {
1230        Self {
1231            default_filter: p
1232                .default_filter
1233                .expect("default-filter present in default profile"),
1234            test_threads: p
1235                .test_threads
1236                .expect("test-threads present in default profile"),
1237            threads_required: p
1238                .threads_required
1239                .expect("threads-required present in default profile"),
1240            run_extra_args: p
1241                .run_extra_args
1242                .expect("run-extra-args present in default profile"),
1243            retries: p.retries.expect("retries present in default profile"),
1244            status_level: p
1245                .status_level
1246                .expect("status-level present in default profile"),
1247            final_status_level: p
1248                .final_status_level
1249                .expect("final-status-level present in default profile"),
1250            failure_output: p
1251                .failure_output
1252                .expect("failure-output present in default profile"),
1253            success_output: p
1254                .success_output
1255                .expect("success-output present in default profile"),
1256            max_fail: p.max_fail.expect("fail-fast present in default profile"),
1257            slow_timeout: p
1258                .slow_timeout
1259                .expect("slow-timeout present in default profile"),
1260            global_timeout: p
1261                .global_timeout
1262                .expect("global-timeout present in default profile"),
1263            leak_timeout: p
1264                .leak_timeout
1265                .expect("leak-timeout present in default profile"),
1266            overrides: p.overrides,
1267            scripts: p.scripts,
1268            junit: DefaultJunitImpl::for_default_profile(p.junit),
1269            archive: p.archive.expect("archive present in default profile"),
1270        }
1271    }
1272
1273    pub(super) fn default_filter(&self) -> &str {
1274        &self.default_filter
1275    }
1276
1277    pub(super) fn overrides(&self) -> &[DeserializedOverride] {
1278        &self.overrides
1279    }
1280
1281    pub(super) fn setup_scripts(&self) -> &[DeserializedProfileScriptConfig] {
1282        &self.scripts
1283    }
1284}
1285
1286#[derive(Clone, Debug, Deserialize)]
1287#[serde(rename_all = "kebab-case")]
1288pub(super) struct CustomProfileImpl {
1289    /// The default set of tests run by `cargo nextest run`.
1290    #[serde(default)]
1291    default_filter: Option<String>,
1292    #[serde(default, deserialize_with = "super::deserialize_retry_policy")]
1293    retries: Option<RetryPolicy>,
1294    #[serde(default)]
1295    test_threads: Option<TestThreads>,
1296    #[serde(default)]
1297    threads_required: Option<ThreadsRequired>,
1298    #[serde(default)]
1299    run_extra_args: Option<Vec<String>>,
1300    #[serde(default)]
1301    status_level: Option<StatusLevel>,
1302    #[serde(default)]
1303    final_status_level: Option<FinalStatusLevel>,
1304    #[serde(default)]
1305    failure_output: Option<TestOutputDisplay>,
1306    #[serde(default)]
1307    success_output: Option<TestOutputDisplay>,
1308    #[serde(
1309        default,
1310        rename = "fail-fast",
1311        deserialize_with = "super::deserialize_fail_fast"
1312    )]
1313    max_fail: Option<MaxFail>,
1314    #[serde(default, deserialize_with = "super::deserialize_slow_timeout")]
1315    slow_timeout: Option<SlowTimeout>,
1316    #[serde(default)]
1317    global_timeout: Option<GlobalTimeout>,
1318    #[serde(default, deserialize_with = "super::deserialize_leak_timeout")]
1319    leak_timeout: Option<LeakTimeout>,
1320    #[serde(default)]
1321    overrides: Vec<DeserializedOverride>,
1322    #[serde(default)]
1323    scripts: Vec<DeserializedProfileScriptConfig>,
1324    #[serde(default)]
1325    junit: JunitImpl,
1326    #[serde(default)]
1327    archive: Option<ArchiveConfig>,
1328}
1329
1330impl CustomProfileImpl {
1331    #[cfg(test)]
1332    pub(super) fn test_threads(&self) -> Option<TestThreads> {
1333        self.test_threads
1334    }
1335
1336    pub(super) fn default_filter(&self) -> Option<&str> {
1337        self.default_filter.as_deref()
1338    }
1339
1340    pub(super) fn overrides(&self) -> &[DeserializedOverride] {
1341        &self.overrides
1342    }
1343
1344    pub(super) fn scripts(&self) -> &[DeserializedProfileScriptConfig] {
1345        &self.scripts
1346    }
1347}
1348
1349#[cfg(test)]
1350mod tests {
1351    use super::*;
1352    use crate::config::test_helpers::*;
1353    use camino_tempfile::tempdir;
1354    use iddqd::{IdHashItem, IdHashMap, id_hash_map, id_upcast};
1355
1356    /// Test implementation of ConfigWarnings that collects warnings for testing.
1357    #[derive(Default)]
1358    struct TestConfigWarnings {
1359        unknown_keys: IdHashMap<UnknownKeys>,
1360        reserved_profiles: IdHashMap<ReservedProfiles>,
1361        deprecated_scripts: IdHashMap<DeprecatedScripts>,
1362        empty_script_warnings: IdHashMap<EmptyScriptSections>,
1363    }
1364
1365    impl ConfigWarnings for TestConfigWarnings {
1366        fn unknown_config_keys(
1367            &mut self,
1368            config_file: &Utf8Path,
1369            _workspace_root: &Utf8Path,
1370            tool: Option<&str>,
1371            unknown: &BTreeSet<String>,
1372        ) {
1373            self.unknown_keys
1374                .insert_unique(UnknownKeys {
1375                    tool: tool.map(|s| s.to_owned()),
1376                    config_file: config_file.to_owned(),
1377                    keys: unknown.clone(),
1378                })
1379                .unwrap();
1380        }
1381
1382        fn unknown_reserved_profiles(
1383            &mut self,
1384            config_file: &Utf8Path,
1385            _workspace_root: &Utf8Path,
1386            tool: Option<&str>,
1387            profiles: &[&str],
1388        ) {
1389            self.reserved_profiles
1390                .insert_unique(ReservedProfiles {
1391                    tool: tool.map(|s| s.to_owned()),
1392                    config_file: config_file.to_owned(),
1393                    profiles: profiles.iter().map(|&s| s.to_owned()).collect(),
1394                })
1395                .unwrap();
1396        }
1397
1398        fn empty_script_sections(
1399            &mut self,
1400            config_file: &Utf8Path,
1401            _workspace_root: &Utf8Path,
1402            tool: Option<&str>,
1403            profile_name: &str,
1404            empty_count: usize,
1405        ) {
1406            self.empty_script_warnings
1407                .insert_unique(EmptyScriptSections {
1408                    tool: tool.map(|s| s.to_owned()),
1409                    config_file: config_file.to_owned(),
1410                    profile_name: profile_name.to_owned(),
1411                    empty_count,
1412                })
1413                .unwrap();
1414        }
1415
1416        fn deprecated_script_config(
1417            &mut self,
1418            config_file: &Utf8Path,
1419            _workspace_root: &Utf8Path,
1420            tool: Option<&str>,
1421        ) {
1422            self.deprecated_scripts
1423                .insert_unique(DeprecatedScripts {
1424                    tool: tool.map(|s| s.to_owned()),
1425                    config_file: config_file.to_owned(),
1426                })
1427                .unwrap();
1428        }
1429    }
1430
1431    #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
1432    struct UnknownKeys {
1433        tool: Option<String>,
1434        config_file: Utf8PathBuf,
1435        keys: BTreeSet<String>,
1436    }
1437
1438    impl IdHashItem for UnknownKeys {
1439        type Key<'a> = Option<&'a str>;
1440        fn key(&self) -> Self::Key<'_> {
1441            self.tool.as_deref()
1442        }
1443        id_upcast!();
1444    }
1445
1446    #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
1447    struct ReservedProfiles {
1448        tool: Option<String>,
1449        config_file: Utf8PathBuf,
1450        profiles: Vec<String>,
1451    }
1452
1453    impl IdHashItem for ReservedProfiles {
1454        type Key<'a> = Option<&'a str>;
1455        fn key(&self) -> Self::Key<'_> {
1456            self.tool.as_deref()
1457        }
1458        id_upcast!();
1459    }
1460
1461    #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
1462    struct DeprecatedScripts {
1463        tool: Option<String>,
1464        config_file: Utf8PathBuf,
1465    }
1466
1467    impl IdHashItem for DeprecatedScripts {
1468        type Key<'a> = Option<&'a str>;
1469        fn key(&self) -> Self::Key<'_> {
1470            self.tool.as_deref()
1471        }
1472        id_upcast!();
1473    }
1474
1475    #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
1476    struct EmptyScriptSections {
1477        tool: Option<String>,
1478        config_file: Utf8PathBuf,
1479        profile_name: String,
1480        empty_count: usize,
1481    }
1482
1483    impl IdHashItem for EmptyScriptSections {
1484        type Key<'a> = (&'a Option<String>, &'a str);
1485        fn key(&self) -> Self::Key<'_> {
1486            (&self.tool, &self.profile_name)
1487        }
1488        id_upcast!();
1489    }
1490
1491    #[test]
1492    fn default_config_is_valid() {
1493        let default_config = NextestConfig::default_config("foo");
1494        default_config
1495            .profile(NextestConfig::DEFAULT_PROFILE)
1496            .expect("default profile should exist");
1497    }
1498
1499    #[test]
1500    fn ignored_keys() {
1501        let config_contents = r#"
1502        ignored1 = "test"
1503
1504        [profile.default]
1505        retries = 3
1506        ignored2 = "hi"
1507
1508        [profile.default-foo]
1509        retries = 5
1510
1511        [[profile.default.overrides]]
1512        filter = 'test(test_foo)'
1513        retries = 20
1514        ignored3 = 42
1515        "#;
1516
1517        let tool_config_contents = r#"
1518        [store]
1519        ignored4 = 20
1520
1521        [profile.default]
1522        retries = 4
1523        ignored5 = false
1524
1525        [profile.default-bar]
1526        retries = 5
1527
1528        [profile.tool]
1529        retries = 12
1530
1531        [[profile.tool.overrides]]
1532        filter = 'test(test_baz)'
1533        retries = 22
1534        ignored6 = 6.5
1535        "#;
1536
1537        let workspace_dir = tempdir().unwrap();
1538
1539        let graph = temp_workspace(&workspace_dir, config_contents);
1540        let workspace_root = graph.workspace().root();
1541        let tool_path = workspace_root.join(".config/tool.toml");
1542        std::fs::write(&tool_path, tool_config_contents).unwrap();
1543
1544        let pcx = ParseContext::new(&graph);
1545
1546        let mut warnings = TestConfigWarnings::default();
1547
1548        let _ = NextestConfig::from_sources_with_warnings(
1549            workspace_root,
1550            &pcx,
1551            None,
1552            &[ToolConfigFile {
1553                tool: "my-tool".to_owned(),
1554                config_file: tool_path.clone(),
1555            }][..],
1556            &Default::default(),
1557            &mut warnings,
1558        )
1559        .expect("config is valid");
1560
1561        assert_eq!(
1562            warnings.unknown_keys.len(),
1563            2,
1564            "there are two files with unknown keys"
1565        );
1566
1567        assert_eq!(
1568            warnings.unknown_keys,
1569            id_hash_map! {
1570                UnknownKeys {
1571                    tool: None,
1572                    config_file: workspace_root.join(".config/nextest.toml"),
1573                    keys: maplit::btreeset! {
1574                        "ignored1".to_owned(),
1575                        "profile.default.ignored2".to_owned(),
1576                        "profile.default.overrides.0.ignored3".to_owned(),
1577                    }
1578                },
1579                UnknownKeys {
1580                    tool: Some("my-tool".to_owned()),
1581                    config_file: tool_path.clone(),
1582                    keys: maplit::btreeset! {
1583                        "store.ignored4".to_owned(),
1584                        "profile.default.ignored5".to_owned(),
1585                        "profile.tool.overrides.0.ignored6".to_owned(),
1586                    }
1587                }
1588            }
1589        );
1590        assert_eq!(
1591            warnings.reserved_profiles,
1592            id_hash_map! {
1593                ReservedProfiles {
1594                    tool: None,
1595                    config_file: workspace_root.join(".config/nextest.toml"),
1596                    profiles: vec!["default-foo".to_owned()],
1597                },
1598                ReservedProfiles {
1599                    tool: Some("my-tool".to_owned()),
1600                    config_file: tool_path,
1601                    profiles: vec!["default-bar".to_owned()],
1602                }
1603            },
1604        )
1605    }
1606
1607    #[test]
1608    fn script_warnings() {
1609        let config_contents = r#"
1610        experimental = ["setup-scripts", "wrapper-scripts"]
1611
1612        [scripts.wrapper.script1]
1613        command = "echo test"
1614
1615        [scripts.wrapper.script2]
1616        command = "echo test2"
1617
1618        [scripts.setup.script3]
1619        command = "echo setup"
1620
1621        [[profile.default.scripts]]
1622        filter = 'all()'
1623        # Empty - no setup or wrapper scripts
1624
1625        [[profile.default.scripts]]
1626        filter = 'test(foo)'
1627        setup = ["script3"]
1628
1629        [profile.custom]
1630        [[profile.custom.scripts]]
1631        filter = 'all()'
1632        # Empty - no setup or wrapper scripts
1633
1634        [[profile.custom.scripts]]
1635        filter = 'test(bar)'
1636        # Another empty section
1637        "#;
1638
1639        let tool_config_contents = r#"
1640        experimental = ["setup-scripts", "wrapper-scripts"]
1641
1642        [scripts.wrapper."@tool:tool:disabled_script"]
1643        command = "echo disabled"
1644
1645        [scripts.setup."@tool:tool:setup_script"]
1646        command = "echo setup"
1647
1648        [profile.tool]
1649        [[profile.tool.scripts]]
1650        filter = 'all()'
1651        # Empty section
1652
1653        [[profile.tool.scripts]]
1654        filter = 'test(foo)'
1655        setup = ["@tool:tool:setup_script"]
1656        "#;
1657
1658        let workspace_dir = tempdir().unwrap();
1659        let graph = temp_workspace(&workspace_dir, config_contents);
1660        let workspace_root = graph.workspace().root();
1661        let tool_path = workspace_root.join(".config/tool.toml");
1662        std::fs::write(&tool_path, tool_config_contents).unwrap();
1663
1664        let pcx = ParseContext::new(&graph);
1665
1666        let mut warnings = TestConfigWarnings::default();
1667
1668        let experimental = maplit::btreeset! {
1669            ConfigExperimental::SetupScripts,
1670            ConfigExperimental::WrapperScripts
1671        };
1672        let _ = NextestConfig::from_sources_with_warnings(
1673            workspace_root,
1674            &pcx,
1675            None,
1676            &[ToolConfigFile {
1677                tool: "tool".to_owned(),
1678                config_file: tool_path.clone(),
1679            }][..],
1680            &experimental,
1681            &mut warnings,
1682        )
1683        .expect("config is valid");
1684
1685        assert_eq!(
1686            warnings.empty_script_warnings,
1687            id_hash_map! {
1688                EmptyScriptSections {
1689                    tool: None,
1690                    config_file: workspace_root.join(".config/nextest.toml"),
1691                    profile_name: "default".to_owned(),
1692                    empty_count: 1,
1693                },
1694                EmptyScriptSections {
1695                    tool: None,
1696                    config_file: workspace_root.join(".config/nextest.toml"),
1697                    profile_name: "custom".to_owned(),
1698                    empty_count: 2,
1699                },
1700                EmptyScriptSections {
1701                    tool: Some("tool".to_owned()),
1702                    config_file: tool_path,
1703                    profile_name: "tool".to_owned(),
1704                    empty_count: 1,
1705                }
1706            }
1707        );
1708    }
1709
1710    #[test]
1711    fn deprecated_script_config_warning() {
1712        let config_contents = r#"
1713        experimental = ["setup-scripts"]
1714
1715        [script.my-script]
1716        command = "echo hello"
1717"#;
1718
1719        let tool_config_contents = r#"
1720        experimental = ["setup-scripts"]
1721
1722        [script."@tool:my-tool:my-script"]
1723        command = "echo hello"
1724"#;
1725
1726        let temp_dir = tempdir().unwrap();
1727
1728        let graph = temp_workspace(&temp_dir, config_contents);
1729        let workspace_root = graph.workspace().root();
1730        let tool_path = workspace_root.join(".config/my-tool.toml");
1731        std::fs::write(&tool_path, tool_config_contents).unwrap();
1732        let pcx = ParseContext::new(&graph);
1733
1734        let mut warnings = TestConfigWarnings::default();
1735        NextestConfig::from_sources_with_warnings(
1736            graph.workspace().root(),
1737            &pcx,
1738            None,
1739            &[ToolConfigFile {
1740                tool: "my-tool".to_owned(),
1741                config_file: tool_path.clone(),
1742            }],
1743            &maplit::btreeset! {ConfigExperimental::SetupScripts},
1744            &mut warnings,
1745        )
1746        .expect("config is valid");
1747
1748        assert_eq!(
1749            warnings.deprecated_scripts,
1750            id_hash_map! {
1751                DeprecatedScripts {
1752                    tool: None,
1753                    config_file: graph.workspace().root().join(".config/nextest.toml"),
1754                },
1755                DeprecatedScripts {
1756                    tool: Some("my-tool".to_owned()),
1757                    config_file: tool_path,
1758                }
1759            }
1760        );
1761    }
1762}