Skip to content

Commit

Permalink
Add a smoke test to ruff_fmt to debug formatter changes (#4)
Browse files Browse the repository at this point in the history
Adds a smoke test to `ruff_fmt` to debug formatter changes.

This PR changes the return value of `fmt` to `Result<Formatted>` to allow printing the formatted IR in the quick test. The motivation behind returning `Formatted` is that the IR represents the formatted source code. Printing it only changes its representation from the IR to a string.
  • Loading branch information
MichaReiser authored and charliermarsh committed Feb 15, 2023
1 parent 3a801f0 commit 51d04ad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
45 changes: 36 additions & 9 deletions crates/ruff_fmt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use rome_formatter::{format, IndentStyle, Printed, SimpleFormatOptions};
use rome_formatter::{format, Formatted, IndentStyle, SimpleFormatOptions};
use rustpython_parser::lexer::LexResult;

use crate::attachment::attach;
Expand All @@ -24,7 +24,7 @@ pub mod shared_traits;
mod test;
pub mod trivia;

pub fn fmt(contents: &str) -> Result<Printed> {
pub fn fmt(contents: &str) -> Result<Formatted<ASTFormatContext>> {
// Tokenize once.
let tokens: Vec<LexResult> = rustpython_helpers::tokenize(contents);

Expand All @@ -42,7 +42,7 @@ pub fn fmt(contents: &str) -> Result<Printed> {
normalize_newlines(&mut python_cst);
normalize_parentheses(&mut python_cst);

let elements = format!(
format!(
ASTFormatContext::new(
SimpleFormatOptions {
indent_style: IndentStyle::Space(4),
Expand All @@ -51,8 +51,8 @@ pub fn fmt(contents: &str) -> Result<Printed> {
Locator::new(contents)
),
[format::builders::block(&python_cst)]
)?;
elements.print().map_err(Into::into)
)
.map_err(Into::into)
}

#[cfg(test)]
Expand All @@ -73,8 +73,8 @@ mod tests {
let content = std::fs::read_to_string(test_resource_path(
Path::new("fixtures/black").join(path).as_path(),
))?;
let printed = fmt(&content)?;
insta::assert_display_snapshot!(snapshot, printed.as_code());
let formatted = fmt(&content)?;
insta::assert_display_snapshot!(snapshot, formatted.print()?.as_code());
Ok(())
}

Expand All @@ -90,8 +90,35 @@ mod tests {
let content = std::fs::read_to_string(test_resource_path(
Path::new("fixtures/black").join(path).as_path(),
))?;
let printed = fmt(&content)?;
insta::assert_display_snapshot!(snapshot, printed.as_code());
let formatted = fmt(&content)?;
insta::assert_display_snapshot!(snapshot, formatted.print()?.as_code());
Ok(())
}

/// Use this test to debug the formatting of some snipped
#[ignore]
#[test]
fn quick_test() {
let src = r#"
{
k: v for k, v in a_very_long_variable_name_that_exceeds_the_line_length_by_far_keep_going
}
"#;
let formatted = fmt(&src).unwrap();

// Uncomment the `dbg` to print the IR.
// Use `dbg_write!(f, []) instead of `write!(f, [])` in your formatting code to print some IR
// inside of a `Format` implementation
// dbg!(formatted.document());

let printed = formatted.print().unwrap();

assert_eq!(
printed.as_code(),
r#"{
k: v
for k, v in a_very_long_variable_name_that_exceeds_the_line_length_by_far_keep_going
}"#
);
}
}
2 changes: 1 addition & 1 deletion crates/ruff_fmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() -> Result<()> {
let contents = fs::read_to_string(cli.file)?;
#[allow(clippy::print_stdout)]
{
println!("{}", fmt(&contents)?.as_code());
println!("{}", fmt(&contents)?.print()?.as_code());
}
Ok(())
}

0 comments on commit 51d04ad

Please sign in to comment.