1
0
mirror of https://github.com/django/django.git synced 2025-03-06 07:22:32 +00:00

[5.2.x] Refs #35515 -- Fixed shell command verbose output when auto-importing 0 or 1 object.

Co-authored-by: Salvo Polizzi <salvopolizzi03@gmail.com>

Backport of 0597e8ad1e55b565292ead732916aa0e39bdf37b from main.
This commit is contained in:
Natalia 2025-02-07 15:25:23 -03:00
parent 7b0c587a68
commit 16c7dc543c
2 changed files with 50 additions and 6 deletions

View File

@ -129,10 +129,13 @@ class Command(BaseCommand):
return namespace
amount = len(namespace)
msg = f"{amount} objects imported automatically"
objects_str = "objects" if amount != 1 else "object"
msg = f"{amount} {objects_str} imported automatically"
if verbosity < 2:
self.stdout.write(f"{msg} (use -v 2 for details).", self.style.SUCCESS)
if amount:
msg += " (use -v 2 for details)"
self.stdout.write(f"{msg}.", self.style.SUCCESS)
return namespace
imports_by_module = defaultdict(list)
@ -163,9 +166,12 @@ class Command(BaseCommand):
else:
import_string = isort.code(import_string)
self.stdout.write(
f"{msg}, including:\n\n{import_string}", self.style.SUCCESS, ending="\n\n"
)
if import_string:
msg = f"{msg}, including:\n\n{import_string}"
else:
msg = f"{msg}."
self.stdout.write(msg, self.style.SUCCESS, ending="\n\n")
return namespace

View File

@ -7,7 +7,7 @@ from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.core.management import CommandError, call_command
from django.core.management.commands import shell
from django.db import models
from django.db import connection, models
from django.test import SimpleTestCase
from django.test.utils import (
captured_stdin,
@ -275,6 +275,44 @@ class ShellCommandAutoImportsTestCase(SimpleTestCase):
" from shell.models import Phone, Marker",
)
def test_message_with_stdout_one_object(self):
class TestCommand(shell.Command):
def get_namespace(self):
return {"connection": connection}
with captured_stdout() as stdout:
TestCommand().get_and_report_namespace(verbosity=2)
cases = {
0: "",
1: "1 object imported automatically (use -v 2 for details).",
2: (
"1 object imported automatically, including:\n\n"
" from django.utils.connection import connection"
),
}
for verbosity, expected in cases.items():
with self.subTest(verbosity=verbosity):
with captured_stdout() as stdout:
TestCommand().get_and_report_namespace(verbosity=verbosity)
self.assertEqual(stdout.getvalue().strip(), expected)
def test_message_with_stdout_zero_objects(self):
class TestCommand(shell.Command):
def get_namespace(self):
return {}
cases = {
0: "",
1: "0 objects imported automatically.",
2: "0 objects imported automatically.",
}
for verbosity, expected in cases.items():
with self.subTest(verbosity=verbosity):
with captured_stdout() as stdout:
TestCommand().get_and_report_namespace(verbosity=verbosity)
self.assertEqual(stdout.getvalue().strip(), expected)
@override_settings(INSTALLED_APPS=["shell", "django.contrib.contenttypes"])
def test_message_with_stdout_listing_objects_with_isort(self):
sorted_imports = (