Skip to content

Commit

Permalink
Run formatter tests also with preview and snapshot the differences
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Oct 19, 2023
1 parent 9bf091e commit f2c60db
Show file tree
Hide file tree
Showing 7 changed files with 624 additions and 9 deletions.
42 changes: 33 additions & 9 deletions crates/ruff_python_formatter/tests/fixtures.rs
@@ -1,5 +1,5 @@
use ruff_formatter::FormatOptions;
use ruff_python_formatter::{format_module_source, PyFormatOptions};
use ruff_python_formatter::{format_module_source, PreviewMode, PyFormatOptions};
use similar::TextDiff;
use std::fmt::{Formatter, Write};
use std::io::BufReader;
Expand Down Expand Up @@ -142,16 +142,40 @@ fn format() {
} else {
let printed =
format_module_source(&content, options.clone()).expect("Formatting to succeed");
let formatted_code = printed.as_code();
let formatted = printed.as_code();

ensure_stability_when_formatting_twice(formatted_code, options, input_path);
ensure_stability_when_formatting_twice(formatted, options.clone(), input_path);

writeln!(
snapshot,
"## Output\n{}",
CodeFrame::new("py", &formatted_code)
)
.unwrap();
// We want to capture the differences in the preview style in our fixtures
let options_preview = options.with_preview(PreviewMode::Enabled);
let printed_preview = format_module_source(&content, options_preview.clone())
.expect("Formatting to succeed");
let formatted_preview = printed_preview.as_code();

ensure_stability_when_formatting_twice(
formatted_preview,
options_preview.clone(),
input_path,
);

if formatted == formatted_preview {
writeln!(snapshot, "## Output\n{}", CodeFrame::new("py", &formatted)).unwrap();
} else {
// Having both snapshots makes it hard to see the difference, so we're keeping only
// diff.
writeln!(
snapshot,
"## Output\n{}\n## Preview changes\n{}",
CodeFrame::new("py", &formatted),
CodeFrame::new(
"diff",
TextDiff::from_lines(formatted, formatted_preview)
.unified_diff()
.header("Stable", "Preview")
)
)
.unwrap();
}
}

insta::with_settings!({
Expand Down
Expand Up @@ -93,4 +93,21 @@ def test3 ():
```


## Preview changes
```diff
--- Stable
+++ Preview
@@ -21,8 +21,7 @@
# formatted
-def test2():
- ...
+def test2(): ...
a = 10
```



Expand Up @@ -549,4 +549,27 @@ if True:
```


## Preview changes
```diff
--- Stable
+++ Preview
@@ -245,13 +245,11 @@
class Path:
if sys.version_info >= (3, 11):
- def joinpath(self):
- ...
+ def joinpath(self): ...
# The .open method comes from pathlib.pyi and should be kept in sync.
@overload
- def open(self):
- ...
+ def open(self): ...
def fakehttp():
```



Expand Up @@ -494,4 +494,45 @@ class QuerySet(AltersData):
```


## Preview changes
```diff
--- Stable
+++ Preview
@@ -25,8 +25,7 @@
pass
-class Test((Aaaa)):
- ...
+class Test((Aaaa)): ...
class Test(
@@ -156,20 +155,17 @@
@dataclass
# Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
-class AltCLIPOutput(ModelOutput):
- ...
+class AltCLIPOutput(ModelOutput): ...
@dataclass
-class AltCLIPOutput: # Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
- ...
+class AltCLIPOutput: ... # Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
@dataclass
class AltCLIPOutput(
# Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
-):
- ...
+): ...
class TestTypeParams[
```



Expand Up @@ -996,4 +996,167 @@ def default_arg_comments2( #
```


## Preview changes
```diff
--- Stable
+++ Preview
@@ -2,8 +2,7 @@
def test(
# comment
# another
-):
- ...
+): ...


# Argument empty line spacing
@@ -12,8 +11,7 @@
a,
# another
b,
-):
- ...
+): ...


### Different function argument wrappings
@@ -57,8 +55,7 @@
b,
# comment
*args,
-):
- ...
+): ...


def kwarg_with_leading_comments(
@@ -66,8 +63,7 @@
b,
# comment
**kwargs,
-):
- ...
+): ...


def argument_with_long_default(
@@ -75,8 +71,7 @@
b=ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
+ [dddddddddddddddddddd, eeeeeeeeeeeeeeeeeeee, ffffffffffffffffffffffff],
h=[],
-):
- ...
+): ...


def argument_with_long_type_annotation(
@@ -85,12 +80,10 @@
| yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
| zzzzzzzzzzzzzzzzzzz = [0, 1, 2, 3],
h=[],
-):
- ...
+): ...


-def test():
- ...
+def test(): ...


# Type parameter empty line spacing
@@ -99,8 +92,7 @@
A,
# another
B,
-]():
- ...
+](): ...


# Type parameter comments
@@ -159,8 +151,7 @@


# Comment
-def with_leading_comment():
- ...
+def with_leading_comment(): ...


# Comment that could be mistaken for a trailing comment of the function declaration when
@@ -192,8 +183,7 @@
# Regression test for https://github.com/astral-sh/ruff/issues/5176#issuecomment-1598171989
def foo(
b=3 + 2, # comment
-):
- ...
+): ...


# Comments on the slash or the star, both of which don't have a node
@@ -454,8 +444,7 @@
def f(
# first
# second
-):
- ...
+): ...


def f( # first
@@ -475,8 +464,7 @@
# first
b,
# second
-):
- ...
+): ...


def f( # first
@@ -484,8 +472,7 @@
# second
b,
# third
-):
- ...
+): ...


def f( # first
@@ -494,8 +481,7 @@
# third
b,
# fourth
-):
- ...
+): ...


def f( # first
@@ -522,17 +508,14 @@
a,
# third
/, # second
-):
- ...
+): ...


# Walrus operator in return type.
-def this_is_unusual() -> (please := no):
- ...
+def this_is_unusual() -> (please := no): ...


-def this_is_unusual(x) -> (please := no):
- ...
+def this_is_unusual(x) -> (please := no): ...


# Regression test for: https://github.com/astral-sh/ruff/issues/7465
```



0 comments on commit f2c60db

Please sign in to comment.