cargo-soothfast 0.4.3

CLI for soothfast: measure, gate, docs, spec, report
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Building, collecting and running the selected packages' test targets.
//! Runs go through cargo, which owns the test environment (`[env]` config,
//! target runners, library paths).

use std::collections::{BTreeSet, VecDeque};
use std::io::{self, BufRead, BufReader, IsTerminal};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use serde_json::{Value, json};

use super::select::Package;
use super::shim;
use crate::invoke;

/// One executable cargo built.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct TestBin {
    pub package: String,
    pub target: String,
    /// The target's kinds: `lib`, `bin`, `test`, `bench`, `example`, and
    /// crate types such as `cdylib` alongside `rlib`.
    pub kinds: Vec<String>,
    /// Whether cargo runs the package's doc tests for this target.
    pub doctest: bool,
    pub exe: PathBuf,
}

impl TestBin {
    /// `package target (kind)`, how the summary names a binary.
    pub fn label(&self) -> String {
        format!(
            "{} {} ({})",
            self.package,
            self.target,
            self.kinds.join(",")
        )
    }

    /// Whether the package's doc tests belong to this target.
    pub fn has_doc_tests(&self) -> bool {
        self.doctest
    }

    /// The cargo flags that select exactly this target. A library's kinds
    /// are its crate types, so anything not a named kind is the library.
    pub fn target_flags(&self) -> Vec<String> {
        match self.kinds.first().map(String::as_str) {
            Some(kind @ ("bin" | "test" | "bench" | "example")) => {
                vec![format!("--{kind}"), self.target.clone()]
            }
            _ => vec!["--lib".into()],
        }
    }
}

/// `cargo test LEAD -p NAME... CARGO_ARGS EXTRA [-- RUN_ARGS]`.
fn cargo_test(
    lead: &[&str],
    names: &[String],
    cargo_args: &[String],
    extra: &[String],
    run_args: &[String],
) -> Command {
    let mut cmd = Command::new("cargo");
    cmd.arg("test").args(lead);
    for n in names {
        cmd.args(["-p", n]);
    }
    cmd.args(cargo_args).args(extra);
    if !run_args.is_empty() {
        cmd.arg("--").args(run_args);
    }
    cmd
}

/// `cargo test --no-run` for `names`, returning cargo's JSON messages.
/// Compiler diagnostics are echoed to stderr as they would read normally.
pub fn build(names: &[String], cargo_args: &[String]) -> io::Result<Vec<Value>> {
    let mut cmd = cargo_test(
        &["--no-run", "--message-format=json"],
        names,
        cargo_args,
        &[],
        &[],
    );
    cmd.stdout(Stdio::piped());
    cmd.stderr(Stdio::inherit());
    let out = cmd.output()?;
    let messages: Vec<Value> = invoke::cargo_messages(&out.stdout).collect();
    for m in messages
        .iter()
        .filter(|m| m["reason"] == "compiler-message")
    {
        if let Some(rendered) = m["message"]["rendered"].as_str() {
            eprint!("{rendered}");
        }
    }
    if !out.status.success() {
        return Err(io::Error::other("cargo test --no-run failed"));
    }
    Ok(messages)
}

/// The run step's one `cargo test`: the build's invocation without
/// `--no-run`, the shim as runner for every triple, and every target run
/// whatever fails first.
pub struct RunStep<'a> {
    pub names: &'a [String],
    pub cargo_args: &'a [String],
    pub run_args: &'a [String],
    pub triples: &'a [String],
    pub plan: &'a Path,
    /// The shim cargo calls as runner.
    pub shim: &'a Path,
    /// The rustdoc wrapper, under `--no-doc`.
    pub rustdoc: Option<&'a Path>,
}

/// How the run step's cargo exited, and whether its output named a failed
/// doc test.
pub struct RunOutcome {
    pub success: bool,
    pub code: Option<i32>,
    pub doc_failed: bool,
    /// The last lines cargo wrote to stderr.
    pub tail: Vec<String>,
}

const TAIL: usize = 20;

pub fn run_step(step: &RunStep) -> io::Result<RunOutcome> {
    let runner = json!([step.shim, "soothfast", shim::SUBCOMMAND]).to_string();
    let mut extra: Vec<String> = step
        .triples
        .iter()
        .flat_map(|t| ["--config".into(), format!("target.\"{t}\".runner={runner}")])
        .collect();
    if std::io::stderr().is_terminal() && !step.cargo_args.iter().any(|a| a.starts_with("--color"))
    {
        extra.push("--color=always".into());
    }
    let mut cmd = cargo_test(
        &["--no-fail-fast"],
        step.names,
        step.cargo_args,
        &extra,
        step.run_args,
    );
    cmd.env(shim::PLAN_ENV, step.plan);
    if let Some(rustdoc) = step.rustdoc {
        cmd.env("RUSTDOC", rustdoc);
    }
    let mut child = cmd.stderr(Stdio::piped()).spawn()?;
    let mut tail = VecDeque::with_capacity(TAIL);
    let mut doc_failed = false;
    if let Some(stderr) = child.stderr.take() {
        for line in BufReader::new(stderr).lines() {
            let line = line?;
            eprintln!("{line}");
            doc_failed |= names_failed_doc_test(&line);
            if tail.len() == TAIL {
                tail.pop_front();
            }
            tail.push_back(line);
        }
    }
    let status = child.wait()?;
    Ok(RunOutcome {
        success: status.success(),
        code: status.code(),
        doc_failed,
        tail: tail.into(),
    })
}

