refactor: move prints to cli via EventListener trait
This commit is contained in:
@@ -15,6 +15,7 @@ hex = "0.4.3"
|
||||
ignore = "0.4.18"
|
||||
indoc = "1.0"
|
||||
miette = { version = "5.3.0", features = ["fancy"] }
|
||||
owo-colors = "3.5.0"
|
||||
pallas-addresses = "0.14.0"
|
||||
pallas-codec = "0.14.0"
|
||||
pallas-crypto = "0.14.0"
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
pub mod cmd;
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
use aiken_project::{config::Config, Project};
|
||||
use aiken_project::{
|
||||
config::Config,
|
||||
telemetry::{self, TestInfo},
|
||||
Project,
|
||||
};
|
||||
use miette::IntoDiagnostic;
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use owo_colors::OwoColorize;
|
||||
use uplc::machine::cost_model::ExBudget;
|
||||
|
||||
pub mod cmd;
|
||||
|
||||
pub fn with_project<A>(directory: Option<PathBuf>, mut action: A) -> miette::Result<()>
|
||||
where
|
||||
A: FnMut(&mut Project) -> Result<(), aiken_project::error::Error>,
|
||||
A: FnMut(&mut Project<Terminal>) -> Result<(), aiken_project::error::Error>,
|
||||
{
|
||||
let project_path = if let Some(d) = directory {
|
||||
d
|
||||
@@ -17,7 +23,7 @@ where
|
||||
|
||||
let config = Config::load(project_path.clone()).into_diagnostic()?;
|
||||
|
||||
let mut project = Project::new(config, project_path);
|
||||
let mut project = Project::new(config, project_path, Terminal::default());
|
||||
|
||||
let build_result = action(&mut project);
|
||||
|
||||
@@ -37,3 +43,97 @@ where
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub struct Terminal;
|
||||
|
||||
impl telemetry::EventListener for Terminal {
|
||||
fn handle_event(&self, event: telemetry::Event) {
|
||||
match event {
|
||||
telemetry::Event::CompilingPackage { .. } => todo!(),
|
||||
telemetry::Event::RunningTests => {
|
||||
println!("\n{}\n", "Running tests...".bold().underline().purple());
|
||||
}
|
||||
telemetry::Event::FinishedTests { tests } => {
|
||||
let (max_mem, max_cpu) = tests.iter().fold(
|
||||
(0, 0),
|
||||
|(max_mem, max_cpu), TestInfo { spent_budget, .. }| {
|
||||
if spent_budget.mem >= max_mem && spent_budget.cpu >= max_cpu {
|
||||
(spent_budget.mem, spent_budget.cpu)
|
||||
} else if spent_budget.mem > max_mem {
|
||||
(spent_budget.mem, max_cpu)
|
||||
} else if spent_budget.cpu > max_cpu {
|
||||
(max_mem, spent_budget.cpu)
|
||||
} else {
|
||||
(max_mem, max_cpu)
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
let max_mem = max_mem.to_string().len() as i32;
|
||||
let max_cpu = max_cpu.to_string().len() as i32;
|
||||
|
||||
for test_info in &tests {
|
||||
println!("{}", fmt_test(test_info, max_mem, max_cpu))
|
||||
}
|
||||
|
||||
let (n_passed, n_failed) =
|
||||
tests
|
||||
.iter()
|
||||
.fold((0, 0), |(n_passed, n_failed), test_info| {
|
||||
if test_info.is_passing {
|
||||
(n_passed + 1, n_failed)
|
||||
} else {
|
||||
(n_passed, n_failed + 1)
|
||||
}
|
||||
});
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
format!(
|
||||
"\n Summary: {} test(s), {}; {}.",
|
||||
tests.len(),
|
||||
format!("{} passed", n_passed).bright_green(),
|
||||
format!("{} failed", n_failed).bright_red()
|
||||
)
|
||||
.bold()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_test(test_info: &TestInfo, max_mem: i32, max_cpu: i32) -> String {
|
||||
let TestInfo {
|
||||
is_passing,
|
||||
test,
|
||||
spent_budget,
|
||||
} = test_info;
|
||||
|
||||
let ExBudget { mem, cpu } = spent_budget;
|
||||
|
||||
format!(
|
||||
" [{}] [mem: {}, cpu: {}] {}::{}",
|
||||
if *is_passing {
|
||||
"PASS".bold().green().to_string()
|
||||
} else {
|
||||
"FAIL".bold().red().to_string()
|
||||
},
|
||||
pad_left(mem.to_string(), max_mem, " "),
|
||||
pad_left(cpu.to_string(), max_cpu, " "),
|
||||
test.module.blue(),
|
||||
test.name.bright_blue()
|
||||
)
|
||||
}
|
||||
|
||||
fn pad_left(mut text: String, n: i32, delimiter: &str) -> String {
|
||||
let diff = n - text.len() as i32;
|
||||
|
||||
if diff.is_positive() {
|
||||
for _ in 0..diff {
|
||||
text.insert_str(0, delimiter);
|
||||
}
|
||||
}
|
||||
|
||||
text
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user