From d835b28d01e53857363a9be97b5c7b07bbd1f8f8 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 22 Dec 2023 08:36:19 +0900 Subject: [PATCH] Show preview changes for tests with options (#9223) --- .../ruff_python_formatter/tests/fixtures.rs | 33 +++++++++ ...g_code_examples_dynamic_line_width.py.snap | 66 +++++++++++++++++ .../format@expression__string.py.snap | 26 +++++++ .../tests/snapshots/format@preview.py.snap | 72 +++++++++++++++++++ .../snapshots/format@quote_style.py.snap | 39 ++++++++++ 5 files changed, 236 insertions(+) diff --git a/crates/ruff_python_formatter/tests/fixtures.rs b/crates/ruff_python_formatter/tests/fixtures.rs index ebe0569ffe..dd24207e01 100644 --- a/crates/ruff_python_formatter/tests/fixtures.rs +++ b/crates/ruff_python_formatter/tests/fixtures.rs @@ -157,6 +157,39 @@ fn format() { CodeFrame::new("python", &formatted_code) ) .unwrap(); + + if options.preview().is_enabled() { + continue; + } + + // 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_unchanged_ast(&content, formatted_preview, &options_preview, input_path); + ensure_stability_when_formatting_twice( + formatted_preview, + &options_preview, + input_path, + ); + + if formatted_code != formatted_preview { + // Having both snapshots makes it hard to see the difference, so we're keeping only + // diff. + writeln!( + snapshot, + "#### Preview changes\n{}", + CodeFrame::new( + "diff", + TextDiff::from_lines(formatted_code, formatted_preview) + .unified_diff() + .header("Stable", "Preview") + ) + ) + .unwrap(); + } } } else { // We want to capture the differences in the preview style in our fixtures diff --git a/crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_dynamic_line_width.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_dynamic_line_width.py.snap index 2d2a1afd30..f9f4084d28 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_dynamic_line_width.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_dynamic_line_width.py.snap @@ -535,6 +535,28 @@ def doctest_extra_indent3(): ``` +#### Preview changes +```diff +--- Stable ++++ Preview +@@ -270,9 +270,11 @@ + + Examples + -------- +- >>> df = pl.DataFrame( +- ... {"foo": [1, 2, 3], "bar": [6, 7, 8], "ham": ["a", "b", "c"]} +- ... ) ++ >>> df = pl.DataFrame({ ++ ... "foo": [1, 2, 3], ++ ... "bar": [6, 7, 8], ++ ... "ham": ["a", "b", "c"], ++ ... }) + """ + + +``` + + ### Output 2 ``` indent-style = space @@ -1139,6 +1161,28 @@ def doctest_extra_indent3(): ``` +#### Preview changes +```diff +--- Stable ++++ Preview +@@ -270,9 +270,11 @@ + + Examples + -------- +- >>> df = pl.DataFrame( +- ... {"foo": [1, 2, 3], "bar": [6, 7, 8], "ham": ["a", "b", "c"]} +- ... ) ++ >>> df = pl.DataFrame({ ++ ... "foo": [1, 2, 3], ++ ... "bar": [6, 7, 8], ++ ... "ham": ["a", "b", "c"], ++ ... }) + """ + + +``` + + ### Output 4 ``` indent-style = tab @@ -1876,4 +1920,26 @@ def doctest_extra_indent3(): ``` +#### Preview changes +```diff +--- Stable ++++ Preview +@@ -700,9 +700,11 @@ + + Examples + -------- +- >>> df = pl.DataFrame( +- ... {"foo": [1, 2, 3], "bar": [6, 7, 8], "ham": ["a", "b", "c"]} +- ... ) ++ >>> df = pl.DataFrame({ ++ ... "foo": [1, 2, 3], ++ ... "bar": [6, 7, 8], ++ ... "ham": ["a", "b", "c"], ++ ... }) + """ + + +``` + + diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__string.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__string.py.snap index 7fa4f1e24e..3f6b55d014 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__string.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__string.py.snap @@ -319,6 +319,19 @@ trailing_preferred_quote_texts = [''' "''', ''' ""''', ''' """''', ''' """"'''] ``` +#### Preview changes +```diff +--- Stable ++++ Preview +@@ -1,4 +1,5 @@ + "' test" ++ + '" test' + + '" test' +``` + + ### Output 2 ``` indent-style = space @@ -496,4 +509,17 @@ trailing_preferred_quote_texts = [''' "''', ''' ""''', ''' """''', ''' """"'''] ``` +#### Preview changes +```diff +--- Stable ++++ Preview +@@ -1,4 +1,5 @@ + "' test" ++ + '" test' + + '" test' +``` + + diff --git a/crates/ruff_python_formatter/tests/snapshots/format@preview.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@preview.py.snap index 6922442d67..70cab1ecbd 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@preview.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@preview.py.snap @@ -156,6 +156,78 @@ def f(): ``` +#### Preview changes +```diff +--- Stable ++++ Preview +@@ -1,13 +1,13 @@ + """ + Black's `Preview.module_docstring_newlines` + """ ++ + first_stmt_after_module_level_docstring = 1 + + + class CachedRepository: + # Black's `Preview.dummy_implementations` +- def get_release_info(self): +- ... ++ def get_release_info(self): ... + + + def raw_docstring(): +@@ -27,23 +27,22 @@ + + + class RemoveNewlineBeforeClassDocstring: +- + """Black's `Preview.no_blank_line_before_class_docstring`""" + + + def f(): + """Black's `Preview.prefer_splitting_right_hand_side_of_assignments`""" +- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[ +- bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +- ] = cccccccc.ccccccccccccc.cccccccc ++ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ( ++ cccccccc.ccccccccccccc.cccccccc ++ ) + +- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[ +- bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +- ] = cccccccc.ccccccccccccc().cccccccc ++ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ( ++ cccccccc.ccccccccccccc().cccccccc ++ ) + +- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[ +- bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +- ] = cccccccc.ccccccccccccc(d).cccccccc ++ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ( ++ cccccccc.ccccccccccccc(d).cccccccc ++ ) + + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ( + cccccccc.ccccccccccccc(d).cccccccc + e +@@ -57,9 +56,9 @@ + + eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee + ) + +- self._cache: dict[ +- DependencyCacheKey, list[list[DependencyPackage]] +- ] = collections.defaultdict(list) +- self._cached_dependencies_by_level: dict[ +- int, list[DependencyCacheKey] +- ] = collections.defaultdict(list) ++ self._cache: dict[DependencyCacheKey, list[list[DependencyPackage]]] = ( ++ collections.defaultdict(list) ++ ) ++ self._cached_dependencies_by_level: dict[int, list[DependencyCacheKey]] = ( ++ collections.defaultdict(list) ++ ) +``` + + ### Output 2 ``` indent-style = space diff --git a/crates/ruff_python_formatter/tests/snapshots/format@quote_style.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@quote_style.py.snap index 4cd816c24e..595daf7bf3 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@quote_style.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@quote_style.py.snap @@ -129,6 +129,19 @@ def docstring_single(): ``` +#### Preview changes +```diff +--- Stable ++++ Preview +@@ -1,4 +1,5 @@ + 'single' ++ + 'double' + r'r single' + r'r double' +``` + + ### Output 2 ``` indent-style = space @@ -201,6 +214,19 @@ def docstring_single(): ``` +#### Preview changes +```diff +--- Stable ++++ Preview +@@ -1,4 +1,5 @@ + "single" ++ + "double" + r"r single" + r"r double" +``` + + ### Output 3 ``` indent-style = space @@ -273,4 +299,17 @@ def docstring_single(): ``` +#### Preview changes +```diff +--- Stable ++++ Preview +@@ -1,4 +1,5 @@ + 'single' ++ + "double" + r'r single' + r"r double" +``` + +