Commit 494ba051 authored by Loic Bistuer's avatar Loic Bistuer
Browse files

Made testing of stdout and stderr more consistent.

Refs #23663.
parent 53353230
Loading
Loading
Loading
Loading
+27 −32
Original line number Diff line number Diff line
@@ -1524,14 +1524,12 @@ class CommandTypes(AdminScriptTestCase):
        Test run_from_argv properly terminates even with custom execute() (#19665)
        Also test proper traceback display.
        """
        command = BaseCommand()
        err = StringIO()
        command = BaseCommand(stderr=err)

        def raise_command_error(*args, **kwargs):
            raise CommandError("Custom error")

        old_stderr = sys.stderr
        sys.stderr = err = StringIO()
        try:
        command.execute = lambda args: args  # This will trigger TypeError

        # If the Exception is not CommandError it should always
@@ -1556,8 +1554,6 @@ class CommandTypes(AdminScriptTestCase):
        err.truncate(0)
        with self.assertRaises(CommandError):
            command.run_from_argv(['', '', '--traceback'])
        finally:
            sys.stderr = old_stderr

    def test_run_from_argv_non_ascii_error(self):
        """
@@ -1567,9 +1563,8 @@ class CommandTypes(AdminScriptTestCase):
        def raise_command_error(*args, **kwargs):
            raise CommandError("Erreur personnalisée")

        command = BaseCommand()
        command = BaseCommand(stderr=StringIO())
        command.execute = raise_command_error
        command.stderr = StringIO()

        with self.assertRaises(SystemExit):
            command.run_from_argv(['', ''])
+6 −9
Original line number Diff line number Diff line
@@ -912,12 +912,9 @@ class DBCacheTests(BaseCacheTests, TransactionTestCase):
        self._perform_cull_test(caches['zero_cull'], 50, 18)

    def test_second_call_doesnt_crash(self):
        stdout = six.StringIO()
        management.call_command(
            'createcachetable',
            stdout=stdout
        )
        self.assertEqual(stdout.getvalue(),
        out = six.StringIO()
        management.call_command('createcachetable', stdout=out)
        self.assertEqual(out.getvalue(),
            "Cache table 'test cache table' already exists.\n" * len(settings.CACHES))

    def test_createcachetable_with_table_argument(self):
@@ -926,14 +923,14 @@ class DBCacheTests(BaseCacheTests, TransactionTestCase):
        specifying the table name).
        """
        self.drop_table()
        stdout = six.StringIO()
        out = six.StringIO()
        management.call_command(
            'createcachetable',
            'test cache table',
            verbosity=2,
            stdout=stdout
            stdout=out,
        )
        self.assertEqual(stdout.getvalue(),
        self.assertEqual(out.getvalue(),
            "Cache table 'test cache table' created.\n")

    def test_clear_commits_transaction(self):
+3 −6
Original line number Diff line number Diff line
@@ -10,10 +10,6 @@ class MultiDBChangepasswordManagementCommandTestCase(TestCase):

    def setUp(self):
        self.user = models.User.objects.db_manager('other').create_user(username='joe', password='qwerty')
        self.stdout = StringIO()

    def tearDown(self):
        self.stdout.close()

    def test_that_changepassword_command_with_database_option_uses_given_db(self):
        """
@@ -24,8 +20,9 @@ class MultiDBChangepasswordManagementCommandTestCase(TestCase):
        command = changepassword.Command()
        command._get_pass = lambda *args: 'not qwerty'

        command.execute(username="joe", database='other', stdout=self.stdout)
        command_output = self.stdout.getvalue().strip()
        out = StringIO()
        command.execute(username="joe", database='other', stdout=out)
        command_output = out.getvalue().strip()

        self.assertEqual(command_output, "Changing password for user 'joe'\nPassword changed successfully for user 'joe'")
        self.assertTrue(models.User.objects.using('other').get(username="joe").check_password("not qwerty"))
+15 −15
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ class TestFixtures(TestCase):
            name='Platypus',
            latin_name='Ornithorhynchus anatinus',
            count=2,
            weight=2.2
            weight=2.2,
        )
        animal.save()
        self.assertGreater(animal.id, 1)
@@ -75,7 +75,7 @@ class TestFixtures(TestCase):
            management.call_command(
                'loaddata',
                'sequence_extra',
                verbosity=0
                verbosity=0,
            )

    def test_loaddata_not_found_fields_ignore(self):
@@ -359,20 +359,20 @@ class TestFixtures(TestCase):
            name='Platypus',
            latin_name='Ornithorhynchus anatinus',
            count=2,
            weight=2.2
            weight=2.2,
        )
        animal.save()

        stdout = StringIO()
        out = StringIO()
        management.call_command(
            'dumpdata',
            'fixtures_regress.animal',
            format='json',
            stdout=stdout
            stdout=out,
        )

        # Output order isn't guaranteed, so check for parts
        data = stdout.getvalue()
        data = out.getvalue()

        # Get rid of artifacts like '000000002' to eliminate the differences
        # between different Python versions.
