Skip to content

Commit

Permalink
Merge pull request #259 from oli-obk/update_status
Browse files Browse the repository at this point in the history
Remove `update_status`
  • Loading branch information
oli-obk authored Aug 15, 2024
2 parents bbe74aa + 2f3a770 commit 2de49ab
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 48 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

* `TestStatus::update_status`, instead use a revision if you want to run subcommands

## [0.25.0] - 2024-07-24

### Added
Expand Down
7 changes: 5 additions & 2 deletions src/build_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::{
sync::{Arc, OnceLock, RwLock},
};

use crate::{status_emitter::StatusEmitter, Config, Errored};
use crate::{
status_emitter::{RevisionStyle, StatusEmitter},
Config, Errored,
};

/// A build shared between all tests of the same `BuildManager`
pub trait Build {
Expand Down Expand Up @@ -83,7 +86,7 @@ impl<'a> BuildManager<'a> {
let build = self
.status_emitter
.register_test(what.description().into())
.for_revision("");
.for_revision("", RevisionStyle::Parent);
let res = what.build(self).map_err(|e| err = Some(e));
build.done(
&res.as_ref()
Expand Down
6 changes: 3 additions & 3 deletions src/custom_flags/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use spanned::Spanned;
use std::process::{Command, Output};

use crate::{
build_manager::BuildManager, display, per_test_config::TestConfig, Error, Errored, TestOk,
TestRun,
build_manager::BuildManager, display, per_test_config::TestConfig,
status_emitter::RevisionStyle, Error, Errored, TestOk, TestRun,
};

use super::Flag;
Expand Down Expand Up @@ -37,7 +37,7 @@ impl Flag for Run {
config: config.config.clone(),
comments: config.comments,
aux_dir: config.aux_dir,
status: config.status.for_revision(&revision),
status: config.status.for_revision(&revision, RevisionStyle::Show),
};
cmd.arg("--print").arg("file-names");
let output = cmd.output().unwrap();
Expand Down
27 changes: 19 additions & 8 deletions src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use crate::{
build_manager::{Build, BuildManager},
custom_flags::Flag,
per_test_config::TestConfig,
status_emitter::RevisionStyle,
test_result::Errored,
CommandBuilder, Config, OutputConflictHandling,
CommandBuilder, Config, OutputConflictHandling, TestOk,
};

#[derive(Default, Debug)]
Expand Down Expand Up @@ -390,13 +391,23 @@ impl Flag for DependencyBuilder {
config: &TestConfig<'_>,
build_manager: &BuildManager<'_>,
) -> Result<(), Errored> {
config
.status
.update_status("waiting for dependencies to finish building".into());
let extra_args = build_manager.build(self.clone())?;
cmd.args(extra_args);
config.status.update_status(String::new());
Ok(())
let status = config.status.for_revision(
"waiting for dependencies to finish building",
RevisionStyle::Quiet,
);
match build_manager.build(self.clone()) {
Ok(extra_args) => {
cmd.args(extra_args);
status.done(&Ok(TestOk::Ok));
Ok(())
}
Err(err) => {
let err = Err(err);
status.done(&err);
#[allow(clippy::unnecessary_literal_unwrap)]
Err(err.unwrap_err())
}
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use core::CrateType;
pub use filter::Match;
use per_test_config::TestConfig;
use spanned::Spanned;
use status_emitter::RevisionStyle;
use status_emitter::{StatusEmitter, TestStatus};
use std::collections::VecDeque;
use std::path::Path;
Expand Down Expand Up @@ -326,7 +327,7 @@ fn parse_and_test_file(
let revisions = comments.revisions.as_deref().unwrap_or(EMPTY);
let mut runs = vec![];
for revision in revisions {
let status = status.for_revision(revision);
let status = status.for_revision(revision, RevisionStyle::Show);
// Ignore file if only/ignore rules do (not) apply
if !config.test_file_conditions(&comments, revision) {
runs.push(TestRun {
Expand Down
73 changes: 39 additions & 34 deletions src/status_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,22 @@ pub trait StatusEmitter: Sync + RefUnwindSafe {
) -> Box<dyn Summary>;
}

/// Some configuration options for revisions
#[derive(Debug, Clone, Copy)]
pub enum RevisionStyle {
/// Things like dependencies or aux files building are noise in non-interactif mode
/// and thus silenced.
Quiet,
/// Always show them, even if rendering to a file
Show,
/// The parent, only show in indicatif mode and on failure
Parent,
}

/// Information about a specific test run.
pub trait TestStatus: Send + Sync + RefUnwindSafe {
/// Create a copy of this test for a new revision.
fn for_revision(&self, revision: &str) -> Box<dyn TestStatus>;
fn for_revision(&self, revision: &str, style: RevisionStyle) -> Box<dyn TestStatus>;

/// Create a copy of this test for a new path.
fn for_path(&self, path: &Path) -> Box<dyn TestStatus>;
Expand All @@ -60,9 +72,6 @@ pub trait TestStatus: Send + Sync + RefUnwindSafe {
stdout: &'a [u8],
) -> Box<dyn Debug + 'a>;

/// Change the status of the test while it is running to supply some kind of progress
fn update_status(&self, msg: String);

/// A test has finished, handle the result immediately.
fn done(&self, _result: &TestResult) {}

Expand Down Expand Up @@ -111,7 +120,7 @@ pub struct SilentStatus {
}

impl TestStatus for SilentStatus {
fn for_revision(&self, revision: &str) -> Box<dyn TestStatus> {
fn for_revision(&self, revision: &str, _style: RevisionStyle) -> Box<dyn TestStatus> {
Box::new(SilentStatus {
revision: revision.into(),
path: self.path.clone(),
Expand All @@ -134,8 +143,6 @@ impl TestStatus for SilentStatus {
Box::new(())
}

fn update_status(&self, _msg: String) {}

fn path(&self) -> &Path {
&self.path
}
Expand Down Expand Up @@ -179,7 +186,6 @@ enum Msg {
Inc,
IncLength,
Finish,
Status(String, String),
}

impl Text {
Expand Down Expand Up @@ -212,9 +218,6 @@ impl Text {
spinner.finish_and_clear();
}
}
Msg::Status(msg, status) => {
threads.get_mut(&msg).unwrap().set_message(status);
}
Msg::Push(msg) => {
let spinner =
bars.add(ProgressBar::new_spinner().with_prefix(msg.clone()));
Expand Down Expand Up @@ -295,6 +298,7 @@ struct TextTest {
path: PathBuf,
revision: String,
first: AtomicBool,
style: RevisionStyle,
}

impl TextTest {
Expand Down Expand Up @@ -322,17 +326,23 @@ impl TestStatus for TextTest {
let old_msg = self.msg();
let msg = format!("... {result}");
if ProgressDrawTarget::stdout().is_hidden() {
println!("{old_msg} {msg}");
std::io::stdout().flush().unwrap();
match self.style {
RevisionStyle::Quiet => {}
RevisionStyle::Show | RevisionStyle::Parent => {
let revision = if self.revision.is_empty() {
String::new()
} else {
format!(" (revision `{}`)", self.revision)
};
println!("{}{revision} {msg}", display(&self.path));
std::io::stdout().flush().unwrap();
}
}
}
self.text.sender.send(Msg::Pop(old_msg, Some(msg))).unwrap();
}
}

fn update_status(&self, msg: String) {
self.text.sender.send(Msg::Status(self.msg(), msg)).unwrap();
}

fn failed_test<'a>(
&self,
cmd: &str,
Expand Down Expand Up @@ -373,7 +383,7 @@ impl TestStatus for TextTest {
&self.path
}

fn for_revision(&self, revision: &str) -> Box<dyn TestStatus> {
fn for_revision(&self, revision: &str, style: RevisionStyle) -> Box<dyn TestStatus> {
if !self.first.swap(false, std::sync::atomic::Ordering::Relaxed)
&& self.text.is_quiet_output()
{
Expand All @@ -385,6 +395,7 @@ impl TestStatus for TextTest {
path: self.path.clone(),
revision: revision.to_owned(),
first: AtomicBool::new(false),
style,
};
self.text.sender.send(Msg::Push(text.msg())).unwrap();
Box::new(text)
Expand All @@ -396,6 +407,7 @@ impl TestStatus for TextTest {
path: path.to_path_buf(),
revision: self.revision.clone(),
first: AtomicBool::new(false),
style: RevisionStyle::Show,
};
self.text.sender.send(Msg::Push(text.msg())).unwrap();
Box::new(text)
Expand All @@ -416,6 +428,7 @@ impl StatusEmitter for Text {
path,
revision: String::new(),
first: AtomicBool::new(true),
style: RevisionStyle::Parent,
})
}

Expand Down Expand Up @@ -917,7 +930,7 @@ impl<const GROUP: bool> TestStatus for PathAndRev<GROUP> {
&self.path
}

fn for_revision(&self, revision: &str) -> Box<dyn TestStatus> {
fn for_revision(&self, revision: &str, _style: RevisionStyle) -> Box<dyn TestStatus> {
Box::new(Self {
path: self.path.clone(),
revision: revision.to_owned(),
Expand Down Expand Up @@ -946,8 +959,6 @@ impl<const GROUP: bool> TestStatus for PathAndRev<GROUP> {
fn revision(&self) -> &str {
&self.revision
}

fn update_status(&self, _msg: String) {}
}

impl<const GROUP: bool> StatusEmitter for Gha<GROUP> {
Expand Down Expand Up @@ -1050,18 +1061,16 @@ impl<T: TestStatus, U: TestStatus> TestStatus for (T, U) {
rev
}

fn for_revision(&self, revision: &str) -> Box<dyn TestStatus> {
Box::new((self.0.for_revision(revision), self.1.for_revision(revision)))
fn for_revision(&self, revision: &str, style: RevisionStyle) -> Box<dyn TestStatus> {
Box::new((
self.0.for_revision(revision, style),
self.1.for_revision(revision, style),
))
}

fn for_path(&self, path: &Path) -> Box<dyn TestStatus> {
Box::new((self.0.for_path(path), self.1.for_path(path)))
}

fn update_status(&self, msg: String) {
self.0.update_status(msg.clone());
self.1.update_status(msg)
}
}

impl<T: StatusEmitter, U: StatusEmitter> StatusEmitter for (T, U) {
Expand Down Expand Up @@ -1099,8 +1108,8 @@ impl<T: TestStatus + ?Sized> TestStatus for Box<T> {
(**self).revision()
}

fn for_revision(&self, revision: &str) -> Box<dyn TestStatus> {
(**self).for_revision(revision)
fn for_revision(&self, revision: &str, style: RevisionStyle) -> Box<dyn TestStatus> {
(**self).for_revision(revision, style)
}

fn for_path(&self, path: &Path) -> Box<dyn TestStatus> {
Expand All @@ -1115,10 +1124,6 @@ impl<T: TestStatus + ?Sized> TestStatus for Box<T> {
) -> Box<dyn Debug + 'a> {
(**self).failed_test(cmd, stderr, stdout)
}

fn update_status(&self, msg: String) {
(**self).update_status(msg)
}
}

impl<T: StatusEmitter + ?Sized> StatusEmitter for Box<T> {
Expand Down
1 change: 1 addition & 0 deletions src/test_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{status_emitter::TestStatus, Error};
use color_eyre::eyre::Result;

/// The possible non-failure results a single test can have.
#[derive(Debug)]
pub enum TestOk {
/// The test passed
Ok,
Expand Down

0 comments on commit 2de49ab

Please sign in to comment.