2024-03-01 04:10:29 -06:00
|
|
|
//! This module provides the functionality needed to run `cargo test` in a background
|
|
|
|
//! thread and report the result of each test in a channel.
|
|
|
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
use crossbeam_channel::Receiver;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use toolchain::Tool;
|
|
|
|
|
|
|
|
use crate::command::{CommandHandle, ParseFromLine};
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(tag = "event", rename_all = "camelCase")]
|
|
|
|
pub enum TestState {
|
|
|
|
Started,
|
|
|
|
Ok,
|
2024-03-08 15:50:43 -06:00
|
|
|
Ignored,
|
2024-03-01 04:10:29 -06:00
|
|
|
Failed { stdout: String },
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(tag = "type", rename_all = "camelCase")]
|
|
|
|
pub enum CargoTestMessage {
|
|
|
|
Test {
|
|
|
|
name: String,
|
|
|
|
#[serde(flatten)]
|
|
|
|
state: TestState,
|
|
|
|
},
|
|
|
|
Suite,
|
|
|
|
Finished,
|
2024-03-15 07:58:59 -05:00
|
|
|
Custom {
|
|
|
|
text: String,
|
|
|
|
},
|
2024-03-01 04:10:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ParseFromLine for CargoTestMessage {
|
2024-03-15 07:58:59 -05:00
|
|
|
fn from_line(line: &str, _: &mut String) -> Option<Self> {
|
2024-03-01 04:10:29 -06:00
|
|
|
let mut deserializer = serde_json::Deserializer::from_str(line);
|
|
|
|
deserializer.disable_recursion_limit();
|
|
|
|
if let Ok(message) = CargoTestMessage::deserialize(&mut deserializer) {
|
|
|
|
return Some(message);
|
|
|
|
}
|
|
|
|
|
2024-03-15 07:58:59 -05:00
|
|
|
Some(CargoTestMessage::Custom { text: line.to_owned() })
|
2024-03-01 04:10:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_eof() -> Option<Self> {
|
|
|
|
Some(CargoTestMessage::Finished)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CargoTestHandle {
|
|
|
|
handle: CommandHandle<CargoTestMessage>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Example of a cargo test command:
|
2024-03-18 17:16:41 -05:00
|
|
|
// cargo test --workspace --no-fail-fast -- module::func -Z unstable-options --format=json
|
2024-03-01 04:10:29 -06:00
|
|
|
|
|
|
|
impl CargoTestHandle {
|
|
|
|
pub fn new(path: Option<&str>) -> std::io::Result<Self> {
|
|
|
|
let mut cmd = Command::new(Tool::Cargo.path());
|
|
|
|
cmd.env("RUSTC_BOOTSTRAP", "1");
|
|
|
|
cmd.arg("test");
|
2024-03-18 17:16:41 -05:00
|
|
|
cmd.arg("--workspace");
|
|
|
|
// --no-fail-fast is needed to ensure that all requested tests will run
|
|
|
|
cmd.arg("--no-fail-fast");
|
2024-03-01 04:10:29 -06:00
|
|
|
cmd.arg("--");
|
|
|
|
if let Some(path) = path {
|
|
|
|
cmd.arg(path);
|
|
|
|
}
|
|
|
|
cmd.args(["-Z", "unstable-options"]);
|
|
|
|
cmd.arg("--format=json");
|
|
|
|
Ok(Self { handle: CommandHandle::spawn(cmd)? })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn receiver(&self) -> &Receiver<CargoTestMessage> {
|
|
|
|
&self.handle.receiver
|
|
|
|
}
|
|
|
|
}
|