Show a pretty markdown table in formatter ecosystem checks (#6496)

**Summary** The formatter ecosystem checks will now print a markdown table you can copy&paste into your PR description. 

![image](https://github.com/astral-sh/ruff/assets/6826232/80289ed9-9d2b-400e-a994-de63dca0b065)

copied markdown:

| project      | similarity index |
|--------------|------------------|
| build        | 0.75623          |
| cpython      | 0.75989          |
| django       | 0.99784          |
| transformers | 0.99470          |
| typeshed     | 0.74853          |
| warehouse    | 0.99585          |
| zulip        | 0.99702          |

raw markdown:
```markdown
| project      | similarity index |
|--------------|------------------|
| build        | 0.75623          |
| cpython      | 0.75989          |
| django       | 0.99784          |
| transformers | 0.99470          |
| typeshed     | 0.74853          |
| warehouse    | 0.99585          |
| zulip        | 0.99702          |
```
This commit is contained in:
konsti 2023-08-11 15:37:21 +02:00 committed by GitHub
parent f2939c678b
commit 8b24238d19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 5 deletions

View File

@ -338,6 +338,6 @@ jobs:
- name: "Formatter progress"
run: scripts/formatter_ecosystem_checks.sh
- name: "Github step summary"
run: grep "similarity index" target/progress_projects_log.txt | sort > $GITHUB_STEP_SUMMARY
run: cat target/progress_projects_stats.txt > $GITHUB_STEP_SUMMARY
- name: "Remove checkouts from cache"
run: rm -r target/progress_projects

View File

@ -22,7 +22,7 @@ use std::panic::catch_unwind;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::time::{Duration, Instant};
use std::{fmt, fs, io};
use std::{fmt, fs, io, iter};
use tempfile::NamedTempFile;
use tracing::{debug, error, info, info_span};
use tracing_indicatif::span_ext::IndicatifSpanExt;
@ -187,6 +187,9 @@ pub(crate) struct Args {
/// Write all log messages (same as cli) to this file
#[arg(long)]
pub(crate) log_file: Option<PathBuf>,
/// Write a markdown table with the similarity indices to this file
#[arg(long)]
pub(crate) stats_file: Option<PathBuf>,
/// Assert that there are exactly this many input files with errors. This catches regressions
/// (or improvements) in the parser.
#[arg(long)]
@ -302,6 +305,8 @@ fn format_dev_multi_project(args: &Args) -> anyhow::Result<bool> {
None => None,
};
let mut results = Vec::new();
for project_path in project_paths {
debug!(parent: None, "Starting {}", project_path.display());
@ -332,6 +337,7 @@ fn format_dev_multi_project(args: &Args) -> anyhow::Result<bool> {
write!(error_file, "{}", result.display(args.format)).unwrap();
error_file.flush().unwrap();
}
results.push(result);
pb_span.pb_inc(1);
}
@ -353,6 +359,35 @@ fn format_dev_multi_project(args: &Args) -> anyhow::Result<bool> {
duration.as_secs_f32(),
);
if let Some(stats_file) = &args.stats_file {
results.sort_by(|result1, result2| result1.name.cmp(&result2.name));
let project_col_len = results
.iter()
.map(|result| result.name.len())
.chain(iter::once("project".len()))
.max()
.unwrap_or_default();
let mut stats_file = BufWriter::new(File::create(stats_file)?);
writeln!(
stats_file,
"| {:<project_col_len$} | similarity index |",
"project"
)?;
writeln!(
stats_file,
"|-{:-<project_col_len$}-|------------------|",
""
)?;
for result in results {
writeln!(
stats_file,
"| {:<project_col_len$} | {:.5} |",
result.name,
result.statistics.similarity_index()
)?;
}
}
if let Some(files_with_errors) = args.files_with_errors {
if total_syntax_error_in_input != files_with_errors {
error!(
@ -433,7 +468,13 @@ fn format_dev_project(
let duration = start.elapsed();
let name = files[0]
.file_name()
.unwrap_or(files[0].as_os_str())
.to_string_lossy()
.to_string();
Ok(CheckRepoResult {
name,
duration,
file_count: formatted_counter,
diagnostics,
@ -511,6 +552,7 @@ fn diff_show_only_changes(
}
struct CheckRepoResult {
name: String,
duration: Duration,
file_count: usize,
diagnostics: Vec<Diagnostic>,

View File

@ -55,10 +55,11 @@ git -C "$dir/cpython" checkout 45de31db9cc9be945702f3a7ca35bbb9f98476af
# Uncomment if you want to update the hashes
# for i in "$dir"/*/; do git -C "$i" switch main && git -C "$i" pull && echo "# $(basename "$i") $(git -C "$i" rev-parse HEAD)"; done
time cargo run --bin ruff_dev -- format-dev --stability-check --error-file "$target/progress_projects_errors.txt" \
--log-file "$target/progress_projects_log.txt" --files-with-errors 25 --multi-project "$dir" || (
time cargo run --bin ruff_dev -- format-dev --stability-check \
--error-file "$target/progress_projects_errors.txt" --log-file "$target/progress_projects_log.txt" --stats-file "$target/progress_projects_stats.txt" \
--files-with-errors 25 --multi-project "$dir" || (
echo "Ecosystem check failed"
cat "$target/progress_projects_log.txt"
exit 1
)
grep "similarity index" "$target/progress_projects_log.txt" | sort
cat "$target/progress_projects_stats.txt"