mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:45:24 +00:00
[red-knot] Add GitHub PR annotations when mdtests fail in CI (#17150)
## Summary This PR adds a CI job that causes GitHub to add annotations to a PR diff when mdtest assertions fail. For example: <details> <summary>Screenshot</summary>  </details> ## Motivation Debugging mdtest failures locally is currently a really nice experience: - Errors are displayed with pretty colours, which makes them much more readable - If you run the test from inside an IDE, you can CTRL-click on a path and jump directly to the line that had the failing assertion - If you use [`mdtest.py`](https://github.com/astral-sh/ruff/blob/main/crates/red_knot_python_semantic/mdtest.py), you don't even need to recompile anything after changing an assertion in an mdtest, amd the test results instantly live-update with each change to the MarkDown file Debugging mdtest failures in CI is much more unpleasant, however. Sometimes an error message is just > [static-assert-error] Argument evaluates to `False` ...which doesn't tell you very much unless you navigate to the line in question that has the failing mdtest assertion. The line in question might not even be touched by the PR, and even if it is, it can be hard to find the line if the PR touches many files. Unlike locally, you can't click on the error and jump straight to the line that contains the failing assertion. You also don't get colourised output in CI (https://github.com/astral-sh/ruff/issues/13939). GitHub PR annotations should make it really easy to debug why mdtests are failing on PRs, making PR review much easier. ## Test Plan I opened a PR to my fork [here](https://github.com/AlexWaygood/ruff/pull/11/files) with some bogus changes to an mdtest to show what it looks like when there are failures in CI and this job has been added. Scroll down to `crates/red_knot_python_semantic/resources/mdtest/type_properties/is_equivalent_to.md` on the "files changed" tab for that PR to see the annotations.
This commit is contained in:
parent
c2bb5d5250
commit
195bb433db
3 changed files with 82 additions and 10 deletions
33
.github/workflows/ci.yaml
vendored
33
.github/workflows/ci.yaml
vendored
|
@ -36,6 +36,8 @@ jobs:
|
|||
code: ${{ steps.check_code.outputs.changed }}
|
||||
# Flag that is raised when any code that affects the fuzzer is changed
|
||||
fuzz: ${{ steps.check_fuzzer.outputs.changed }}
|
||||
# Flag that is set to "true" when code related to red-knot changes.
|
||||
red_knot: ${{ steps.check_red_knot.outputs.changed }}
|
||||
|
||||
# Flag that is set to "true" when code related to the playground changes.
|
||||
playground: ${{ steps.check_playground.outputs.changed }}
|
||||
|
@ -166,6 +168,29 @@ jobs:
|
|||
echo "changed=true" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
|
||||
- name: Check if the red-knot code changed
|
||||
id: check_red_knot
|
||||
env:
|
||||
MERGE_BASE: ${{ steps.merge_base.outputs.sha }}
|
||||
run: |
|
||||
if git diff --quiet "${MERGE_BASE}...HEAD" -- \
|
||||
':Cargo.toml' \
|
||||
':Cargo.lock' \
|
||||
':crates/red_knot*/**' \
|
||||
':crates/ruff_db/**' \
|
||||
':crates/ruff_annotate_snippets/**' \
|
||||
':crates/ruff_python_ast/**' \
|
||||
':crates/ruff_python_parser/**' \
|
||||
':crates/ruff_python_trivia/**' \
|
||||
':crates/ruff_source_file/**' \
|
||||
':crates/ruff_text_size/**' \
|
||||
':.github/workflows/ci.yaml' \
|
||||
; then
|
||||
echo "changed=false" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
echo "changed=true" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
|
||||
cargo-fmt:
|
||||
name: "cargo fmt"
|
||||
runs-on: ubuntu-latest
|
||||
|
@ -221,6 +246,14 @@ jobs:
|
|||
uses: taiki-e/install-action@6aca1cfa12ef3a6b98ee8c70e0171bfa067604f5 # v2
|
||||
with:
|
||||
tool: cargo-insta
|
||||
- name: Red-knot mdtests (GitHub annotations)
|
||||
if: ${{ needs.determine_changes.outputs.red_knot == 'true' }}
|
||||
env:
|
||||
NO_COLOR: 1
|
||||
MDTEST_GITHUB_ANNOTATIONS_FORMAT: 1
|
||||
# Ignore errors if this step fails; we want to continue to later steps in the workflow anyway.
|
||||
# This step is just to get nice GitHub annotations on the PR diff in the files-changed tab.
|
||||
run: cargo test -p red_knot_python_semantic --test mdtest || true
|
||||
- name: "Run tests"
|
||||
shell: bash
|
||||
env:
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use camino::Utf8Path;
|
||||
use dir_test::{dir_test, Fixture};
|
||||
use red_knot_test::OutputFormat;
|
||||
|
||||
/// See `crates/red_knot_test/README.md` for documentation on these tests.
|
||||
#[dir_test(
|
||||
|
@ -18,12 +19,19 @@ fn mdtest(fixture: Fixture<&str>) {
|
|||
|
||||
let test_name = test_name("mdtest", absolute_fixture_path);
|
||||
|
||||
let output_format = if std::env::var("MDTEST_GITHUB_ANNOTATIONS_FORMAT").is_ok() {
|
||||
OutputFormat::GitHub
|
||||
} else {
|
||||
OutputFormat::Cli
|
||||
};
|
||||
|
||||
red_knot_test::run(
|
||||
absolute_fixture_path,
|
||||
relative_fixture_path,
|
||||
&snapshot_path,
|
||||
short_title,
|
||||
&test_name,
|
||||
output_format,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ pub fn run(
|
|||
snapshot_path: &Utf8Path,
|
||||
short_title: &str,
|
||||
test_name: &str,
|
||||
output_format: OutputFormat,
|
||||
) {
|
||||
let source = std::fs::read_to_string(absolute_fixture_path).unwrap();
|
||||
let suite = match test_parser::parse(short_title, &source) {
|
||||
|
@ -59,7 +60,10 @@ pub fn run(
|
|||
|
||||
if let Err(failures) = run_test(&mut db, relative_fixture_path, snapshot_path, &test) {
|
||||
any_failures = true;
|
||||
|
||||
if output_format.is_cli() {
|
||||
println!("\n{}\n", test.name().bold().underline());
|
||||
}
|
||||
|
||||
let md_index = LineIndex::from_source_text(&source);
|
||||
|
||||
|
@ -72,15 +76,24 @@ pub fn run(
|
|||
source_map.to_absolute_line_number(relative_line_number);
|
||||
|
||||
for failure in failures {
|
||||
match output_format {
|
||||
OutputFormat::Cli => {
|
||||
let line_info =
|
||||
format!("{relative_fixture_path}:{absolute_line_number}").cyan();
|
||||
format!("{relative_fixture_path}:{absolute_line_number}")
|
||||
.cyan();
|
||||
println!(" {line_info} {failure}");
|
||||
}
|
||||
OutputFormat::GitHub => println!(
|
||||
"::error file={absolute_fixture_path},line={absolute_line_number}::{failure}"
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let escaped_test_name = test.name().replace('\'', "\\'");
|
||||
|
||||
if output_format.is_cli() {
|
||||
println!(
|
||||
"\nTo rerun this specific test, set the environment variable: {MDTEST_TEST_FILTER}='{escaped_test_name}'",
|
||||
);
|
||||
|
@ -89,12 +102,30 @@ pub fn run(
|
|||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("\n{}\n", "-".repeat(50));
|
||||
|
||||
assert!(!any_failures, "Some tests failed.");
|
||||
}
|
||||
|
||||
/// Defines the format in which mdtest should print an error to the terminal
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum OutputFormat {
|
||||
/// The format `cargo test` should use by default.
|
||||
Cli,
|
||||
/// A format that will provide annotations from GitHub Actions
|
||||
/// if mdtest fails on a PR.
|
||||
/// See <https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-an-error-message>
|
||||
GitHub,
|
||||
}
|
||||
|
||||
impl OutputFormat {
|
||||
const fn is_cli(self) -> bool {
|
||||
matches!(self, OutputFormat::Cli)
|
||||
}
|
||||
}
|
||||
|
||||
fn run_test(
|
||||
db: &mut db::Db,
|
||||
relative_fixture_path: &Utf8Path,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue