Skip to content

Commit

Permalink
tests: drop colordiff dependency, perform diff internally in test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
laniakea64 committed Jan 25, 2024
1 parent 57974cd commit 6d95cd7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 30 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
53 changes: 29 additions & 24 deletions tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::{
fs,
io::{self, ErrorKind},
path::{Path, PathBuf},
process::Command,
sync::{
atomic::{AtomicU64, Ordering::Relaxed},
Arc,
Expand Down Expand Up @@ -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));
}
}

Expand Down

0 comments on commit 6d95cd7

Please sign in to comment.