nextest_metadata/
test_list.rs

1// Copyright (c) The nextest Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use crate::CommandError;
5use camino::{Utf8Path, Utf8PathBuf};
6use serde::{Deserialize, Serialize};
7use smol_str::SmolStr;
8use std::{
9    borrow::Cow,
10    cmp::Ordering,
11    collections::{BTreeMap, BTreeSet},
12    fmt::{self, Write as _},
13    path::PathBuf,
14    process::Command,
15};
16use target_spec::summaries::PlatformSummary;
17
18/// Command builder for `cargo nextest list`.
19#[derive(Clone, Debug, Default)]
20pub struct ListCommand {
21    cargo_path: Option<Box<Utf8Path>>,
22    manifest_path: Option<Box<Utf8Path>>,
23    current_dir: Option<Box<Utf8Path>>,
24    args: Vec<Box<str>>,
25}
26
27impl ListCommand {
28    /// Creates a new `ListCommand`.
29    ///
30    /// This command runs `cargo nextest list`.
31    pub fn new() -> Self {
32        Self::default()
33    }
34
35    /// Path to `cargo` executable. If not set, this will use the the `$CARGO` environment variable, and
36    /// if that is not set, will simply be `cargo`.
37    pub fn cargo_path(&mut self, path: impl Into<Utf8PathBuf>) -> &mut Self {
38        self.cargo_path = Some(path.into().into());
39        self
40    }
41
42    /// Path to `Cargo.toml`.
43    pub fn manifest_path(&mut self, path: impl Into<Utf8PathBuf>) -> &mut Self {
44        self.manifest_path = Some(path.into().into());
45        self
46    }
47
48    /// Current directory of the `cargo nextest list` process.
49    pub fn current_dir(&mut self, path: impl Into<Utf8PathBuf>) -> &mut Self {
50        self.current_dir = Some(path.into().into());
51        self
52    }
53
54    /// Adds an argument to the end of `cargo nextest list`.
55    pub fn add_arg(&mut self, arg: impl Into<String>) -> &mut Self {
56        self.args.push(arg.into().into());
57        self
58    }
59
60    /// Adds several arguments to the end of `cargo nextest list`.
61    pub fn add_args(&mut self, args: impl IntoIterator<Item = impl Into<String>>) -> &mut Self {
62        for arg in args {
63            self.add_arg(arg.into());
64        }
65        self
66    }
67
68    /// Builds a command for `cargo nextest list`. This is the first part of the
69    /// work of [`Self::exec`].
70    pub fn cargo_command(&self) -> Command {
71        let cargo_path: PathBuf = self.cargo_path.as_ref().map_or_else(
72            || std::env::var_os("CARGO").map_or("cargo".into(), PathBuf::from),
73            |path| PathBuf::from(path.as_std_path()),
74        );
75
76        let mut command = Command::new(cargo_path);
77        if let Some(path) = &self.manifest_path.as_deref() {
78            command.args(["--manifest-path", path.as_str()]);
79        }
80        if let Some(current_dir) = &self.current_dir.as_deref() {
81            command.current_dir(current_dir);
82        }
83
84        command.args(["nextest", "list", "--message-format=json"]);
85
86        command.args(self.args.iter().map(|s| s.as_ref()));
87        command
88    }
89
90    /// Executes `cargo nextest list` and parses the output into a [`TestListSummary`].
91    pub fn exec(&self) -> Result<TestListSummary, CommandError> {
92        let mut command = self.cargo_command();
93        let output = command.output().map_err(CommandError::Exec)?;
94
95        if !output.status.success() {
96            // The process exited with a non-zero code.
97            let exit_code = output.status.code();
98            let stderr = output.stderr;
99            return Err(CommandError::CommandFailed { exit_code, stderr });
100        }
101
102        // Try parsing stdout.
103        serde_json::from_slice(&output.stdout).map_err(CommandError::Json)
104    }
105
106    /// Executes `cargo nextest list --list-type binaries-only` and parses the output into a
107    /// [`BinaryListSummary`].
108    pub fn exec_binaries_only(&self) -> Result<BinaryListSummary, CommandError> {
109        let mut command = self.cargo_command();
110        command.arg("--list-type=binaries-only");
111        let output = command.output().map_err(CommandError::Exec)?;
112
113        if !output.status.success() {
114            // The process exited with a non-zero code.
115            let exit_code = output.status.code();
116            let stderr = output.stderr;
117            return Err(CommandError::CommandFailed { exit_code, stderr });
118        }
119
120        // Try parsing stdout.
121        serde_json::from_slice(&output.stdout).map_err(CommandError::Json)
122    }
123}
124
125/// Root element for a serializable list of tests generated by nextest.
126#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
127#[serde(rename_all = "kebab-case")]
128#[non_exhaustive]
129pub struct TestListSummary {
130    /// Rust metadata used for builds and test runs.
131    pub rust_build_meta: RustBuildMetaSummary,
132
133    /// Number of tests (including skipped and ignored) across all binaries.
134    pub test_count: usize,
135
136    /// A map of Rust test suites to the test binaries within them, keyed by a unique identifier
137    /// for each test suite.
138    pub rust_suites: BTreeMap<RustBinaryId, RustTestSuiteSummary>,
139}
140
141impl TestListSummary {
142    /// Creates a new `TestListSummary` with the given Rust metadata.
143    pub fn new(rust_build_meta: RustBuildMetaSummary) -> Self {
144        Self {
145            rust_build_meta,
146            test_count: 0,
147            rust_suites: BTreeMap::new(),
148        }
149    }
150    /// Parse JSON output from `cargo nextest list --message-format json`.
151    pub fn parse_json(json: impl AsRef<str>) -> Result<Self, serde_json::Error> {
152        serde_json::from_str(json.as_ref())
153    }
154}
155
156/// The platform a binary was built on (useful for cross-compilation)
157#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
158#[serde(rename_all = "kebab-case")]
159pub enum BuildPlatform {
160    /// The target platform.
161    Target,
162
163    /// The host platform: the platform the build was performed on.
164    Host,
165}
166
167impl fmt::Display for BuildPlatform {
168    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169        match self {
170            Self::Target => write!(f, "target"),
171            Self::Host => write!(f, "host"),
172        }
173    }
174}
175
176/// A serializable Rust test binary.
177///
178/// Part of a [`RustTestSuiteSummary`] and [`BinaryListSummary`].
179#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
180#[serde(rename_all = "kebab-case")]
181pub struct RustTestBinarySummary {
182    /// A unique binary ID.
183    pub binary_id: RustBinaryId,
184
185    /// The name of the test binary within the package.
186    pub binary_name: String,
187
188    /// The unique package ID assigned by Cargo to this test.
189    ///
190    /// This package ID can be used for lookups in `cargo metadata`.
191    pub package_id: String,
192
193    /// The kind of Rust test binary this is.
194    pub kind: RustTestBinaryKind,
195
196    /// The path to the test binary executable.
197    pub binary_path: Utf8PathBuf,
198
199    /// Platform for which this binary was built.
200    /// (Proc-macro tests are built for the host.)
201    pub build_platform: BuildPlatform,
202}
203
204/// Information about the kind of a Rust test binary.
205///
206/// Kinds are used to generate [`RustBinaryId`] instances, and to figure out whether some
207/// environment variables should be set.
208#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deserialize, Serialize)]
209#[serde(transparent)]
210pub struct RustTestBinaryKind(pub Cow<'static, str>);
211
212impl RustTestBinaryKind {
213    /// Creates a new `RustTestBinaryKind` from a string.
214    #[inline]
215    pub fn new(kind: impl Into<Cow<'static, str>>) -> Self {
216        Self(kind.into())
217    }
218
219    /// Creates a new `RustTestBinaryKind` from a static string.
220    #[inline]
221    pub const fn new_const(kind: &'static str) -> Self {
222        Self(Cow::Borrowed(kind))
223    }
224
225    /// Returns the kind as a string.
226    pub fn as_str(&self) -> &str {
227        &self.0
228    }
229
230    /// The "lib" kind, used for unit tests within the library.
231    pub const LIB: Self = Self::new_const("lib");
232
233    /// The "test" kind, used for integration tests.
234    pub const TEST: Self = Self::new_const("test");
235
236    /// The "bench" kind, used for benchmarks.
237    pub const BENCH: Self = Self::new_const("bench");
238
239    /// The "bin" kind, used for unit tests within binaries.
240    pub const BIN: Self = Self::new_const("bin");
241
242    /// The "example" kind, used for unit tests within examples.
243    pub const EXAMPLE: Self = Self::new_const("example");
244
245    /// The "proc-macro" kind, used for tests within procedural macros.
246    pub const PROC_MACRO: Self = Self::new_const("proc-macro");
247}
248
249impl fmt::Display for RustTestBinaryKind {
250    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
251        write!(f, "{}", self.0)
252    }
253}
254
255/// A serializable suite of test binaries.
256#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
257#[serde(rename_all = "kebab-case")]
258pub struct BinaryListSummary {
259    /// Rust metadata used for builds and test runs.
260    pub rust_build_meta: RustBuildMetaSummary,
261
262    /// The list of Rust test binaries (indexed by binary-id).
263    pub rust_binaries: BTreeMap<RustBinaryId, RustTestBinarySummary>,
264}
265
266// IMPLEMENTATION NOTE: SmolStr is *not* part of the public API.
267
268/// A unique identifier for a test suite (a Rust binary).
269#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
270#[serde(transparent)]
271pub struct RustBinaryId(SmolStr);
272
273impl fmt::Display for RustBinaryId {
274    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
275        f.write_str(&self.0)
276    }
277}
278
279impl RustBinaryId {
280    /// Creates a new `RustBinaryId` from a string.
281    #[inline]
282    pub fn new(id: &str) -> Self {
283        Self(id.into())
284    }
285
286    /// Creates a new `RustBinaryId` from its constituent parts:
287    ///
288    /// * `package_name`: The name of the package as defined in `Cargo.toml`.
289    /// * `kind`: The kind of the target (see [`RustTestBinaryKind`]).
290    /// * `target_name`: The name of the target.
291    ///
292    /// The algorithm is as follows:
293    ///
294    /// 1. If the kind is `lib` or `proc-macro` (i.e. for unit tests), the binary ID is the same as
295    ///    the package name. There can only be one library per package, so this will always be
296    ///    unique.
297    /// 2. If the target is an integration test, the binary ID is `package_name::target_name`.
298    /// 3. Otherwise, the binary ID is `package_name::{kind}/{target_name}`.
299    ///
300    /// This format is part of nextest's stable API.
301    ///
302    /// # Examples
303    ///
304    /// ```
305    /// use nextest_metadata::{RustBinaryId, RustTestBinaryKind};
306    ///
307    /// // The lib and proc-macro kinds.
308    /// assert_eq!(
309    ///     RustBinaryId::from_parts("foo-lib", &RustTestBinaryKind::LIB, "foo_lib"),
310    ///     RustBinaryId::new("foo-lib"),
311    /// );
312    /// assert_eq!(
313    ///     RustBinaryId::from_parts("foo-derive", &RustTestBinaryKind::PROC_MACRO, "derive"),
314    ///     RustBinaryId::new("foo-derive"),
315    /// );
316    ///
317    /// // Integration tests.
318    /// assert_eq!(
319    ///     RustBinaryId::from_parts("foo-lib", &RustTestBinaryKind::TEST, "foo_test"),
320    ///     RustBinaryId::new("foo-lib::foo_test"),
321    /// );
322    ///
323    /// // Other kinds.
324    /// assert_eq!(
325    ///     RustBinaryId::from_parts("foo-lib", &RustTestBinaryKind::BIN, "foo_bin"),
326    ///     RustBinaryId::new("foo-lib::bin/foo_bin"),
327    /// );
328    /// ```
329    pub fn from_parts(package_name: &str, kind: &RustTestBinaryKind, target_name: &str) -> Self {
330        let mut id = package_name.to_owned();
331        // To ensure unique binary IDs, we use the following scheme:
332        if kind == &RustTestBinaryKind::LIB || kind == &RustTestBinaryKind::PROC_MACRO {
333            // 1. The binary ID is the same as the package name.
334        } else if kind == &RustTestBinaryKind::TEST {
335            // 2. For integration tests, use package_name::target_name. Cargo enforces unique names
336            //    for the same kind of targets in a package, so these will always be unique.
337            id.push_str("::");
338            id.push_str(target_name);
339        } else {
340            // 3. For all other target kinds, use a combination of the target kind and
341            //    the target name. For the same reason as above, these will always be
342            //    unique.
343            write!(id, "::{kind}/{target_name}").unwrap();
344        }
345
346        Self(id.into())
347    }
348
349    /// Returns the identifier as a string.
350    #[inline]
351    pub fn as_str(&self) -> &str {
352        &self.0
353    }
354
355    /// Returns the length of the identifier in bytes.
356    #[inline]
357    pub fn len(&self) -> usize {
358        self.0.len()
359    }
360
361    /// Returns `true` if the identifier is empty.
362    #[inline]
363    pub fn is_empty(&self) -> bool {
364        self.0.is_empty()
365    }
366
367    /// Returns the components of this identifier.
368    #[inline]
369    pub fn components(&self) -> RustBinaryIdComponents<'_> {
370        RustBinaryIdComponents::new(self)
371    }
372}
373
374impl<S> From<S> for RustBinaryId
375where
376    S: AsRef<str>,
377{
378    #[inline]
379    fn from(s: S) -> Self {
380        Self(s.as_ref().into())
381    }
382}
383
384impl Ord for RustBinaryId {
385    fn cmp(&self, other: &RustBinaryId) -> Ordering {
386        // Use the components as the canonical sort order.
387        //
388        // Note: this means that we can't impl Borrow<str> for RustBinaryId,
389        // since the Ord impl is inconsistent with that of &str.
390        self.components().cmp(&other.components())
391    }
392}
393
394impl PartialOrd for RustBinaryId {
395    fn partial_cmp(&self, other: &RustBinaryId) -> Option<Ordering> {
396        Some(self.cmp(other))
397    }
398}
399
400/// The components of a [`RustBinaryId`].
401///
402/// This defines the canonical sort order for a `RustBinaryId`.
403///
404/// Returned by [`RustBinaryId::components`].
405#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
406pub struct RustBinaryIdComponents<'a> {
407    /// The name of the package.
408    pub package_name: &'a str,
409
410    /// The kind and binary name, if specified.
411    pub binary_name_and_kind: RustBinaryIdNameAndKind<'a>,
412}
413
414impl<'a> RustBinaryIdComponents<'a> {
415    fn new(id: &'a RustBinaryId) -> Self {
416        let mut parts = id.as_str().splitn(2, "::");
417
418        let package_name = parts
419            .next()
420            .expect("splitn(2) returns at least 1 component");
421        let binary_name_and_kind = if let Some(suffix) = parts.next() {
422            let mut parts = suffix.splitn(2, '/');
423
424            let part1 = parts
425                .next()
426                .expect("splitn(2) returns at least 1 component");
427            if let Some(binary_name) = parts.next() {
428                RustBinaryIdNameAndKind::NameAndKind {
429                    kind: part1,
430                    binary_name,
431                }
432            } else {
433                RustBinaryIdNameAndKind::NameOnly { binary_name: part1 }
434            }
435        } else {
436            RustBinaryIdNameAndKind::None
437        };
438
439        Self {
440            package_name,
441            binary_name_and_kind,
442        }
443    }
444}
445
446/// The name and kind of a Rust binary, present within a [`RustBinaryId`].
447///
448/// Part of [`RustBinaryIdComponents`].
449#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
450pub enum RustBinaryIdNameAndKind<'a> {
451    /// The binary has no name or kind.
452    None,
453
454    /// The binary has a name but no kind.
455    NameOnly {
456        /// The name of the binary.
457        binary_name: &'a str,
458    },
459
460    /// The binary has a name and kind.
461    NameAndKind {
462        /// The kind of the binary.
463        kind: &'a str,
464
465        /// The name of the binary.
466        binary_name: &'a str,
467    },
468}
469
470/// Rust metadata used for builds and test runs.
471#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
472#[serde(rename_all = "kebab-case")]
473pub struct RustBuildMetaSummary {
474    /// The target directory for Rust artifacts.
475    pub target_directory: Utf8PathBuf,
476
477    /// Base output directories, relative to the target directory.
478    pub base_output_directories: BTreeSet<Utf8PathBuf>,
479
480    /// Information about non-test binaries, keyed by package ID.
481    pub non_test_binaries: BTreeMap<String, BTreeSet<RustNonTestBinarySummary>>,
482
483    /// Build script output directory, relative to the target directory and keyed by package ID.
484    /// Only present for workspace packages that have build scripts.
485    ///
486    /// Added in cargo-nextest 0.9.65.
487    #[serde(default)]
488    pub build_script_out_dirs: BTreeMap<String, Utf8PathBuf>,
489
490    /// Linked paths, relative to the target directory.
491    pub linked_paths: BTreeSet<Utf8PathBuf>,
492
493    /// The build platforms used while compiling the Rust artifacts.
494    ///
495    /// Added in cargo-nextest 0.9.72.
496    #[serde(default)]
497    pub platforms: Option<BuildPlatformsSummary>,
498
499    /// The target platforms used while compiling the Rust artifacts.
500    ///
501    /// Deprecated in favor of [`Self::platforms`]; use that if available.
502    #[serde(default)]
503    pub target_platforms: Vec<PlatformSummary>,
504
505    /// A deprecated form of the target platform used for cross-compilation, if any.
506    ///
507    /// Deprecated in favor of (in order) [`Self::platforms`] and [`Self::target_platforms`]; use
508    /// those if available.
509    #[serde(default)]
510    pub target_platform: Option<String>,
511}
512
513/// A non-test Rust binary. Used to set the correct environment
514/// variables in reused builds.
515#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
516#[serde(rename_all = "kebab-case")]
517pub struct RustNonTestBinarySummary {
518    /// The name of the binary.
519    pub name: String,
520
521    /// The kind of binary this is.
522    pub kind: RustNonTestBinaryKind,
523
524    /// The path to the binary, relative to the target directory.
525    pub path: Utf8PathBuf,
526}
527
528/// Serialized representation of the host and the target platform.
529#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
530#[serde(rename_all = "kebab-case")]
531pub struct BuildPlatformsSummary {
532    /// The host platform used while compiling the Rust artifacts.
533    pub host: HostPlatformSummary,
534
535    /// The target platforms used while compiling the Rust artifacts.
536    ///
537    /// With current versions of nextest, this will contain at most one element.
538    pub targets: Vec<TargetPlatformSummary>,
539}
540
541/// Serialized representation of the host platform.
542#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
543#[serde(rename_all = "kebab-case")]
544pub struct HostPlatformSummary {
545    /// The host platform, if specified.
546    pub platform: PlatformSummary,
547
548    /// The libdir for the host platform.
549    pub libdir: PlatformLibdirSummary,
550}
551
552/// Serialized representation of the target platform.
553#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
554#[serde(rename_all = "kebab-case")]
555pub struct TargetPlatformSummary {
556    /// The target platform, if specified.
557    pub platform: PlatformSummary,
558
559    /// The libdir for the target platform.
560    ///
561    /// Err if we failed to discover it.
562    pub libdir: PlatformLibdirSummary,
563}
564
565/// Serialized representation of a platform's library directory.
566#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
567#[serde(tag = "status", rename_all = "kebab-case")]
568pub enum PlatformLibdirSummary {
569    /// The libdir is available.
570    Available {
571        /// The libdir.
572        path: Utf8PathBuf,
573    },
574
575    /// The libdir is unavailable, for the reason provided in the inner value.
576    Unavailable {
577        /// The reason why the libdir is unavailable.
578        reason: PlatformLibdirUnavailable,
579    },
580}
581
582/// The reason why a platform libdir is unavailable.
583///
584/// Part of [`PlatformLibdirSummary`].
585///
586/// This is an open-ended enum that may have additional deserializable variants in the future.
587#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
588pub struct PlatformLibdirUnavailable(pub Cow<'static, str>);
589
590impl PlatformLibdirUnavailable {
591    /// The libdir is not available because the rustc invocation to obtain it failed.
592    pub const RUSTC_FAILED: Self = Self::new_const("rustc-failed");
593
594    /// The libdir is not available because it was attempted to be read from rustc, but there was an
595    /// issue with its output.
596    pub const RUSTC_OUTPUT_ERROR: Self = Self::new_const("rustc-output-error");
597
598    /// The libdir is unavailable because it was deserialized from a summary serialized by an older
599    /// version of nextest.
600    pub const OLD_SUMMARY: Self = Self::new_const("old-summary");
601
602    /// The libdir is unavailable because a build was reused from an archive, and the libdir was not
603    /// present in the archive
604    pub const NOT_IN_ARCHIVE: Self = Self::new_const("not-in-archive");
605
606    /// Converts a static string into Self.
607    pub const fn new_const(reason: &'static str) -> Self {
608        Self(Cow::Borrowed(reason))
609    }
610
611    /// Converts a string into Self.
612    pub fn new(reason: impl Into<Cow<'static, str>>) -> Self {
613        Self(reason.into())
614    }
615
616    /// Returns self as a string.
617    pub fn as_str(&self) -> &str {
618        &self.0
619    }
620}
621
622/// Information about the kind of a Rust non-test binary.
623///
624/// This is part of [`RustNonTestBinarySummary`], and is used to determine runtime environment
625/// variables.
626#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deserialize, Serialize)]
627#[serde(transparent)]
628pub struct RustNonTestBinaryKind(pub Cow<'static, str>);
629
630impl RustNonTestBinaryKind {
631    /// Creates a new `RustNonTestBinaryKind` from a string.
632    #[inline]
633    pub fn new(kind: impl Into<Cow<'static, str>>) -> Self {
634        Self(kind.into())
635    }
636
637    /// Creates a new `RustNonTestBinaryKind` from a static string.
638    #[inline]
639    pub const fn new_const(kind: &'static str) -> Self {
640        Self(Cow::Borrowed(kind))
641    }
642
643    /// Returns the kind as a string.
644    pub fn as_str(&self) -> &str {
645        &self.0
646    }
647
648    /// The "dylib" kind, used for dynamic libraries (`.so` on Linux). Also used for
649    /// .pdb and other similar files on Windows.
650    pub const DYLIB: Self = Self::new_const("dylib");
651
652    /// The "bin-exe" kind, used for binary executables.
653    pub const BIN_EXE: Self = Self::new_const("bin-exe");
654}
655
656impl fmt::Display for RustNonTestBinaryKind {
657    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
658        write!(f, "{}", self.0)
659    }
660}
661
662/// A serializable suite of tests within a Rust test binary.
663///
664/// Part of a [`TestListSummary`].
665#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
666#[serde(rename_all = "kebab-case")]
667pub struct RustTestSuiteSummary {
668    /// The name of this package in the workspace.
669    pub package_name: String,
670
671    /// The binary within the package.
672    #[serde(flatten)]
673    pub binary: RustTestBinarySummary,
674
675    /// The working directory that tests within this package are run in.
676    pub cwd: Utf8PathBuf,
677
678    /// Status of this test suite.
679    ///
680    /// Introduced in cargo-nextest 0.9.25. Older versions always imply
681    /// [`LISTED`](RustTestSuiteStatusSummary::LISTED).
682    #[serde(default = "listed_status")]
683    pub status: RustTestSuiteStatusSummary,
684
685    /// Test cases within this test suite.
686    #[serde(rename = "testcases")]
687    pub test_cases: BTreeMap<String, RustTestCaseSummary>,
688}
689
690fn listed_status() -> RustTestSuiteStatusSummary {
691    RustTestSuiteStatusSummary::LISTED
692}
693
694/// Information about whether a test suite was listed or skipped.
695///
696/// This is part of [`RustTestSuiteSummary`].
697#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deserialize, Serialize)]
698#[serde(transparent)]
699pub struct RustTestSuiteStatusSummary(pub Cow<'static, str>);
700
701impl RustTestSuiteStatusSummary {
702    /// Creates a new `RustNonTestBinaryKind` from a string.
703    #[inline]
704    pub fn new(kind: impl Into<Cow<'static, str>>) -> Self {
705        Self(kind.into())
706    }
707
708    /// Creates a new `RustNonTestBinaryKind` from a static string.
709    #[inline]
710    pub const fn new_const(kind: &'static str) -> Self {
711        Self(Cow::Borrowed(kind))
712    }
713
714    /// Returns the kind as a string.
715    pub fn as_str(&self) -> &str {
716        &self.0
717    }
718
719    /// The "listed" kind, which means that the test binary was executed with `--list` to gather the
720    /// list of tests in it.
721    pub const LISTED: Self = Self::new_const("listed");
722
723    /// The "skipped" kind, which indicates that the test binary was not executed because it didn't
724    /// match any filtersets.
725    ///
726    /// In this case, the contents of [`RustTestSuiteSummary::test_cases`] is empty.
727    pub const SKIPPED: Self = Self::new_const("skipped");
728
729    /// The binary doesn't match the profile's `default-filter`.
730    ///
731    /// This is the lowest-priority reason for skipping a binary.
732    pub const SKIPPED_DEFAULT_FILTER: Self = Self::new_const("skipped-default-filter");
733}
734
735/// Serializable information about an individual test case within a Rust test suite.
736///
737/// Part of a [`RustTestSuiteSummary`].
738#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
739#[serde(rename_all = "kebab-case")]
740pub struct RustTestCaseSummary {
741    /// Returns true if this test is marked ignored.
742    ///
743    /// Ignored tests, if run, are executed with the `--ignored` argument.
744    pub ignored: bool,
745
746    /// Whether the test matches the provided test filter.
747    ///
748    /// Only tests that match the filter are run.
749    pub filter_match: FilterMatch,
750}
751
752/// An enum describing whether a test matches a filter.
753#[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
754#[serde(rename_all = "kebab-case", tag = "status")]
755pub enum FilterMatch {
756    /// This test matches this filter.
757    Matches,
758
759    /// This test does not match this filter.
760    Mismatch {
761        /// Describes the reason this filter isn't matched.
762        reason: MismatchReason,
763    },
764}
765
766impl FilterMatch {
767    /// Returns true if the filter doesn't match.
768    pub fn is_match(&self) -> bool {
769        matches!(self, FilterMatch::Matches)
770    }
771}
772
773/// The reason for why a test doesn't match a filter.
774#[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
775#[serde(rename_all = "kebab-case")]
776#[non_exhaustive]
777pub enum MismatchReason {
778    /// This test does not match the run-ignored option in the filter.
779    Ignored,
780
781    /// This test does not match the provided string filters.
782    String,
783
784    /// This test does not match the provided expression filters.
785    Expression,
786
787    /// This test is in a different partition.
788    Partition,
789
790    /// This test is filtered out by the default-filter.
791    ///
792    /// This is the lowest-priority reason for skipping a test.
793    DefaultFilter,
794}
795
796impl fmt::Display for MismatchReason {
797    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
798        match self {
799            MismatchReason::Ignored => write!(f, "does not match the run-ignored option"),
800            MismatchReason::String => write!(f, "does not match the provided string filters"),
801            MismatchReason::Expression => {
802                write!(f, "does not match the provided expression filters")
803            }
804            MismatchReason::Partition => write!(f, "is in a different partition"),
805            MismatchReason::DefaultFilter => {
806                write!(f, "is filtered out by the profile's default-filter")
807            }
808        }
809    }
810}
811
812#[cfg(test)]
813mod tests {
814    use super::*;
815    use test_case::test_case;
816
817    #[test_case(r#"{
818        "target-directory": "/foo",
819        "base-output-directories": [],
820        "non-test-binaries": {},
821        "linked-paths": []
822    }"#, RustBuildMetaSummary {
823        target_directory: "/foo".into(),
824        base_output_directories: BTreeSet::new(),
825        non_test_binaries: BTreeMap::new(),
826        build_script_out_dirs: BTreeMap::new(),
827        linked_paths: BTreeSet::new(),
828        target_platform: None,
829        target_platforms: vec![],
830        platforms: None,
831    }; "no target platform")]
832    #[test_case(r#"{
833        "target-directory": "/foo",
834        "base-output-directories": [],
835        "non-test-binaries": {},
836        "linked-paths": [],
837        "target-platform": "x86_64-unknown-linux-gnu"
838    }"#, RustBuildMetaSummary {
839        target_directory: "/foo".into(),
840        base_output_directories: BTreeSet::new(),
841        non_test_binaries: BTreeMap::new(),
842        build_script_out_dirs: BTreeMap::new(),
843        linked_paths: BTreeSet::new(),
844        target_platform: Some("x86_64-unknown-linux-gnu".to_owned()),
845        target_platforms: vec![],
846        platforms: None,
847    }; "single target platform specified")]
848    fn test_deserialize_old_rust_build_meta(input: &str, expected: RustBuildMetaSummary) {
849        let build_meta: RustBuildMetaSummary =
850            serde_json::from_str(input).expect("input deserialized correctly");
851        assert_eq!(
852            build_meta, expected,
853            "deserialized input matched expected output"
854        );
855    }
856
857    #[test]
858    fn test_binary_id_ord() {
859        let empty = RustBinaryId::new("");
860        let foo = RustBinaryId::new("foo");
861        let bar = RustBinaryId::new("bar");
862        let foo_name1 = RustBinaryId::new("foo::name1");
863        let foo_name2 = RustBinaryId::new("foo::name2");
864        let bar_name = RustBinaryId::new("bar::name");
865        let foo_bin_name1 = RustBinaryId::new("foo::bin/name1");
866        let foo_bin_name2 = RustBinaryId::new("foo::bin/name2");
867        let bar_bin_name = RustBinaryId::new("bar::bin/name");
868        let foo_proc_macro_name = RustBinaryId::new("foo::proc_macro/name");
869        let bar_proc_macro_name = RustBinaryId::new("bar::proc_macro/name");
870
871        // This defines the expected sort order.
872        let sorted_ids = [
873            empty,
874            bar,
875            bar_name,
876            bar_bin_name,
877            bar_proc_macro_name,
878            foo,
879            foo_name1,
880            foo_name2,
881            foo_bin_name1,
882            foo_bin_name2,
883            foo_proc_macro_name,
884        ];
885
886        for (i, id) in sorted_ids.iter().enumerate() {
887            for (j, other_id) in sorted_ids.iter().enumerate() {
888                let expected = i.cmp(&j);
889                assert_eq!(
890                    id.cmp(other_id),
891                    expected,
892                    "comparing {id:?} to {other_id:?} gave {expected:?}"
893                );
894            }
895        }
896    }
897}