nextest-runner 0.122.1

Core runner logic for cargo nextest.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use guppy::PackageId;
use nextest_metadata::{RustNonTestBinaryKind, RustNonTestBinarySummary};
use std::collections::{BTreeMap, BTreeSet, HashSet};

/// A collection of non-test binaries that are part of a build.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(crate) struct RustNonTestBinaries {
    by_package_id: BTreeMap<PackageId, BTreeSet<RustNonTestBinarySummary>>,
}

impl RustNonTestBinaries {
    pub(crate) fn from_summary(
        summary: BTreeMap<String, BTreeSet<RustNonTestBinarySummary>>,
    ) -> Self {
        Self {
            by_package_id: summary
                .into_iter()
                .map(|(package_id, files)| (PackageId::new(package_id), files))
                .collect(),
        }
    }

    pub(crate) fn to_summary(&self) -> BTreeMap<String, BTreeSet<RustNonTestBinarySummary>> {
        self.by_package_id
            .iter()
            .map(|(package_id, files)| (package_id.repr().to_owned(), files.clone()))
            .collect()
    }

    pub(crate) fn insert(&mut self, package_id: PackageId, file: RustNonTestBinarySummary) {
        self.by_package_id
            .entry(package_id)
            .or_default()
            .insert(file);
    }

    /// Returns the number of distinct (package ID, name, kind, build platform)
    /// tuples of non-test binaries in this collection.
    ///
    /// One binary can be stored as several files sharing a name and kind (e.g.,
    /// on Windows, a dylib comes with an import library, an export library, and
    /// a .pdb). This function treats that as a single binary.
    pub(crate) fn binary_count(&self) -> usize {
        self.by_package_id.values().map(binary_count).sum()
    }

    pub(crate) fn files(&self) -> impl Iterator<Item = &RustNonTestBinarySummary> {
        self.by_package_id.values().flatten()
    }

    pub(crate) fn files_for_package(
        &self,
        package_id: &PackageId,
    ) -> impl Iterator<Item = &RustNonTestBinarySummary> {
        self.by_package_id.get(package_id).into_iter().flatten()
    }

    /// Partitions the non-test binaries in this collection for use with an archive.
    ///
    /// This drops package-scoped binaries owned by packages with no archived test binary.
    pub(crate) fn partition_for_archive(
        &self,
        relevant_package_ids: &HashSet<&str>,
    ) -> PartitionedNonTestBinaries {
        self.partition(|package_id, binary| {
            relevant_package_ids.contains(package_id.repr()) || !is_package_scoped(&binary.kind)
        })
    }

    fn partition(
        &self,
        mut retain: impl FnMut(&PackageId, &RustNonTestBinarySummary) -> bool,
    ) -> PartitionedNonTestBinaries {
        let mut by_package_id = BTreeMap::new();
        let mut filtered_out_binary_count = 0;

        for (package_id, files) in &self.by_package_id {
            let retained: BTreeSet<_> = files
                .iter()
                .filter(|file| retain(package_id, file))
                .cloned()
                .collect();

            filtered_out_binary_count += binary_count(files) - binary_count(&retained);
            // If nothing was retained, don't bother adding it to the result.
            if !retained.is_empty() {
                by_package_id.insert(package_id.clone(), retained);
            }
        }

        PartitionedNonTestBinaries {
            retained: Self { by_package_id },
            filtered_out_binary_count,
        }
    }
}

pub(crate) struct PartitionedNonTestBinaries {
    pub(crate) retained: RustNonTestBinaries,
    pub(crate) filtered_out_binary_count: usize,
}

fn binary_count(files: &BTreeSet<RustNonTestBinarySummary>) -> usize {
    files
        .iter()
        .map(|file| (file.name.as_str(), &file.kind, file.build_platform))
        .collect::<BTreeSet<_>>()
        .len()
}

/// Returns true if this non-test binary is package-scoped.
///
/// This returns true for bin-exes (via `CARGO_BIN_EXE_<name>`) and false for
/// other kinds.
fn is_package_scoped(kind: &RustNonTestBinaryKind) -> bool {
    *kind == RustNonTestBinaryKind::BIN_EXE
}

#[cfg(test)]
mod tests {
    use super::*;
    use nextest_metadata::BuildPlatform;

