diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index af8134bf..52192e1e 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -1,6 +1,7 @@ mod config; mod render; mod show_all_files; +mod show_all_mutations; mod show_language_files; use std::sync::atomic::{AtomicBool, Ordering}; @@ -13,7 +14,7 @@ use render::{Noop, Render}; use tracing::{Level, debug, error, info, warn}; use crate::render::{ - AllMutations, FileMutations, LangMutations, SingleMutation, + FileMutations, LangMutations, SingleMutation, find_mutation_by_hash, }; @@ -61,15 +62,7 @@ fn main() { lang: None, file: _, } => { - let mut session = Session::new(cli.config.clone()).expect("session creation"); - session.tend_add_missing_states().expect("tend states"); - let base = session.base(); - let mutations: Vec<_> = base - .mutations() - .collect::, _>>() - .expect("mutation scan"); - let states = collect_states(&session, mutations); - Box::new(AllMutations(states)) + show_all_mutations::ShowAllMutations::run(cli.config.clone()) } Show::Mutations { diff --git a/crates/bough-cli/src/render.rs b/crates/bough-cli/src/render.rs index 56750570..3cc73dcf 100644 --- a/crates/bough-cli/src/render.rs +++ b/crates/bough-cli/src/render.rs @@ -75,12 +75,12 @@ impl Render for Noop { -fn mutation_hash(m: &Mutation) -> String { +pub(crate) fn mutation_hash(m: &Mutation) -> String { let hash = m.hash().expect("hashing should not fail"); format!("{hash}") } -fn fmt_status_colored(state: &State) -> String { +pub(crate) fn fmt_status_colored(state: &State) -> String { match state.status() { Some(Status::Caught) | Some(Status::CaughtByTests(_)) => { format!("{CAUGHT}Caught {RESET}") @@ -91,7 +91,7 @@ fn fmt_status_colored(state: &State) -> String { } } -fn fmt_mutation_terse(s: &State) -> String { +pub(crate) fn fmt_mutation_terse(s: &State) -> String { let m = s.mutation(); let mutant = m.mutant(); format!( @@ -106,7 +106,7 @@ fn fmt_mutation_terse(s: &State) -> String { ) } -fn fmt_mutation_markdown_row(m: &Mutation) -> String { +pub(crate) fn fmt_mutation_markdown_row(m: &Mutation) -> String { let mutant = m.mutant(); format!( "| `{}` | {} | {:?} | {:?} | {}:{}-{}:{} | `{}` |", @@ -125,12 +125,12 @@ fn fmt_mutation_markdown_row(m: &Mutation) -> String { const MARKDOWN_TABLE_HEADER: &str = "| Hash | File | Lang | Kind | Span | Subst |\n| --- | --- | --- | --- | --- | --- |"; -fn fmt_mutation_markdown_table(mutations: &[Mutation]) -> String { +pub(crate) fn fmt_mutation_markdown_table(mutations: &[Mutation]) -> String { let rows: Vec<_> = mutations.iter().map(fmt_mutation_markdown_row).collect(); format!("{MARKDOWN_TABLE_HEADER}\n{}", rows.join("\n")) } -fn fmt_mutation_verbose(s: &State) -> String { +pub(crate) fn fmt_mutation_verbose(s: &State) -> String { let m = s.mutation(); let mutant = m.mutant(); format!( @@ -148,7 +148,7 @@ fn fmt_mutation_verbose(s: &State) -> String { ) } -fn fmt_status_from_state(state: &State) -> &'static str { +pub(crate) fn fmt_status_from_state(state: &State) -> &'static str { match state.status() { None => "pending", Some(Status::Caught) | Some(Status::CaughtByTests(_)) => "caught", @@ -157,7 +157,7 @@ fn fmt_status_from_state(state: &State) -> &'static str { } } -fn fmt_outcome_at_from_state(state: &State) -> String { +pub(crate) fn fmt_outcome_at_from_state(state: &State) -> String { match state.outcome_at() { None => "-".to_string(), Some(at) => at.format("%Y-%m-%d %H:%M:%S").to_string(), @@ -361,7 +361,7 @@ pub struct TendState { pub removed: Vec, } -fn hash_list_json(hashes: &[bough_core::MutationHash]) -> String { +pub(crate) fn hash_list_json(hashes: &[bough_core::MutationHash]) -> String { let items: Vec = hashes.iter().map(|h| format!("\"{h}\"")).collect(); format!("[{}]", items.join(",")) } @@ -457,7 +457,7 @@ pub struct InitWorkspace { pub outcome: bough_core::PhaseOutcome, } -fn fmt_phase_outcome_terse(outcome: &bough_core::PhaseOutcome) -> String { +pub(crate) fn fmt_phase_outcome_terse(outcome: &bough_core::PhaseOutcome) -> String { format!( "exit={} duration={:.2}s{}", outcome.exit_code(), @@ -470,7 +470,7 @@ fn fmt_phase_outcome_terse(outcome: &bough_core::PhaseOutcome) -> String { ) } -fn fmt_phase_outcome_verbose(outcome: &bough_core::PhaseOutcome) -> String { +pub(crate) fn fmt_phase_outcome_verbose(outcome: &bough_core::PhaseOutcome) -> String { let stdout = String::from_utf8_lossy(outcome.stdout()); let stderr = String::from_utf8_lossy(outcome.stderr()); let mut out = format!( @@ -488,7 +488,7 @@ fn fmt_phase_outcome_verbose(outcome: &bough_core::PhaseOutcome) -> String { out } -fn fmt_phase_outcome_json(outcome: &bough_core::PhaseOutcome) -> String { +pub(crate) fn fmt_phase_outcome_json(outcome: &bough_core::PhaseOutcome) -> String { let stdout = String::from_utf8_lossy(outcome.stdout()); let stderr = String::from_utf8_lossy(outcome.stderr()); format!( diff --git a/crates/bough-cli/src/show_all_mutations.rs b/crates/bough-cli/src/show_all_mutations.rs new file mode 100644 index 00000000..2b6b0175 --- /dev/null +++ b/crates/bough-cli/src/show_all_mutations.rs @@ -0,0 +1,63 @@ +use bough_core::{Session, State}; +use bough_typed_hash::TypedHashable; +use facet::Facet; + +use crate::config::Config; +use crate::render::{Render, fmt_mutation_markdown_table, fmt_mutation_terse, fmt_mutation_verbose}; + +#[derive(Facet)] +pub struct ShowAllMutations(pub Vec); + +impl ShowAllMutations { + pub fn run(config: Config) -> Box { + let mut session = Session::new(config).expect("session creation"); + session.tend_add_missing_states().expect("tend states"); + let base = session.base(); + let mutations: Vec<_> = base + .mutations() + .collect::, _>>() + .expect("mutation scan"); + let states = mutations + .into_iter() + .map(|m| { + let hash = m.hash().expect("hash"); + session + .get_state() + .get(&hash) + .expect("state not found for mutation") + }) + .collect(); + Box::new(Self(states)) + } +} + +impl Render for ShowAllMutations { + fn markdown(&self) -> String { + let mutations: Vec<_> = self.0.iter().map(|s| s.mutation().clone()).collect(); + format!( + "# All Mutations\n\n{} total\n\n{}", + self.0.len(), + fmt_mutation_markdown_table(&mutations), + ) + } + + fn terse(&self) -> String { + self.0 + .iter() + .map(fmt_mutation_terse) + .collect::>() + .join("\n") + } + + fn verbose(&self) -> String { + self.0 + .iter() + .map(fmt_mutation_verbose) + .collect::>() + .join("\n") + } + + fn json(&self) -> String { + facet_json::to_string(self).unwrap() + } +}