summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSkyiesac <jainsachi1202@gmail.com>2025-12-18 16:35:38 +0530
committerJacob Walls <jacobtylerwalls@gmail.com>2025-12-22 14:07:55 -0500
commit8346657aaff7da999dca32574e6595f8c9543a0c (patch)
tree048da1493f30448630d5b00323c1ee3b7c05615c /tests
parent9cc231e8243091519f5d627cd02ee40bbb853ced (diff)
[5.2.x] Fixed #36376 -- Fixed --no-color for command help in Python 3.14+.
https://github.com/python/cpython/pull/136809 made `color` default to True in ArgumentParser. Backport of d0d85cd165e54582cce98cf685252e771460a9d4 from main.
Diffstat (limited to 'tests')
-rw-r--r--tests/user_commands/tests.py30
1 files changed, 30 insertions, 0 deletions
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):
"""