diff options
| -rw-r--r-- | django/core/management/base.py | 4 | ||||
| -rw-r--r-- | docs/releases/5.2.10.txt | 4 | ||||
| -rw-r--r-- | tests/user_commands/tests.py | 30 |
3 files changed, 37 insertions, 1 deletions
diff --git a/django/core/management/base.py b/django/core/management/base.py index 12b4b42f61..fd5ca3d805 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -15,6 +15,7 @@ from django.core import checks from django.core.exceptions import ImproperlyConfigured from django.core.management.color import color_style, no_style from django.db import DEFAULT_DB_ALIAS, connections +from django.utils.version import PY314 ALL_CHECKS = "__all__" @@ -57,6 +58,9 @@ class CommandParser(ArgumentParser): ): self.missing_args_message = missing_args_message self.called_from_command_line = called_from_command_line + if PY314: + if os.environ.get("DJANGO_COLORS") == "nocolor" or "--no-color" in sys.argv: + kwargs.setdefault("color", False) super().__init__(**kwargs) def parse_args(self, args=None, namespace=None): diff --git a/docs/releases/5.2.10.txt b/docs/releases/5.2.10.txt index 35626cfedc..0b7ab06d54 100644 --- a/docs/releases/5.2.10.txt +++ b/docs/releases/5.2.10.txt @@ -9,4 +9,6 @@ Django 5.2.10 fixes several bugs in 5.2.9. Bugfixes ======== -* ... +* Fixed a bug where management command colorized help (introduced in + Python 3.14) ignored the :option:`--no-color` option and the + :envvar:`DJANGO_COLORS` setting (:ticket:`36376`). diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py index acc338685e..321bde4ce9 100644 --- a/tests/user_commands/tests.py +++ b/tests/user_commands/tests.py @@ -1,5 +1,6 @@ import os import sys +import unittest from argparse import ArgumentDefaultsHelpFormatter from io import BytesIO, StringIO, TextIOWrapper from pathlib import Path @@ -24,6 +25,7 @@ from django.db import connection from django.test import SimpleTestCase, override_settings from django.test.utils import captured_stderr, extend_sys_path from django.utils import translation +from django.utils.version import PY314 from .management.commands import dance from .utils import AssertFormatterFailureCaughtContext @@ -454,6 +456,34 @@ class CommandTests(SimpleTestCase): self.assertIn("Working...", out.getvalue()) self.assertIs(mocked_flush.called, True) + @unittest.skipUnless(PY314, "Only relevant for Python 3.14+") + def test_color_enabled_by_default(self): + with mock.patch.dict(os.environ, {}, clear=True): + command = BaseCommand() + parser = command.create_parser("prog_name", "subcommand") + self.assertTrue(parser.color) + + @unittest.skipUnless(PY314, "Only relevant for Python 3.14+") + def test_color_disabled_with_django_colors_nocolor(self): + with mock.patch.dict(os.environ, {"DJANGO_COLORS": "nocolor"}): + command = BaseCommand() + parser = command.create_parser("prog_name", "subcommand") + self.assertFalse(parser.color) + + @unittest.skipUnless(PY314, "Only relevant for Python 3.14+") + def test_force_color_does_not_affect_argparse_color(self): + with mock.patch.dict(os.environ, {}, clear=True): + command = BaseCommand(force_color=True) + parser = command.create_parser("prog_name", "subcommand") + self.assertTrue(parser.color) + + @unittest.skipUnless(PY314, "Only relevant for Python 3.14+") + def test_no_color_flag_disables_color(self): + with mock.patch.object(sys, "argv", ["manage.py", "mycommand", "--no-color"]): + command = BaseCommand() + parser = command.create_parser("manage.py", "mycommand") + self.assertFalse(parser.color) + class CommandRunTests(AdminScriptTestCase): """ |
