Loading django/core/management/base.py +1 −0 Original line number Diff line number Diff line Loading @@ -306,6 +306,7 @@ 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 Loading tests/user_commands/tests.py +27 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ import sys from django.core import management from django.core.management import CommandError from django.core.management.base import BaseCommand from django.core.management.utils import popen_wrapper from django.test import SimpleTestCase from django.utils import translation Loading Loading @@ -60,6 +61,32 @@ 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): Loading Loading
django/core/management/base.py +1 −0 Original line number Diff line number Diff line Loading @@ -306,6 +306,7 @@ 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 Loading
tests/user_commands/tests.py +27 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ import sys from django.core import management from django.core.management import CommandError from django.core.management.base import BaseCommand from django.core.management.utils import popen_wrapper from django.test import SimpleTestCase from django.utils import translation Loading Loading @@ -60,6 +61,32 @@ 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): Loading