mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #35308 -- Handled OSError when launching code formatters.
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
This commit is contained in:
parent
978aae4334
commit
58cc91275a
11 changed files with 110 additions and 10 deletions
1
tests/user_commands/test_files/black
Normal file
1
tests/user_commands/test_files/black
Normal file
|
@ -0,0 +1 @@
|
|||
# This file is not executable, to simulate a broken `black` install.
|
|
@ -1,6 +1,8 @@
|
|||
import os
|
||||
import sys
|
||||
from argparse import ArgumentDefaultsHelpFormatter
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from admin_scripts.tests import AdminScriptTestCase
|
||||
|
@ -15,6 +17,7 @@ from django.core.management.utils import (
|
|||
is_ignored_path,
|
||||
normalize_path_patterns,
|
||||
popen_wrapper,
|
||||
run_formatters,
|
||||
)
|
||||
from django.db import connection
|
||||
from django.test import SimpleTestCase, override_settings
|
||||
|
@ -22,6 +25,7 @@ from django.test.utils import captured_stderr, extend_sys_path
|
|||
from django.utils import translation
|
||||
|
||||
from .management.commands import dance
|
||||
from .utils import AssertFormatterFailureCaughtContext
|
||||
|
||||
|
||||
# A minimal set of apps to avoid system checks running on all apps.
|
||||
|
@ -535,3 +539,24 @@ class UtilsTests(SimpleTestCase):
|
|||
def test_normalize_path_patterns_truncates_wildcard_base(self):
|
||||
expected = [os.path.normcase(p) for p in ["foo/bar", "bar/*/"]]
|
||||
self.assertEqual(normalize_path_patterns(["foo/bar/*", "bar/*/"]), expected)
|
||||
|
||||
def test_run_formatters_handles_oserror_for_black_path(self):
|
||||
cases = [
|
||||
(FileNotFoundError, "nonexistent"),
|
||||
(
|
||||
OSError if sys.platform == "win32" else PermissionError,
|
||||
str(Path(__file__).parent / "test_files" / "black"),
|
||||
),
|
||||
]
|
||||
for exception, location in cases:
|
||||
with (
|
||||
self.subTest(exception.__qualname__),
|
||||
AssertFormatterFailureCaughtContext(
|
||||
self, shutil_which_result=location
|
||||
) as ctx,
|
||||
):
|
||||
run_formatters([], stderr=ctx.stderr)
|
||||
parsed_error = ctx.stderr.getvalue()
|
||||
self.assertIn(exception.__qualname__, parsed_error)
|
||||
if sys.platform != "win32":
|
||||
self.assertIn(location, parsed_error)
|
||||
|
|
23
tests/user_commands/utils.py
Normal file
23
tests/user_commands/utils.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
from io import StringIO
|
||||
from unittest import mock
|
||||
|
||||
|
||||
class AssertFormatterFailureCaughtContext:
|
||||
|
||||
def __init__(self, test, shutil_which_result="nonexistent"):
|
||||
self.stdout = StringIO()
|
||||
self.stderr = StringIO()
|
||||
self.test = test
|
||||
self.shutil_which_result = shutil_which_result
|
||||
|
||||
def __enter__(self):
|
||||
self.mocker = mock.patch(
|
||||
"django.core.management.utils.shutil.which",
|
||||
return_value=self.shutil_which_result,
|
||||
)
|
||||
self.mocker.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
self.mocker.stop()
|
||||
self.test.assertIn("Formatters failed to launch", self.stderr.getvalue())
|
Loading…
Add table
Add a link
Reference in a new issue