From 6d95cd79c40d82d26f2ca887494d01e8a7e90d6e Mon Sep 17 00:00:00 2001 From: laniakea64 Date: Wed, 24 Jan 2024 23:33:33 -0500 Subject: [PATCH] tests: drop colordiff dependency, perform diff internally in test runner --- .github/workflows/build.yaml | 5 ---- README.md | 1 - tests/Cargo.lock | 7 +++++ tests/Cargo.toml | 1 + tests/src/main.rs | 53 ++++++++++++++++++++---------------- 5 files changed, 37 insertions(+), 30 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 53b64f1..bf0b524 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -37,11 +37,6 @@ jobs: run: | rustup component add clippy rustfmt - - name: Install dependencies - run: | - sudo apt-get update - sudo apt-get install colordiff - - name: Info run: | rustup --version diff --git a/README.md b/README.md index 8ba5b85..e600892 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,6 @@ surprised if I close your PR while pushing your authored commit directly to main ### Prerequisites * just (of course, lol) -* [colordiff](https://www.colordiff.org/) * Rust ([simple/recommended installation instructions](https://www.rust-lang.org/tools/install); for detailed and alternative installation instructions see [here](https://forge.rust-lang.org/infra/other-installation-methods.html)) Run `just deps` to install the cargo dev dependencies, which right now is only diff --git a/tests/Cargo.lock b/tests/Cargo.lock index e134fe6..04dad0b 100644 --- a/tests/Cargo.lock +++ b/tests/Cargo.lock @@ -737,6 +737,12 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "similar" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32fea41aca09ee824cc9724996433064c89f7777e60762749a4170a14abbfa21" + [[package]] name = "siphasher" version = "0.3.11" @@ -845,6 +851,7 @@ dependencies = [ "scraper", "serde", "serde_json", + "similar", "tempfile", "wait-timeout", ] diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 716756f..ba8912c 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -18,5 +18,6 @@ rayon = "1.8.1" scraper = ">=0.18.1" serde = { version = "1.0.195", features = ["derive"] } serde_json = "1.0.111" +similar = "2.4.0" tempfile = "3.9.0" wait-timeout = "0.2.0" diff --git a/tests/src/main.rs b/tests/src/main.rs index fb9ed41..5883357 100644 --- a/tests/src/main.rs +++ b/tests/src/main.rs @@ -10,7 +10,6 @@ use std::{ fs, io::{self, ErrorKind}, path::{Path, PathBuf}, - process::Command, sync::{ atomic::{AtomicU64, Ordering::Relaxed}, Arc, @@ -119,37 +118,43 @@ fn _main() -> io::Result<()> { continue; } - let diff_output_result = Command::new("colordiff") - .arg("--unified") - .args(["--label", "output"]) - .arg(output) - .args(["--label", "expected"]) - .arg(expected) - .output(); - - let diff_output = match diff_output_result { - Ok(o) => o, - Err(e) => { - eprintln!("Could not run colordiff: attempt failed with \"{e}\"\nIs colordiff installed and available executable on $PATH ?"); - return Err(e); - } - }; - if interrupted.load(Relaxed) { return Err(io::Error::new(ErrorKind::Interrupted, "interrupted!")); } - if diff_output.status.success() { + let output = fs::read_to_string(output)?; + let expected = fs::read_to_string(expected)?; + + let diff = format!( + "{}", + similar::TextDiff::from_lines(&output, &expected) + .unified_diff() + .header("output", "expected") + ); + if diff.is_empty() { eprintln!("ok"); passed += 1; } else { - if diff_output.status.code() == Some(2) { - eprintln!("syntax highlighting mismatch:"); - } else { - eprintln!("diff failed:"); + eprintln!("syntax highlighting mismatch:"); + for line in diff.lines() { + if line.starts_with(' ') { + eprintln!("{}", line); + continue; + } + let color = if line.starts_with('+') { + "0;32" + } else if line.starts_with('-') { + "0;31" + } else if line.starts_with('@') { + "0;36" + } else { + return Err(io::Error::new( + ErrorKind::Other, + format!("no defined color for line: {:?}", line), + )); + }; + eprintln!("\x1B[{}m{}\x1B[0m", color, line); } - eprint!("{}", String::from_utf8_lossy(&diff_output.stdout)); - eprint!("{}", String::from_utf8_lossy(&diff_output.stderr)); } }