From 2a9ee49f3cbdc42a98a0c8bc654165f2f35ef3d6 Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Tue, 18 Feb 2014 00:01:03 +0100 Subject: [PATCH] Removed BaseCommand.stdin introduced in 116d39842dab2569013856e9f3701a7cb6554f09. This option is not actually very useful in the general case because it doesn't override sys.stdin. It's still marginally useful for testing some features of the createsuperuser command so it was moved there. This commit also makes the detection of a TTY in createsuperuser a bit more robust, after a suggestion of appolo13. --- .../management/commands/createsuperuser.py | 6 +++- django/contrib/auth/tests/test_management.py | 29 +++++++++++++++++++ django/core/management/base.py | 1 - tests/user_commands/tests.py | 26 ----------------- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/django/contrib/auth/management/commands/createsuperuser.py b/django/contrib/auth/management/commands/createsuperuser.py index 9d18dd430d..0ec4ec39b5 100644 --- a/django/contrib/auth/management/commands/createsuperuser.py +++ b/django/contrib/auth/management/commands/createsuperuser.py @@ -50,6 +50,10 @@ class Command(BaseCommand): option_list = BaseCommand.option_list help = 'Used to create a superuser.' + def execute(self, *args, **options): + self.stdin = options.get('stdin', sys.stdin) # Used for testing + return super(Command, self).execute(*args, **options) + def handle(self, *args, **options): username = options.get(self.UserModel.USERNAME_FIELD, None) interactive = options.get('interactive') @@ -84,7 +88,7 @@ class Command(BaseCommand): default_username = get_default_username() try: - if not self.stdin.isatty(): + if hasattr(self.stdin, 'isatty') and not self.stdin.isatty(): raise NotRunningInTTYException("Not running in a TTY") # Get a username diff --git a/django/contrib/auth/tests/test_management.py b/django/contrib/auth/tests/test_management.py index 16247dbb9a..033b271780 100644 --- a/django/contrib/auth/tests/test_management.py +++ b/django/contrib/auth/tests/test_management.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from datetime import date import locale +import sys from django.apps import apps from django.contrib.auth import models, management @@ -309,6 +310,34 @@ class CreatesuperuserManagementCommandTestCase(TestCase): self.assertEqual(User._default_manager.count(), 0) self.assertIn("Superuser creation skipped", out.getvalue()) + def test_passing_stdin(self): + """ + You can pass a stdin object as an option and it should be + available on self.stdin. + If no such option is passed, it defaults to sys.stdin. + """ + + user_data = {'username': 'foo', 'email': 'foo@example.com'} + sentinel = object() + command = createsuperuser.Command() + command.execute( + stdin=sentinel, + stdout=six.StringIO(), + interactive=False, + username='janet', + email='janet@example.com', + ) + self.assertIs(command.stdin, sentinel) + + command = createsuperuser.Command() + command.execute( + stdout=six.StringIO(), + interactive=False, + username='joe', + email='joe@example.com', + ) + self.assertIs(command.stdin, sys.stdin) + class CustomUserModelValidationTestCase(TestCase): @override_settings(AUTH_USER_MODEL='auth.CustomUserNonListRequiredFields') diff --git a/django/core/management/base.py b/django/core/management/base.py index 9eeca23c47..77c0bc6034 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -306,7 +306,6 @@ class BaseCommand(object): self.stderr = OutputWrapper(options.get('stderr', sys.stderr)) else: self.stderr = OutputWrapper(options.get('stderr', sys.stderr), self.style.ERROR) - self.stdin = options.get('stdin', sys.stdin) # Useful for testing if self.can_import_settings: from django.conf import settings # NOQA diff --git a/tests/user_commands/tests.py b/tests/user_commands/tests.py index e184d0e6df..52665f1a1c 100644 --- a/tests/user_commands/tests.py +++ b/tests/user_commands/tests.py @@ -61,32 +61,6 @@ class CommandTests(SimpleTestCase): management.call_command('leave_locale_alone_true', stdout=out) self.assertEqual(out.getvalue(), "pl\n") - def test_passing_stdin(self): - """ - You can pass a stdin object to a command's options and it should be - available on self.stdin. - """ - class CustomCommand(BaseCommand): - def handle(self, *args, **kwargs): - pass - - sentinel = object() - command = CustomCommand() - command.execute(stdin=sentinel, stdout=StringIO()) - self.assertIs(command.stdin, sentinel) - - def test_passing_stdin_default(self): - """ - If stdin is not passed as an option, the default should be sys.stdin. - """ - class CustomCommand(BaseCommand): - def handle(self, *args, **kwargs): - return 'OK' - - command = CustomCommand() - command.execute(stdout=StringIO()) - self.assertIs(command.stdin, sys.stdin) - class UtilsTests(SimpleTestCase):