Skip to content

Commit

Permalink
fix: use black.format_str to avoid CliRunner which has also multithre…
Browse files Browse the repository at this point in the history
…ading problems (#202, #192)
  • Loading branch information
15r10nk committed Feb 27, 2025
1 parent e175bdc commit 032b05a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ version = "0.20.2"
[project.optional-dependencies]
black = [
"black>=23.3.0",
"click>=8.1.4"
]

dirty-equals =[
"dirty-equals>=0.9.0",
]
Expand Down
48 changes: 35 additions & 13 deletions src/inline_snapshot/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@ def enforce_formatting():
return _config.config.format_command is not None


def file_mode_for_path(path):
from black import FileMode
from black import find_pyproject_toml
from black import parse_pyproject_toml

mode = FileMode()
pyproject_path = find_pyproject_toml((), path)
if pyproject_path is not None:
config = parse_pyproject_toml(pyproject_path)

if "line_length" in config:
mode.line_length = int(config["line_length"])
if "skip_magic_trailing_comma" in config:
mode.magic_trailing_comma = not config["skip_magic_trailing_comma"]
if "skip_string_normalization" in config:
# The ``black`` command line argument is
# ``--skip-string-normalization``, but the parameter for
# ``black.Mode`` needs to be the opposite boolean of
# ``skip-string-normalization``, hence the inverse boolean
mode.string_normalization = not config["skip_string_normalization"]
if "preview" in config:
mode.preview = config["preview"]

return mode


def format_code(text, filename):
if _config.config.format_command is not None:
format_command = _config.config.format_command.format(filename=filename)
Expand All @@ -29,8 +55,7 @@ def format_code(text, filename):
return result.stdout.decode("utf-8")

try:
from black import main
from click.testing import CliRunner
from black import format_str
except ImportError:
raise_problem(
f"""\
Expand All @@ -45,18 +70,15 @@ def format_code(text, filename):
with warnings.catch_warnings():
warnings.simplefilter("ignore")

runner = CliRunner(mix_stderr=False)
result = runner.invoke(
main, ["--stdin-filename", str(filename), "-"], input=text
)
mode = file_mode_for_path(filename)

if result.exit_code != 0:
raise_problem(
"""\
try:
return format_str(text, mode=mode)
except:
raise_problem(
"""\
[b]black could not format your code, which might be caused by this issue:[/b]
[link=https://github.com/15r10nk/inline-snapshot/issues/138]https://github.com/15r10nk/inline-snapshot/issues/138[/link]\
"""
)
return text

return result.stdout
)
return text
46 changes: 40 additions & 6 deletions tests/test_formating.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import platform
import re
import sys
from types import SimpleNamespace

from click.testing import CliRunner

from inline_snapshot import snapshot
from inline_snapshot.testing import Example
Expand All @@ -13,7 +10,10 @@


def test_black_formatting_error(mocker):
mocker.patch.object(CliRunner, "invoke", return_value=SimpleNamespace(exit_code=1))
def custom_format_str():
raise Exception()

mocker.patch("black.format_str", custom_format_str)

Example(
"""\
Expand Down Expand Up @@ -118,7 +118,8 @@ def NoPaths(text):
)
text = re.sub(path_re, "/.../", text, flags=re.MULTILINE)

text = text.replace("python.exe", "python3")
text = text.replace("python.exe", "python")
text = text.replace("python3", "python")

return text

Expand Down Expand Up @@ -169,7 +170,7 @@ def test_a():
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
These changes will be applied, because you used --inline-snapshot=fix
----------------------------------------------------------------------------------------------- Problems -----------------------------------------------------------------------------------------------
The format_command '/.../python3 fmt_cmd.py /.../test_a.py' caused the following error:
The format_command '/.../python fmt_cmd.py /.../test_a.py' caused the following error:
some problem\
"""
)
Expand Down Expand Up @@ -218,3 +219,36 @@ def test_a():
"""
),
)


def test_black_config():

Example(
{
"pyproject.toml": """
[tool.black]
skip_magic_trailing_comma=true
skip_string_normalization=true
preview=true
""",
"test_a.py": """
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot(3)
""",
}
).run_pytest(
["--inline-snapshot=fix"],
changed_files=snapshot(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot(2)
"""
}
),
)

0 comments on commit 032b05a

Please sign in to comment.