From ef211e52f07a130fc94706d2e00194537dce5767 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 16 Aug 2023 09:52:23 +0100 Subject: [PATCH] Support passing --preview through to Black --- CHANGELOG.rst | 2 ++ README.rst | 22 +++++++++++++++++++--- src/blacken_docs/__init__.py | 14 ++++++++------ tests/test_blacken_docs.py | 29 +++++++++++++++++++++++++++-- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index acfc996..6073c73 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,6 +16,8 @@ Changelog Thanks to Julianus Pfeuffer for the report in `Issue #218 `__. +* Support passing the ``--preview`` option through to Black, to select the future style. + * Remove ``language_version`` from ``.pre-commit-hooks.yaml``. This change allows ``default_language_version`` in ``.pre-commit-config.yaml` to take precedence. diff --git a/README.rst b/README.rst index ee7cdcb..00247ed 100644 --- a/README.rst +++ b/README.rst @@ -94,9 +94,25 @@ __ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core blacken-docs currently passes the following options through to Black: -* ``-l`` / ``--line-length`` -* ``-t`` / ``--target-version`` -* ``-S`` / ``--skip-string-normalization`` +* |-l / --line-length|__ + + .. |-l / --line-length| replace:: ``-l`` / ``--line-length`` + __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#l-line-length + +* |--preview|__ + + .. |--preview| replace:: ``--preview`` + __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#preview + +* |-S / --skip-string-normalization|__ + + .. |-S / --skip-string-normalization| replace:: ``-S`` / ``--skip-string-normalization`` + __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#s-skip-string-normalization + +* |-t / --target-version|__ + + .. |-t / --target-version| replace:: ``-t`` / ``--target-version`` + __ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#t-target-version It also has the below extra options: diff --git a/src/blacken_docs/__init__.py b/src/blacken_docs/__init__.py index ba2c40b..8d174cf 100644 --- a/src/blacken_docs/__init__.py +++ b/src/blacken_docs/__init__.py @@ -261,6 +261,12 @@ def main(argv: Sequence[str] | None = None) -> int: type=int, default=DEFAULT_LINE_LENGTH, ) + parser.add_argument("--preview", action="store_true") + parser.add_argument( + "-S", + "--skip-string-normalization", + action="store_true", + ) parser.add_argument( "-t", "--target-version", @@ -270,11 +276,6 @@ def main(argv: Sequence[str] | None = None) -> int: help=f"choices: {[v.name.lower() for v in TargetVersion]}", dest="target_versions", ) - parser.add_argument( - "-S", - "--skip-string-normalization", - action="store_true", - ) parser.add_argument("-E", "--skip-errors", action="store_true") parser.add_argument( "--rst-literal-blocks", @@ -284,9 +285,10 @@ def main(argv: Sequence[str] | None = None) -> int: args = parser.parse_args(argv) black_mode = black.FileMode( - target_versions=set(args.target_versions), line_length=args.line_length, + preview=args.preview, string_normalization=not args.skip_string_normalization, + target_versions=set(args.target_versions), ) retv = 0 diff --git a/tests/test_blacken_docs.py b/tests/test_blacken_docs.py index d8e64be..b845e1d 100644 --- a/tests/test_blacken_docs.py +++ b/tests/test_blacken_docs.py @@ -470,9 +470,10 @@ def test_integration_line_length(tmp_path): ) result = blacken_docs.main((str(f), "--line-length=80")) - assert result == 0 - assert blacken_docs.main((str(f), "--line-length=50")) + + result2 = blacken_docs.main((str(f), "--line-length=50")) + assert result2 == 1 assert f.read_text() == ( "```python\n" "foo(\n" @@ -483,6 +484,30 @@ def test_integration_line_length(tmp_path): ) +def test_integration_preview(tmp_path): + f = tmp_path / "f.md" + f.write_text( + dedent( + """\ + ```python + x = 'a' 'b' + ``` + """ + ) + ) + + result = blacken_docs.main((str(f), "--preview")) + + assert result == 1 + assert f.read_text() == dedent( + """\ + ```python + x = "ab" + ``` + """ + ) + + def test_integration_py36(tmp_path): f = tmp_path / "f.md" f.write_text(