Add a silent mode for running the diff.

This commit is contained in:
Tom Alexander 2023-10-14 14:47:57 -04:00
parent dde4bc7920
commit f43920fc7c
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 64 additions and 26 deletions

View File

@ -34,10 +34,17 @@ fn main_body() -> Result<(), Box<dyn std::error::Error>> {
let args = std::env::args().skip(1); let args = std::env::args().skip(1);
if args.is_empty() { if args.is_empty() {
let org_contents = read_stdin_to_string()?; let org_contents = read_stdin_to_string()?;
run_anonymous_compare(org_contents) if let Ok(true) = run_anonymous_compare(org_contents) {
} else {
Err("Diff results do not match.")?;
}
Ok(())
} else { } else {
for arg in args { for arg in args {
run_compare_on_file(arg)? if run_compare_on_file(arg)? {
} else {
Err("Diff results do not match.")?;
}
} }
Ok(()) Ok(())
} }

View File

@ -7,6 +7,7 @@ use std::path::PathBuf;
use futures::future::BoxFuture; use futures::future::BoxFuture;
use futures::future::FutureExt; use futures::future::FutureExt;
use organic::compare::run_compare_on_file; use organic::compare::run_compare_on_file;
use organic::compare::silent_compare_on_file;
use tokio::sync::Semaphore; use tokio::sync::Semaphore;
use tokio::task::JoinError; use tokio::task::JoinError;
use walkdir::WalkDir; use walkdir::WalkDir;
@ -156,11 +157,11 @@ impl TestConfig {
impl SingleFile { impl SingleFile {
async fn run_test(self) -> Result<SingleFileResult, JoinError> { async fn run_test(self) -> Result<SingleFileResult, JoinError> {
let _permit = TEST_PERMITS.acquire().await.unwrap(); let _permit = TEST_PERMITS.acquire().await.unwrap();
let result = run_compare_on_file(&self.file_path); let result = silent_compare_on_file(&self.file_path);
Ok(SingleFileResult { Ok(SingleFileResult {
name: self.name, name: self.name,
file_path: self.file_path, file_path: self.file_path,
status: if result.is_ok() { status: if let Ok(true) = result {
TestStatus::Pass TestStatus::Pass
} else { } else {
TestStatus::Fail TestStatus::Fail

View File

@ -14,37 +14,58 @@ use crate::parser::parse_with_settings;
pub fn run_anonymous_compare<P: AsRef<str>>( pub fn run_anonymous_compare<P: AsRef<str>>(
org_contents: P, org_contents: P,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<bool, Box<dyn std::error::Error>> {
run_anonymous_compare_with_settings(org_contents, &GlobalSettings::default()) run_anonymous_compare_with_settings(org_contents, &GlobalSettings::default(), false)
} }
pub fn run_compare_on_file<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::error::Error>> { pub fn run_compare_on_file<P: AsRef<Path>>(
run_compare_on_file_with_settings(org_path, &GlobalSettings::default()) org_path: P,
) -> Result<bool, Box<dyn std::error::Error>> {
run_compare_on_file_with_settings(org_path, &GlobalSettings::default(), false)
}
pub fn silent_anonymous_compare<P: AsRef<str>>(
org_contents: P,
) -> Result<bool, Box<dyn std::error::Error>> {
run_anonymous_compare_with_settings(org_contents, &GlobalSettings::default(), true)
}
pub fn silent_compare_on_file<P: AsRef<Path>>(
org_path: P,
) -> Result<bool, Box<dyn std::error::Error>> {
run_compare_on_file_with_settings(org_path, &GlobalSettings::default(), true)
} }
pub fn run_anonymous_compare_with_settings<P: AsRef<str>>( pub fn run_anonymous_compare_with_settings<P: AsRef<str>>(
org_contents: P, org_contents: P,
global_settings: &GlobalSettings, global_settings: &GlobalSettings,
) -> Result<(), Box<dyn std::error::Error>> { silent: bool,
) -> Result<bool, Box<dyn std::error::Error>> {
// TODO: This is a work-around to pretend that dos line endings do not exist. It would be better to handle the difference in line endings. // TODO: This is a work-around to pretend that dos line endings do not exist. It would be better to handle the difference in line endings.
let org_contents = org_contents.as_ref().replace("\r\n", "\n"); let org_contents = org_contents.as_ref().replace("\r\n", "\n");
let org_contents = org_contents.as_str(); let org_contents = org_contents.as_str();
print_versions()?; if !silent {
print_versions()?;
}
let rust_parsed = parse_with_settings(org_contents, global_settings)?; let rust_parsed = parse_with_settings(org_contents, global_settings)?;
let org_sexp = emacs_parse_anonymous_org_document(org_contents, global_settings)?; let org_sexp = emacs_parse_anonymous_org_document(org_contents, global_settings)?;
let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).map_err(|e| e.to_string())?; let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).map_err(|e| e.to_string())?;
println!("{}\n\n\n", org_contents); if !silent {
println!("{}", org_sexp); println!("{}\n\n\n", org_contents);
println!("{:#?}", rust_parsed); println!("{}", org_sexp);
println!("{:#?}", rust_parsed);
}
// We do the diffing after printing out both parsed forms in case the diffing panics // We do the diffing after printing out both parsed forms in case the diffing panics
let diff_result = compare_document(&parsed_sexp, &rust_parsed)?; let diff_result = compare_document(&parsed_sexp, &rust_parsed)?;
diff_result.print(org_contents)?; if !silent {
diff_result.print(org_contents)?;
}
if diff_result.is_bad() { if diff_result.is_bad() {
Err("Diff results do not match.")?; return Ok(false);
} else { } else if !silent {
println!( println!(
"{color}Entire document passes.{reset}", "{color}Entire document passes.{reset}",
color = DiffResult::foreground_color(0, 255, 0), color = DiffResult::foreground_color(0, 255, 0),
@ -52,15 +73,18 @@ pub fn run_anonymous_compare_with_settings<P: AsRef<str>>(
); );
} }
Ok(()) Ok(true)
} }
pub fn run_compare_on_file_with_settings<P: AsRef<Path>>( pub fn run_compare_on_file_with_settings<P: AsRef<Path>>(
org_path: P, org_path: P,
global_settings: &GlobalSettings, global_settings: &GlobalSettings,
) -> Result<(), Box<dyn std::error::Error>> { silent: bool,
) -> Result<bool, Box<dyn std::error::Error>> {
let org_path = org_path.as_ref(); let org_path = org_path.as_ref();
print_versions()?; if !silent {
print_versions()?;
}
let parent_directory = org_path let parent_directory = org_path
.parent() .parent()
.ok_or("Should be contained inside a directory.")?; .ok_or("Should be contained inside a directory.")?;
@ -80,17 +104,21 @@ pub fn run_compare_on_file_with_settings<P: AsRef<Path>>(
let org_sexp = emacs_parse_file_org_document(org_path, &global_settings)?; let org_sexp = emacs_parse_file_org_document(org_path, &global_settings)?;
let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).map_err(|e| e.to_string())?; let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).map_err(|e| e.to_string())?;
println!("{}\n\n\n", org_contents); if !silent {
println!("{}", org_sexp); println!("{}\n\n\n", org_contents);
println!("{:#?}", rust_parsed); println!("{}", org_sexp);
println!("{:#?}", rust_parsed);
}
// We do the diffing after printing out both parsed forms in case the diffing panics // We do the diffing after printing out both parsed forms in case the diffing panics
let diff_result = compare_document(&parsed_sexp, &rust_parsed)?; let diff_result = compare_document(&parsed_sexp, &rust_parsed)?;
diff_result.print(org_contents)?; if !silent {
diff_result.print(org_contents)?;
}
if diff_result.is_bad() { if diff_result.is_bad() {
Err("Diff results do not match.")?; return Ok(false);
} else { } else if !silent {
println!( println!(
"{color}Entire document passes.{reset}", "{color}Entire document passes.{reset}",
color = DiffResult::foreground_color(0, 255, 0), color = DiffResult::foreground_color(0, 255, 0),
@ -98,7 +126,7 @@ pub fn run_compare_on_file_with_settings<P: AsRef<Path>>(
); );
} }
Ok(()) Ok(true)
} }
fn print_versions() -> Result<(), Box<dyn std::error::Error>> { fn print_versions() -> Result<(), Box<dyn std::error::Error>> {

View File

@ -10,3 +10,5 @@ pub use compare::run_anonymous_compare;
pub use compare::run_anonymous_compare_with_settings; pub use compare::run_anonymous_compare_with_settings;
pub use compare::run_compare_on_file; pub use compare::run_compare_on_file;
pub use compare::run_compare_on_file_with_settings; pub use compare::run_compare_on_file_with_settings;
pub use compare::silent_anonymous_compare;
pub use compare::silent_compare_on_file;