From e09dfabc8c10c3f66af9a7b930c8f19f57a8ece6 Mon Sep 17 00:00:00 2001 From: Zanie Date: Tue, 29 Aug 2023 13:35:42 -0500 Subject: [PATCH] Fix test that checks for black and linter compatibility --- .github/workflows/ci.yaml | 7 ++-- .../tests/black_compatibility_test.rs | 39 ++++++++++++++----- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7575fc3875..e2b4aae8f2 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -106,10 +106,9 @@ jobs: shell: bash # We can't reject unreferenced snapshots on windows because flake8_executable can't run on windows run: cargo insta test --all --all-features - - run: cargo test --package ruff_cli --test black_compatibility_test -- --ignored - # TODO: Skipped as it's currently broken. The resource were moved from the - # ruff_cli to ruff crate, but this test was not updated. - if: false + # Check linter fix compatibility with Black + - name: "Check Black compatibility" + run: cargo test --package ruff_cli --test black_compatibility_test -- --ignored # Check for broken links in the documentation. - run: cargo doc --all --no-deps env: diff --git a/crates/ruff_cli/tests/black_compatibility_test.rs b/crates/ruff_cli/tests/black_compatibility_test.rs index a09cb6185a..ac323eaf75 100644 --- a/crates/ruff_cli/tests/black_compatibility_test.rs +++ b/crates/ruff_cli/tests/black_compatibility_test.rs @@ -8,9 +8,10 @@ use std::thread::sleep; use std::time::Duration; use std::{fs, process, str}; -use anyhow::{anyhow, Context, Result}; +use anyhow::{anyhow, bail, Context, Result}; use assert_cmd::Command; use log::info; +use similar::TextDiff; use walkdir::WalkDir; use ruff::logging::{set_up_logging, LogLevel}; @@ -133,12 +134,17 @@ fn run_test(path: &Path, blackd: &Blackd, ruff_args: &[&str]) -> Result<()> { } let step_3_output = step_3.get_output().stdout.clone(); - assert_eq!( - str::from_utf8(&step_2_output), - str::from_utf8(&step_3_output), - "Mismatch found for {}", - path.display() - ); + let step_2_output_str = str::from_utf8(&step_2_output)?; + let step_3_output_str = str::from_utf8(&step_3_output)?; + + if step_2_output_str != step_3_output_str { + let text_diff = TextDiff::from_lines(step_2_output_str, step_3_output_str); + let unified_diff = text_diff.unified_diff(); + println!("Mismatch found for {}", path.display()); + println!("---------- FIRST ----------\n{}\n", step_2_output_str); + println!("---------- SECOND ----------\n{}\n", step_3_output_str); + println!("---------- DIFF ----------\n{}\n", unified_diff); + }; Ok(()) } @@ -151,6 +157,10 @@ fn test_ruff_black_compatibility() -> Result<()> { let blackd = Blackd::new()?; let fixtures_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + // Get the workspace directory https://github.com/rust-lang/cargo/issues/3946 + .parent() + .expect("parent should be available for crate") + .join("ruff") .join("resources") .join("test") .join("fixtures"); @@ -177,16 +187,25 @@ fn test_ruff_black_compatibility() -> Result<()> { "--fix", "--line-length", "88", - "--select ALL", + "--select", + "ALL", // Exclude ruff codes, specifically RUF100, because it causes differences that are not a // problem. Ruff would add a `# noqa: W292` after the first run, black introduces a // newline, and ruff removes the `# noqa: W292` again. - "--ignore RUF", + // "--ignore", + // "RUF", ]; + let mut results = vec![]; for entry in paths { let path = entry.path(); - run_test(path, &blackd, &ruff_args).context(format!("Testing {}", path.display()))?; + results.push( + run_test(path, &blackd, &ruff_args).context(format!("Testing {}", path.display())), + ); + } + let errors = results.iter().filter(|result| result.is_err()).count(); + if errors > 0 { + bail!("{} mismatches found", errors); } Ok(())