/// Cargo's line for a failed doc test: `doctest failed, to rerun pass ...`
/// on its own, or a `` `-p NAME --doc` `` entry in the no-fail-fast list.
fn names_failed_doc_test(line: &str) -> bool {
    line.contains("doctest failed")
        || (line.trim_start().starts_with('`') && line.contains("--doc`"))
}

/// Run one already-built target through `cargo test`, output streaming.
/// Whether it exited successfully. The run step falls back to this when a
/// configured runner cannot be chained through the shim.
pub fn run_target(bin: &TestBin, cargo_args: &[String], run_args: &[String]) -> io::Result<bool> {
    let names = std::slice::from_ref(&bin.package);
    let mut cmd = cargo_test(&[], names, cargo_args, &bin.target_flags(), run_args);
    Ok(cmd.status()?.success())
}

/// `cargo test --doc` for `names`; executables cannot be cached, so doc tests
/// always run.
pub fn doc_tests(names: &[String], cargo_args: &[String], run_args: &[String]) -> io::Result<bool> {
    let mut cmd = cargo_test(&["--doc"], names, cargo_args, &[], run_args);
    Ok(cmd.status()?.success())
}

/// Whether cargo flags already pick test targets, which `--doc` refuses to
/// combine with.
pub fn narrows_targets(cargo_args: &[String]) -> bool {
    const FLAGS: &[&str] = &[
        "--lib",
        "--bin",
        "--bins",
        "--test",
        "--tests",
        "--example",
        "--examples",
        "--bench",
        "--benches",
        "--all-targets",
    ];
    cargo_args
        .iter()
        .any(|a| FLAGS.contains(&a.split_once('=').map_or(a.as_str(), |(f, _)| f)))
}

/// What a build produced for the workspace's packages.
#[derive(Debug, Default, PartialEq)]
pub struct Built {
    /// Test executables.
    pub tests: Vec<TestBin>,
    /// Plain `bin` and `example` executables. Integration tests spawn these
    /// without embedding them, so a test's key must cover their code too.
    pub siblings: Vec<TestBin>,
}

/// Every workspace executable named in cargo's messages, tagged with the
/// package that owns it.
pub fn collect(messages: &[Value], pkgs: &[Package]) -> Built {
    let mut tests = BTreeSet::new();
    let mut siblings = BTreeSet::new();
    for m in messages
        .iter()
        .filter(|m| m["reason"] == "compiler-artifact")
    {
        let Some(bin) = executable(m, pkgs) else {
            continue;
        };
        if m["profile"]["test"] == true {
            tests.insert(bin);
        } else if bin.kinds.iter().any(|k| k == "bin" || k == "example") {
            siblings.insert(bin);
        }
    }
    Built {
        tests: tests.into_iter().collect(),
        siblings: siblings.into_iter().collect(),
    }
}

