use super::*;
impl<'a> EpochTx<'a> {
pub(super) fn send(&self, resp: TestResponse) {
if let (Some(owners), Some(runner), TestResponse::Case(case)) =
(self.owners, self.tag, &resp)
{
owners.record(case.name.clone(), runner);
}
let _ = self.tx.send((self.epoch, resp));
}
pub(super) fn tagged(&self, runner: Runner) -> EpochTx<'a> {
EpochTx {
tag: Some(runner),
..*self
}
}
pub(super) fn choose(
&self,
runners: &[Runner],
name: &str,
file: Option<&Path>,
) -> Option<Runner> {
let empty = BTreeMap::new();
match self.owners {
Some(owners) => choose_runner(runners, owners.map().by_test(), name, file),
None => choose_runner(runners, &empty, name, file),
}
}
pub(super) fn to_owned(&self) -> (Sender<(u64, TestResponse)>, u64) {
(self.tx.clone(), self.epoch)
}
}
pub(super) fn one(parse: fn(&str) -> Option<TestCase>) -> impl Fn(&str) -> Vec<TestCase> {
move |line| parse(line).into_iter().collect()
}
pub(super) fn run_streaming(
tx: &EpochTx,
cmd: Command,
parse: impl Fn(&str) -> Vec<TestCase>,
) -> Option<bool> {
run_streaming_shown(tx, cmd, |line| Some(line.to_string()), parse)
}
pub(super) fn run_streaming_shown(
tx: &EpochTx,
cmd: Command,
show: impl Fn(&str) -> Option<String>,
parse: impl Fn(&str) -> Vec<TestCase>,
) -> Option<bool> {
run_streaming_exit(tx, cmd, show, parse).map(|code| code == Some(0))
}
pub(super) fn run_streaming_exit(
tx: &EpochTx,
mut cmd: Command,
show: impl Fn(&str) -> Option<String>,
parse: impl Fn(&str) -> Vec<TestCase>,
) -> Option<Option<i32>> {
let mut child = match tx.control.spawn(tx.request, &mut cmd) {
None => return Some(None),
Some(Ok(c)) => c,
Some(Err(e)) => {
output::push(
output::CHANNEL_TESTS,
OutputLevel::Error,
&format!("failed to spawn the test runner: {e}"),
);
return None;
}
};
let stderr_handle = child.stderr.take().map(|err| {
let (tx, epoch) = tx.to_owned();
std::thread::spawn(move || {
for line in BufReader::new(err).lines().map_while(Result::ok) {
output::push(output::CHANNEL_TESTS, OutputLevel::Info, &line);
if let Some(p) = cargo_progress(&line) {
let _ = tx.send((epoch, TestResponse::Progress(p)));
}
}
})
});
if let Some(out) = child.stdout.take() {
for line in BufReader::new(out).lines().map_while(Result::ok) {
if let Some(text) = show(&line) {
output::push(output::CHANNEL_TESTS, OutputLevel::Info, &text);
}
for case in parse(&line) {
tx.send(TestResponse::Case(case));
}
}
}
if let Some(h) = stderr_handle {
let _ = h.join();
}
let code = child.wait().ok().and_then(|s| s.code());
tx.control.finished();
Some(code)
}