Make ruff_cli binary a small wrapper around lib (#3398)

This commit is contained in:
Micha Reiser 2023-03-08 12:11:55 +01:00 committed by GitHub
parent d9dfec30eb
commit a3de791f0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 457 additions and 453 deletions

View file

@ -3,7 +3,7 @@
use std::fs;
use std::path::PathBuf;
use crate::generate_all::REGENERATE_ALL_COMMAND;
use crate::generate_all::{Mode, REGENERATE_ALL_COMMAND};
use anyhow::{bail, Result};
use pretty_assertions::StrComparison;
use ruff::settings::options::Options;
@ -14,11 +14,8 @@ use crate::ROOT_DIR;
#[derive(clap::Args)]
pub struct Args {
/// Write the generated table to stdout (rather than to `ruff.schema.json`).
#[arg(long)]
pub(crate) dry_run: bool,
/// Don't write to the file, check if the file is up-to-date and error if not
#[arg(long)]
pub(crate) check: bool,
#[arg(long, default_value_t, value_enum)]
pub(crate) mode: Mode,
}
pub fn main(args: &Args) -> Result<()> {
@ -27,33 +24,36 @@ pub fn main(args: &Args) -> Result<()> {
let filename = "ruff.schema.json";
let schema_path = PathBuf::from(ROOT_DIR).join(filename);
if args.dry_run {
println!("{schema_string}");
} else if args.check {
let current = fs::read_to_string(schema_path)?;
if current == schema_string {
println!("up-to-date: {filename}");
} else {
let comparison = StrComparison::new(&current, &schema_string);
bail!("{filename} changed, please run `{REGENERATE_ALL_COMMAND}`:\n{comparison}");
match args.mode {
Mode::DryRun => {
println!("{schema_string}");
}
Mode::Check => {
let current = fs::read_to_string(schema_path)?;
if current == schema_string {
println!("up-to-date: {filename}");
} else {
let comparison = StrComparison::new(&current, &schema_string);
bail!("{filename} changed, please run `{REGENERATE_ALL_COMMAND}`:\n{comparison}");
}
}
Mode::Write => {
let file = schema_path;
fs::write(file, schema_string.as_bytes())?;
}
} else {
let file = schema_path;
fs::write(file, schema_string.as_bytes())?;
}
Ok(())
}
#[cfg(test)]
mod test {
use super::{main, Args};
use crate::generate_all::Mode;
use anyhow::Result;
#[test]
fn test_generate_json_schema() -> Result<()> {
main(&Args {
dry_run: false,
check: true,
})
main(&Args { mode: Mode::Check })
}
}