fn executable(m: &Value, pkgs: &[Package]) -> Option<TestBin> {
    let exe = PathBuf::from(m["executable"].as_str()?);
    let manifest = PathBuf::from(m["manifest_path"].as_str()?);
    let package = pkgs
        .iter()
        .find(|p| manifest.parent() == Some(p.manifest_dir.as_path()))?;
    let kinds = m["target"]["kind"]
        .as_array()?
        .iter()
        .filter_map(|k| k.as_str().map(String::from))
        .collect();
    Some(TestBin {
        package: package.name.clone(),
        target: m["target"]["name"].as_str()?.to_string(),
        kinds,
        doctest: m["target"]["doctest"] == true,
        exe,
    })
}

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

    #[test]
    fn cargo_lines_naming_a_failed_doc_test_are_recognized() {
        assert!(names_failed_doc_test(
            "error: doctest failed, to rerun pass `-p a --doc`"
        ));
        assert!(names_failed_doc_test("    `-p a --doc`"));
        assert!(!names_failed_doc_test("    `-p a --test it`"));
        assert!(!names_failed_doc_test(
            "error: could not compile `a` (lib test)"
        ));
    }

    fn pkg(name: &str) -> Package {
        Package {
            name: name.into(),
            dir: name.into(),
            manifest_dir: PathBuf::from("/ws").join(name),
            deps: Vec::new(),
        }
    }

    fn artifact(
        manifest: &str,
        target: &str,
        kinds: &[&str],
        test: bool,
        exe: Option<&str>,
    ) -> String {
        let doctest = kinds
            .iter()
            .any(|k| ["lib", "rlib", "proc-macro"].contains(k));
        json!({ "reason": "compiler-artifact", "manifest_path": manifest,
                "target": { "name": target, "kind": kinds, "doctest": doctest },
                "profile": { "test": test }, "executable": exe })
        .to_string()
    }

    fn collected(lines: &[String], pkgs: &[Package]) -> Built {
        let stdout = lines.join("\n");
        let messages: Vec<Value> = invoke::cargo_messages(stdout.as_bytes()).collect();
        collect(&messages, pkgs)
    }

    #[test]
    fn test_executables_are_collected_with_their_package_and_kind() {
        let out = [
            artifact("/ws/a/Cargo.toml", "a", &["lib"], true, Some("/t/a-1")),
            artifact("/ws/a/Cargo.toml", "it", &["test"], true, Some("/t/it-2")),
            artifact("/ws/b/Cargo.toml", "b", &["bin"], true, Some("/t/b-3")),
        ];
        let bins = collected(&out, &[pkg("a"), pkg("b")]).tests;
        let labels: Vec<String> = bins.iter().map(TestBin::label).collect();
        assert_eq!(labels, ["a a (lib)", "a it (test)", "b b (bin)"]);
        assert_eq!(bins[1].exe, PathBuf::from("/t/it-2"));
    }

    #[test]
    fn non_test_and_non_executable_artifacts_are_skipped() {
        let out = [
            artifact("/ws/a/Cargo.toml", "a", &["lib"], false, Some("/t/a-lib")),
            artifact("/ws/a/Cargo.toml", "a", &["lib"], true, None),
            artifact(
                "/ws/a/Cargo.toml",
                "build-script",
                &["custom-build"],
                false,
                None,
            ),
        ];
        assert_eq!(collected(&out, &[pkg("a")]), Built::default());
    }

    #[test]
    fn plain_bins_and_examples_are_siblings_not_tests() {
        let out = [
            artifact("/ws/a/Cargo.toml", "a", &["bin"], false, Some("/t/a")),
            artifact(
                "/ws/a/Cargo.toml",
                "demo",
                &["example"],
                false,
                Some("/t/demo"),
            ),
            artifact(
                "/ws/a/Cargo.toml",
                "a",
                &["lib"],
                false,
                Some("/t/liba.rlib"),
            ),
        ];
        let built = collected(&out, &[pkg("a")]);
        assert!(built.tests.is_empty());
        let labels: Vec<String> = built.siblings.iter().map(TestBin::label).collect();
        assert_eq!(labels, ["a a (bin)", "a demo (example)"]);
    }

    #[test]
    fn a_repeated_artifact_is_one_binary() {
        let line = artifact("/ws/a/Cargo.toml", "a", &["lib"], true, Some("/t/a-1"));
        let out = [line.clone(), line, "not json".to_string()];
        assert_eq!(collected(&out, &[pkg("a")]).tests.len(), 1);
    }

    #[test]
    fn a_dependency_outside_the_workspace_is_not_collected() {
        let out = [artifact(
            "/reg/serde/Cargo.toml",
            "serde",
            &["lib"],
            true,
            Some("/t/s"),
        )];
        assert_eq!(collected(&out, &[pkg("a")]), Built::default());
    }

    #[test]
    fn a_dual_crate_type_lib_keeps_its_doc_tests_and_selects_with_lib() {
        let out = [artifact(
            "/ws/a/Cargo.toml",
            "a",
            &["cdylib", "rlib"],
            true,
            Some("/t/a"),
        )];
        let bin = collected(&out, &[pkg("a")]).tests.remove(0);
        assert!(bin.has_doc_tests());
        assert_eq!(bin.target_flags(), ["--lib"]);
    }

    #[test]
    fn named_targets_select_by_kind_and_name() {
        let kinds = |k: &str, name: &str| TestBin {
            package: "p".into(),
            target: name.into(),
            kinds: vec![k.into()],
            doctest: false,
            exe: PathBuf::new(),
        };
        assert_eq!(kinds("test", "golden").target_flags(), ["--test", "golden"]);
        assert_eq!(kinds("bin", "cli").target_flags(), ["--bin", "cli"]);
        assert_eq!(kinds("bench", "b").target_flags(), ["--bench", "b"]);
        assert!(!kinds("test", "golden").has_doc_tests());
    }

    fn args(list: &[&str]) -> Vec<String> {
        list.iter().map(|s| s.to_string()).collect()
    }

    #[test]
    fn target_selecting_flags_are_detected() {
        assert!(narrows_targets(&args(&["--release", "--lib"])));
        assert!(narrows_targets(&args(&["--test=golden"])));
        assert!(narrows_targets(&args(&["--bin=cli"])));
        assert!(!narrows_targets(&args(&["--all-features", "--release"])));
    }
}