mirror of
https://github.com/astral-sh/ruff.git
synced 2026-10-28 10:45:37 +08:00
Limit generated-file check diffs to 100 lines (#28002)
This commit is contained in:
1 parent
6b388d27dc
commit
715287d6f3
10 files changed
+105
-23
No files matched your search
Generated
+1
-1
@@ -3263,6 +3263,7 @@ version = "0.0.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"colored",
|
||||
"ignore",
|
||||
"imara-diff",
|
||||
"indicatif",
|
||||
@@ -3270,7 +3271,6 @@ dependencies = [
|
||||
"itertools 0.15.0",
|
||||
"libcst",
|
||||
"markdown",
|
||||
"pretty_assertions",
|
||||
"rayon",
|
||||
"regex",
|
||||
"ruff",
|
||||
|
||||
@@ -29,13 +29,13 @@ ty_static = { workspace = true }
|
||||
|
||||
anyhow = { workspace = true }
|
||||
clap = { workspace = true, features = ["wrap_help"] }
|
||||
colored = { workspace = true }
|
||||
ignore = { workspace = true }
|
||||
imara-diff = { workspace = true }
|
||||
indicatif = { workspace = true }
|
||||
itertools = { workspace = true }
|
||||
libcst = { workspace = true }
|
||||
markdown = { version = "1.0.0" }
|
||||
pretty_assertions = { workspace = true }
|
||||
rayon = { workspace = true }
|
||||
regex = { workspace = true }
|
||||
schemars = { workspace = true }
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
//! Run all code and documentation generation steps.
|
||||
|
||||
use std::fmt::Write as _;
|
||||
|
||||
use anyhow::Result;
|
||||
use colored::Colorize;
|
||||
use similar::TextDiff;
|
||||
|
||||
use crate::{
|
||||
generate_cli_help, generate_docs, generate_json_schema, generate_ty_cli_reference,
|
||||
@@ -49,3 +53,88 @@ pub(crate) fn main(args: &Args) -> Result<()> {
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Limit generated-file diffs so stale files do not overwhelm CI logs.
|
||||
pub(crate) fn generated_file_diff(current: &str, generated: &str) -> String {
|
||||
const MAX_DIFF_LINES: usize = 100;
|
||||
|
||||
let diff = TextDiff::from_lines(current, generated)
|
||||
.unified_diff()
|
||||
.to_string();
|
||||
let mut output = String::new();
|
||||
for (index, line) in diff.split_terminator('\n').enumerate() {
|
||||
if index == MAX_DIFF_LINES {
|
||||
let _ = writeln!(output, "… diff truncated after {MAX_DIFF_LINES} lines");
|
||||
break;
|
||||
}
|
||||
|
||||
let line = match line.as_bytes().first() {
|
||||
Some(b'-') => line.red(),
|
||||
Some(b'+') => line.green(),
|
||||
_ => line.normal(),
|
||||
};
|
||||
let _ = writeln!(output, "{line}");
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use colored::Colorize;
|
||||
|
||||
use super::generated_file_diff;
|
||||
|
||||
#[test]
|
||||
fn short_diff() {
|
||||
assert_eq!(
|
||||
generated_file_diff("unchanged\nold\n", "unchanged\nnew\n"),
|
||||
format!(
|
||||
"@@ -1,2 +1,2 @@\n unchanged\n{}\n{}\n",
|
||||
"-old".red(),
|
||||
"+new".green()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn diff_within_limit() {
|
||||
for line_count in [98, 99] {
|
||||
let generated = "new\n".repeat(line_count);
|
||||
assert_eq!(
|
||||
generated_file_diff("", &generated),
|
||||
format!(
|
||||
"@@ -0,0 +1,{line_count} @@\n{}",
|
||||
format!("{}\n", "+new".green()).repeat(line_count)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncated_diff() {
|
||||
for line_count in [100, 200] {
|
||||
let generated = "new\n".repeat(line_count);
|
||||
assert_eq!(
|
||||
generated_file_diff("", &generated),
|
||||
format!(
|
||||
"@@ -0,0 +1,{line_count} @@\n{}… diff truncated after 100 lines\n",
|
||||
format!("{}\n", "+new".green()).repeat(99)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn diff_after_unchanged_prefix() {
|
||||
let prefix = "unchanged\n".repeat(150);
|
||||
assert_eq!(
|
||||
generated_file_diff(&format!("{prefix}old\n"), &format!("{prefix}new\n")),
|
||||
format!(
|
||||
"@@ -148,4 +148,4 @@\n unchanged\n unchanged\n unchanged\n{}\n{}\n",
|
||||
"-old".red(),
|
||||
"+new".green()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,11 @@ use std::{fs, str};
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use clap::CommandFactory;
|
||||
use pretty_assertions::StrComparison;
|
||||
|
||||
use ruff::args;
|
||||
|
||||
use crate::ROOT_DIR;
|
||||
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND};
|
||||
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND, generated_file_diff};
|
||||
|
||||
const COMMAND_HELP_BEGIN_PRAGMA: &str = "<!-- Begin auto-generated command help. -->\n";
|
||||
const COMMAND_HELP_END_PRAGMA: &str = "<!-- End auto-generated command help. -->";
|
||||
@@ -99,7 +98,7 @@ pub(super) fn main(args: &Args) -> Result<()> {
|
||||
if existing == new {
|
||||
println!("up-to-date: {filename}");
|
||||
} else {
|
||||
let comparison = StrComparison::new(&existing, &new);
|
||||
let comparison = generated_file_diff(&existing, &new);
|
||||
bail!("{filename} changed, please run `{REGENERATE_ALL_COMMAND}`:\n{comparison}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,10 @@ use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use pretty_assertions::StrComparison;
|
||||
use schemars::generate::SchemaSettings;
|
||||
|
||||
use crate::ROOT_DIR;
|
||||
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND};
|
||||
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND, generated_file_diff};
|
||||
use ruff_workspace::options::Options;
|
||||
|
||||
#[derive(clap::Args)]
|
||||
@@ -33,7 +32,7 @@ pub(crate) fn main(args: &Args) -> Result<()> {
|
||||
if current == schema_string {
|
||||
println!("Up-to-date: {filename}");
|
||||
} else {
|
||||
let comparison = StrComparison::new(¤t, &schema_string);
|
||||
let comparison = generated_file_diff(¤t, &schema_string);
|
||||
bail!("{filename} changed, please run `{REGENERATE_ALL_COMMAND}`:\n{comparison}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,9 @@ use std::path::PathBuf;
|
||||
use anyhow::{Result, bail};
|
||||
use clap::{Command, CommandFactory};
|
||||
use itertools::Itertools;
|
||||
use pretty_assertions::StrComparison;
|
||||
|
||||
use crate::ROOT_DIR;
|
||||
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND};
|
||||
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND, generated_file_diff};
|
||||
|
||||
use ty::Cli;
|
||||
|
||||
@@ -34,7 +33,7 @@ pub(crate) fn main(args: &Args) -> Result<()> {
|
||||
if current == reference_string {
|
||||
println!("Up-to-date: {filename}");
|
||||
} else {
|
||||
let comparison = StrComparison::new(¤t, &reference_string);
|
||||
let comparison = generated_file_diff(¤t, &reference_string);
|
||||
bail!(
|
||||
"{filename} changed, please run `{REGENERATE_ALL_COMMAND}`:\n{comparison}"
|
||||
);
|
||||
|
||||
@@ -5,11 +5,10 @@ use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::bail;
|
||||
use pretty_assertions::StrComparison;
|
||||
|
||||
use ty_static::EnvVars;
|
||||
|
||||
use crate::generate_all::Mode;
|
||||
use crate::generate_all::{Mode, generated_file_diff};
|
||||
|
||||
#[derive(clap::Args)]
|
||||
pub(crate) struct Args {
|
||||
@@ -39,7 +38,7 @@ pub(crate) fn main(args: &Args) -> anyhow::Result<()> {
|
||||
if current == reference_string {
|
||||
println!("Up-to-date: {filename}");
|
||||
} else {
|
||||
let comparison = StrComparison::new(¤t, &reference_string);
|
||||
let comparison = generated_file_diff(¤t, &reference_string);
|
||||
bail!(
|
||||
"{filename} changed, please run `cargo dev generate-ty-env-vars-reference`:\n{comparison}"
|
||||
);
|
||||
|
||||
@@ -5,14 +5,13 @@ use std::{fmt::Write, path::PathBuf};
|
||||
|
||||
use anyhow::bail;
|
||||
use itertools::Itertools;
|
||||
use pretty_assertions::StrComparison;
|
||||
use ruff_options_metadata::{OptionField, OptionSet, OptionsMetadata, Visit};
|
||||
use ruff_python_trivia::textwrap;
|
||||
use ty_project::metadata::Options;
|
||||
|
||||
use crate::{
|
||||
ROOT_DIR,
|
||||
generate_all::{Mode, REGENERATE_ALL_COMMAND},
|
||||
generate_all::{Mode, REGENERATE_ALL_COMMAND, generated_file_diff},
|
||||
};
|
||||
|
||||
#[derive(clap::Args)]
|
||||
@@ -46,7 +45,7 @@ pub(crate) fn main(args: &Args) -> anyhow::Result<()> {
|
||||
if output == current {
|
||||
println!("Up-to-date: {file_name}");
|
||||
} else {
|
||||
let comparison = StrComparison::new(¤t, &output);
|
||||
let comparison = generated_file_diff(¤t, &output);
|
||||
bail!("{file_name} changed, please run `{REGENERATE_ALL_COMMAND}`:\n{comparison}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,10 @@ use std::path::PathBuf;
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use itertools::Itertools as _;
|
||||
use pretty_assertions::StrComparison;
|
||||
use regex::{Captures, Regex};
|
||||
|
||||
use crate::ROOT_DIR;
|
||||
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND};
|
||||
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND, generated_file_diff};
|
||||
|
||||
#[derive(clap::Args)]
|
||||
pub(crate) struct Args {
|
||||
@@ -34,7 +33,7 @@ pub(crate) fn main(args: &Args) -> Result<()> {
|
||||
if current == markdown {
|
||||
println!("Up-to-date: {filename}");
|
||||
} else {
|
||||
let comparison = StrComparison::new(¤t, &markdown);
|
||||
let comparison = generated_file_diff(¤t, &markdown);
|
||||
bail!("{filename} changed, please run `{REGENERATE_ALL_COMMAND}`:\n{comparison}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,10 @@ use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use pretty_assertions::StrComparison;
|
||||
use schemars::generate::SchemaSettings;
|
||||
|
||||
use crate::ROOT_DIR;
|
||||
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND};
|
||||
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND, generated_file_diff};
|
||||
use ty_project::metadata::options::Options;
|
||||
|
||||
#[derive(clap::Args)]
|
||||
@@ -33,7 +32,7 @@ pub(crate) fn main(args: &Args) -> Result<()> {
|
||||
if current == schema_string {
|
||||
println!("Up-to-date: {filename}");
|
||||
} else {
|
||||
let comparison = StrComparison::new(¤t, &schema_string);
|
||||
let comparison = generated_file_diff(¤t, &schema_string);
|
||||
bail!("{filename} changed, please run `{REGENERATE_ALL_COMMAND}`:\n{comparison}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user