    #[test]
    fn binary_count_is_platform_stable() {
        let unix = RustNonTestBinaries::from_summary(BTreeMap::from([
            (
                "fixture-project".to_owned(),
                BTreeSet::from([
                    bin_exe("fixture-project", "debug/fixture-project"),
                    bin_exe("other", "debug/other"),
                    bin_exe("wrapper", "debug/wrapper"),
                ]),
            ),
            (
                "dylib-test".to_owned(),
                BTreeSet::from([dylib("dylib_test", "debug/libdylib_test.so")]),
            ),
        ]));

        let windows = RustNonTestBinaries::from_summary(BTreeMap::from([
            (
                "fixture-project".to_owned(),
                BTreeSet::from([
                    bin_exe("fixture-project", "debug/fixture-project.exe"),
                    bin_exe("other", "debug/other.exe"),
                    bin_exe("wrapper", "debug/wrapper.exe"),
                ]),
            ),
            (
                "dylib-test".to_owned(),
                windows_dylib("dylib_test").into_iter().collect(),
            ),
        ]));

        assert_eq!(
            unix.files().count(),
            4,
            "on Unix, each binary is stored as exactly one file"
        );
        assert_eq!(
            unix.binary_count(),
            4,
            "3 executables and 1 dylib, spread across 2 packages"
        );

        assert_eq!(
            windows.files().count(),
            7,
            "on Windows, the dylib is stored as 4 files"
        );
        assert_eq!(
            windows.binary_count(),
            4,
            "the same 4 binaries: a dylib's import library, export library, and \
             .pdb are stored as separate files but are not separate binaries"
        );
    }

    #[test]
    fn binary_count_counts_distinct_targets() {
        let non_test_binaries = RustNonTestBinaries::from_summary(BTreeMap::from([
            (
                "pkg-a".to_owned(),
                BTreeSet::from([bin_exe("helper", "debug/helper")]),
            ),
            (
                "pkg-b".to_owned(),
                BTreeSet::from([bin_exe("helper", "debug/helper")]),
            ),
            (
                "bin-and-lib".to_owned(),
                BTreeSet::from([
                    bin_exe("dual", "debug/dual"),
                    dylib("dual", "debug/libdual.so"),
                ]),
            ),
            (
                "host-and-target".to_owned(),
                BTreeSet::from([
                    host_dylib("cross", "debug/libcross.so"),
                    dylib("cross", "aarch64-unknown-linux-gnu/debug/libcross.so"),
                ]),
            ),
        ]));

        assert_eq!(
            non_test_binaries.binary_count(),
            6,
            "one per distinct (package, name, kind, build platform): pkg-a 1, pkg-b 1, \
             bin-and-lib 2 (one name, two kinds), host-and-target 2 (one name and kind, two \
             compilations)"
        );
    }

    #[test]
    fn partition_counts_binaries() {
        let non_test_binaries = RustNonTestBinaries::from_summary(BTreeMap::from([
            (
                "with-tests".to_owned(),
                BTreeSet::from([bin_exe("helper", "debug/helper")]),
            ),
            (
                "three-bins".to_owned(),
                BTreeSet::from([
                    bin_exe("one", "debug/one"),
                    bin_exe("two", "debug/two"),
                    bin_exe("three", "debug/three"),
                ]),
            ),
            (
                "mixed".to_owned(),
                BTreeSet::from([
                    bin_exe("mixed-bin", "debug/mixed-bin"),
                    dylib("mixed_dylib", "debug/libmixed_dylib.so"),
                ]),
            ),
            (
                "dylib-only".to_owned(),
                BTreeSet::from([dylib("only_dylib", "debug/libonly_dylib.so")]),
            ),
            (
                "windows-dylib-only".to_owned(),
                windows_dylib("win_dylib").into_iter().collect(),
            ),
        ]));

        let partitioned = non_test_binaries
            .partition(|package_id, _| matches!(package_id.repr(), "with-tests" | "mixed"));

        assert_eq!(
            partitioned.retained.to_summary(),
            BTreeMap::from([
                (
                    "with-tests".to_owned(),
                    BTreeSet::from([bin_exe("helper", "debug/helper")]),
                ),
                (
                    "mixed".to_owned(),
                    BTreeSet::from([
                        bin_exe("mixed-bin", "debug/mixed-bin"),
                        dylib("mixed_dylib", "debug/libmixed_dylib.so"),
                    ]),
                ),
            ]),
            "retained packages keep all of their files"
        );
        assert_eq!(
            partitioned.retained.binary_count(),
            3,
            "retained binaries are counted the same way as filtered-out ones"
        );
        assert_eq!(
            partitioned.filtered_out_binary_count, 5,
            "filtered-out count is a count of binaries (3 + 1 + 1), not of packages (3) \
             or of files (3 + 1 + 4)"
        );
    }