@@ -393,7 +393,7 @@ class TestFixtures(TestCase):
        """
        Regression for #11428 - Proxy models aren't included when you dumpdata
        """
        stdout = StringIO()
        out = StringIO()
        # Create an instance of the concrete class
        widget = Widget.objects.create(name='grommet')
        management.call_command(
@@ -401,10 +401,10 @@ class TestFixtures(TestCase):
            'fixtures_regress.widget',
            'fixtures_regress.widgetproxy',
            format='json',
            stdout=stdout
            stdout=out,
        )
        self.assertJSONEqual(
            stdout.getvalue(),
            out.getvalue(),
            """[{"pk": %d, "model": "fixtures_regress.widget", "fields": {"name": "grommet"}}]"""
            % widget.pk
        )
@@ -554,7 +554,7 @@ class NaturalKeyFixtureTests(TestCase):
            verbosity=0,
        )

        stdout = StringIO()
        out = StringIO()
        management.call_command(
            'dumpdata',
            'fixtures_regress.book',
@@ -564,10 +564,10 @@ class NaturalKeyFixtureTests(TestCase):
            format='json',
            use_natural_foreign_keys=True,
            use_natural_primary_keys=True,
            stdout=stdout,
            stdout=out,
        )
        self.assertJSONEqual(
            stdout.getvalue(),
            out.getvalue(),
            """[{"fields": {"main": null, "name": "Amazon"}, "model": "fixtures_regress.store"}, {"fields": {"main": null, "name": "Borders"}, "model": "fixtures_regress.store"}, {"fields": {"name": "Neal Stephenson"}, "model": "fixtures_regress.person"}, {"pk": 1, "model": "fixtures_regress.book", "fields": {"stores": [["Amazon"], ["Borders"]], "name": "Cryptonomicon", "author": ["Neal Stephenson"]}}]"""
        )

@@ -800,19 +800,19 @@ class M2MNaturalKeyFixtureTests(TestCase):
        a.b_set.add(b1)
        a.b_set.add(b2)

        stdout = StringIO()
        out = StringIO()
        management.call_command(
            'dumpdata',
            'fixtures_regress.M2MSimpleA',
            'fixtures_regress.M2MSimpleB',
            use_natural_foreign_keys=True,
            stdout=stdout
            stdout=out,
        )

        for model in [M2MSimpleA, M2MSimpleB]:
            model.objects.all().delete()

        objects = serializers.deserialize("json", stdout.getvalue())
        objects = serializers.deserialize("json", out.getvalue())
        for obj in objects:
            obj.save()

+9 −9
Original line number Diff line number Diff line
@@ -57,10 +57,10 @@ class ExtractorTests(SimpleTestCase):

    def _run_makemessages(self, **options):
        os.chdir(self.test_dir)
        stdout = StringIO()
        out = StringIO()
        management.call_command('makemessages', locale=[LOCALE], verbosity=2,
            stdout=stdout, **options)
        output = stdout.getvalue()
            stdout=out, **options)
        output = out.getvalue()
        self.assertTrue(os.path.exists(self.PO_FILE))
        with open(self.PO_FILE, 'r') as fp:
            po_contents = fp.read()
@@ -224,19 +224,19 @@ class BasicExtractorTests(ExtractorTests):
        os.chdir(self.test_dir)
        shutil.copyfile('./not_utf8.sample', './not_utf8.txt')
        self.addCleanup(self.rmfile, os.path.join(self.test_dir, 'not_utf8.txt'))
        stdout = StringIO()
        management.call_command('makemessages', locale=[LOCALE], stdout=stdout)
        out = StringIO()
        management.call_command('makemessages', locale=[LOCALE], stdout=out)
        self.assertIn("UnicodeDecodeError: skipped file not_utf8.txt in .",
                      force_text(stdout.getvalue()))
                      force_text(out.getvalue()))

    def test_extraction_warning(self):
        """test xgettext warning about multiple bare interpolation placeholders"""
        os.chdir(self.test_dir)
        shutil.copyfile('./code.sample', './code_sample.py')
        self.addCleanup(self.rmfile, os.path.join(self.test_dir, 'code_sample.py'))
        stdout = StringIO()
        management.call_command('makemessages', locale=[LOCALE], stdout=stdout)
        self.assertIn("code_sample.py:4", force_text(stdout.getvalue()))
        out = StringIO()
        management.call_command('makemessages', locale=[LOCALE], stdout=out)
        self.assertIn("code_sample.py:4", force_text(out.getvalue()))

    def test_template_message_context_extractor(self):
        """
Loading