    #[test]
    fn partition_for_archive_only_scopes_bin_exes() {
        let non_test_binaries = RustNonTestBinaries::from_summary(BTreeMap::from([
            (
                "with-tests".to_owned(),
                BTreeSet::from([bin_exe("helper", "debug/helper")]),
            ),
            (
                "three-bins".to_owned(),
                BTreeSet::from([
                    bin_exe("one", "debug/one"),
                    bin_exe("two", "debug/two"),
                    bin_exe("three", "debug/three"),
                ]),
            ),
            (
                "mixed".to_owned(),
                BTreeSet::from([
                    bin_exe("mixed-bin", "debug/mixed-bin"),
                    dylib("mixed_dylib", "debug/libmixed_dylib.so"),
                ]),
            ),
            (
                "mixed-no-tests".to_owned(),
                BTreeSet::from([
                    bin_exe("untested-bin", "debug/untested-bin"),
                    dylib("untested_dylib", "debug/libuntested_dylib.so"),
                ]),
            ),
            (
                "dylib-only".to_owned(),
                BTreeSet::from([dylib("only_dylib", "debug/libonly_dylib.so")]),
            ),
            (
                "unknown-only".to_owned(),
                BTreeSet::from([future_kind(
                    "from_a_newer_nextest",
                    "debug/from_a_newer_nextest",
                )]),
            ),
        ]));

        let relevant_package_ids = HashSet::from(["with-tests", "mixed"]);
        let partitioned = non_test_binaries.partition_for_archive(&relevant_package_ids);

        assert_eq!(
            partitioned.retained.to_summary(),
            BTreeMap::from([
                (
                    "with-tests".to_owned(),
                    BTreeSet::from([bin_exe("helper", "debug/helper")]),
                ),
                (
                    "mixed".to_owned(),
                    BTreeSet::from([
                        bin_exe("mixed-bin", "debug/mixed-bin"),
                        dylib("mixed_dylib", "debug/libmixed_dylib.so"),
                    ]),
                ),
                (
                    "mixed-no-tests".to_owned(),
                    BTreeSet::from([dylib("untested_dylib", "debug/libuntested_dylib.so")]),
                ),
                (
                    "dylib-only".to_owned(),
                    BTreeSet::from([dylib("only_dylib", "debug/libonly_dylib.so")]),
                ),
                (
                    "unknown-only".to_owned(),
                    BTreeSet::from([future_kind(
                        "from_a_newer_nextest",
                        "debug/from_a_newer_nextest",
                    )]),
                ),
            ]),
            "only bin-exes are package-scoped: dylibs and unrecognized kinds are always retained, \
             and packages left with no binaries are dropped"
        );
        assert_eq!(
            partitioned.filtered_out_binary_count, 4,
            "3 bin-exes are dropped from three-bins and 1 from mixed-no-tests; counting map \
             entries that vanished, as the old code did, would report 1"
        );
    }

    fn bin_exe(name: &str, path: &str) -> RustNonTestBinarySummary {
        RustNonTestBinarySummary {
            name: name.to_owned(),
            kind: RustNonTestBinaryKind::BIN_EXE,
            path: path.into(),
            build_platform: Some(BuildPlatform::Target),
        }
    }

    fn dylib(name: &str, path: &str) -> RustNonTestBinarySummary {
        RustNonTestBinarySummary {
            name: name.to_owned(),
            kind: RustNonTestBinaryKind::DYLIB,
            path: path.into(),
            build_platform: Some(BuildPlatform::Target),
        }
    }

    fn host_dylib(name: &str, path: &str) -> RustNonTestBinarySummary {
        RustNonTestBinarySummary {
            name: name.to_owned(),
            kind: RustNonTestBinaryKind::DYLIB,
            path: path.into(),
            build_platform: Some(BuildPlatform::Host),
        }
    }

    fn future_kind(name: &str, path: &str) -> RustNonTestBinarySummary {
        RustNonTestBinarySummary {
            name: name.to_owned(),
            kind: RustNonTestBinaryKind::new("some-future-kind"),
            path: path.into(),
            build_platform: Some(BuildPlatform::Target),
        }
    }

    fn windows_dylib(name: &str) -> [RustNonTestBinarySummary; 4] {
        ["dll", "dll.lib", "dll.exp", "pdb"].map(|extension| RustNonTestBinarySummary {
            name: name.to_owned(),
            kind: RustNonTestBinaryKind::DYLIB,
            path: format!("debug/{name}.{extension}").into(),
            build_platform: Some(BuildPlatform::Target),
        })
    }
}