From 2aa06e439a29a1c24fa03744395cc57787e7198e Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 14 Feb 2015 19:47:07 -0500 Subject: [PATCH] [1.8.x] Refs #24324 -- Fixed Python 2 test failures when path to Django source contains non-ASCII characters. Backport of 307c0f299a6c26f5231d3516df5b4edc54b36553 from master --- tests/admin_scripts/tests.py | 4 ++-- tests/apps/tests.py | 2 +- tests/migrations/test_commands.py | 15 ++++++++------- tests/migrations/test_writer.py | 3 ++- tests/staticfiles_tests/tests.py | 2 +- tests/template_tests/syntax_tests/test_ssi.py | 2 ++ tests/test_runner/test_debug_sql.py | 6 ++++-- tests/test_runner/tests.py | 3 ++- tests/utils_tests/test_autoreload.py | 17 +++++++++-------- 9 files changed, 31 insertions(+), 23 deletions(-) diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index 99056308701..90f6cc077f8 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -36,7 +36,7 @@ if not os.path.exists(test_dir): os.mkdir(test_dir) open(os.path.join(test_dir, '__init__.py'), 'w').close() -custom_templates_dir = os.path.join(os.path.dirname(__file__), 'custom_templates') +custom_templates_dir = os.path.join(os.path.dirname(upath(__file__)), 'custom_templates') SYSTEM_CHECK_MSG = 'System check identified no issues' @@ -117,7 +117,7 @@ class AdminScriptTestCase(unittest.TestCase): def run_test(self, script, args, settings_file=None, apps=None): base_dir = os.path.dirname(test_dir) # The base dir for Django's tests is one level up. - tests_dir = os.path.dirname(os.path.dirname(__file__)) + tests_dir = os.path.dirname(os.path.dirname(upath(__file__))) # The base dir for Django is one level above the test dir. We don't use # `import django` to figure that out, so we don't pick up a Django # from site-packages or similar. diff --git a/tests/apps/tests.py b/tests/apps/tests.py index 5e23ddb9d11..4d7cd562a34 100644 --- a/tests/apps/tests.py +++ b/tests/apps/tests.py @@ -35,7 +35,7 @@ SOME_INSTALLED_APPS_NAMES = [ 'django.contrib.auth', ] + SOME_INSTALLED_APPS[2:] -HERE = os.path.dirname(__file__) +HERE = os.path.dirname(upath(__file__)) class AppsTests(TestCase): diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index f20a083fb64..f97ba05cb05 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -563,7 +563,7 @@ class MakeMigrationsTests(MigrationTestBase): self.fail("Makemigrations failed while running interactive questioner") finally: questioner.input = old_input - self.assertIn("Created new merge migration", out.getvalue()) + self.assertIn("Created new merge migration", force_text(out.getvalue())) @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"}) def test_makemigrations_handle_merge(self): @@ -572,14 +572,15 @@ class MakeMigrationsTests(MigrationTestBase): """ out = six.StringIO() call_command("makemigrations", "migrations", merge=True, interactive=False, stdout=out) - self.assertIn("Merging migrations", out.getvalue()) - self.assertIn("Branch 0002_second", out.getvalue()) - self.assertIn("Branch 0002_conflicting_second", out.getvalue()) + output = force_text(out.getvalue()) + self.assertIn("Merging migrations", output) + self.assertIn("Branch 0002_second", output) + self.assertIn("Branch 0002_conflicting_second", output) merge_file = os.path.join(self.test_dir, 'test_migrations_conflict', '0003_merge.py') self.assertTrue(os.path.exists(merge_file)) os.remove(merge_file) self.assertFalse(os.path.exists(merge_file)) - self.assertIn("Created new merge migration", out.getvalue()) + self.assertIn("Created new merge migration", output) @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_no_default"}) def test_makemigrations_dry_run(self): @@ -797,7 +798,7 @@ class SquashMigrationsTest(MigrationTestBase): """ out = six.StringIO() call_command("squashmigrations", "migrations", "0002", interactive=False, verbosity=1, stdout=out) - self.assertIn("Optimized from 7 operations to 5 operations.", out.getvalue()) + self.assertIn("Optimized from 7 operations to 5 operations.", force_text(out.getvalue())) @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) def test_ticket_23799_squashmigrations_no_optimize(self): @@ -807,4 +808,4 @@ class SquashMigrationsTest(MigrationTestBase): out = six.StringIO() call_command("squashmigrations", "migrations", "0002", interactive=False, verbosity=1, no_optimize=True, stdout=out) - self.assertIn("Skipping optimization", out.getvalue()) + self.assertIn("Skipping optimization", force_text(out.getvalue())) diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 0f01a877d2c..79a374b5673 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -19,6 +19,7 @@ from django.db.migrations.writer import ( ) from django.test import SimpleTestCase, TestCase, ignore_warnings from django.utils import datetime_safe, six +from django.utils._os import upath from django.utils.deconstruct import deconstructible from django.utils.timezone import FixedOffset, get_default_timezone, utc from django.utils.translation import ugettext_lazy as _ @@ -393,7 +394,7 @@ class WriterTests(TestCase): 'migrations.migrations_test_apps.without_init_file', ] - base_dir = os.path.dirname(os.path.dirname(__file__)) + base_dir = os.path.dirname(os.path.dirname(upath(__file__))) for app in test_apps: with self.modify_settings(INSTALLED_APPS={'append': app}): diff --git a/tests/staticfiles_tests/tests.py b/tests/staticfiles_tests/tests.py index 4ab72ad1ce4..e4b04b52281 100644 --- a/tests/staticfiles_tests/tests.py +++ b/tests/staticfiles_tests/tests.py @@ -241,7 +241,7 @@ class TestFindStatic(CollectionTestCase, TestDefaults): self.assertIn('project', force_text(lines[1])) self.assertIn('apps', force_text(lines[2])) self.assertIn("Looking in the following locations:", force_text(lines[3])) - searched_locations = ', '.join(lines[4:]) + searched_locations = ', '.join(force_text(x) for x in lines[4:]) # AppDirectoriesFinder searched locations self.assertIn(os.path.join('staticfiles_tests', 'apps', 'test', 'static'), searched_locations) diff --git a/tests/template_tests/syntax_tests/test_ssi.py b/tests/template_tests/syntax_tests/test_ssi.py index b000dd6583d..e52931d19bf 100644 --- a/tests/template_tests/syntax_tests/test_ssi.py +++ b/tests/template_tests/syntax_tests/test_ssi.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import os from django.test import SimpleTestCase, ignore_warnings diff --git a/tests/test_runner/test_debug_sql.py b/tests/test_runner/test_debug_sql.py index cc583cbb377..fa7c7f74010 100644 --- a/tests/test_runner/test_debug_sql.py +++ b/tests/test_runner/test_debug_sql.py @@ -5,6 +5,7 @@ from django.db import connection from django.test import TestCase from django.test.runner import DiscoverRunner from django.utils import six +from django.utils.encoding import force_text from .models import Person @@ -42,8 +43,9 @@ class TestDebugSQL(unittest.TestCase): ).run(suite) runner.teardown_databases(old_config) - stream.seek(0) - return stream.read() + if six.PY2: + stream.buflist = [force_text(x) for x in stream.buflist] + return stream.getvalue() def test_output_normal(self): full_output = self._test_output(1) diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py index d94d2643215..f880d4a56c7 100644 --- a/tests/test_runner/tests.py +++ b/tests/test_runner/tests.py @@ -18,6 +18,7 @@ from django.test import ( from django.test.runner import DiscoverRunner, dependency_ordered from django.test.testcases import connections_support_transactions from django.utils import six +from django.utils.encoding import force_text from .models import Person @@ -391,7 +392,7 @@ class DeprecationDisplayTest(AdminScriptTestCase): def test_runner_deprecation_verbosity_default(self): args = ['test', '--settings=test_project.settings', 'test_runner_deprecation_app'] out, err = self.run_django_admin(args) - self.assertIn("Ran 1 test", err) + self.assertIn("Ran 1 test", force_text(err)) six.assertRegex(self, err, r"RemovedInDjango\d\dWarning: warning from test") six.assertRegex(self, err, r"RemovedInDjango\d\dWarning: module-level warning from deprecation_app") diff --git a/tests/utils_tests/test_autoreload.py b/tests/utils_tests/test_autoreload.py index 3b7135ee77f..3fd5e426e30 100644 --- a/tests/utils_tests/test_autoreload.py +++ b/tests/utils_tests/test_autoreload.py @@ -5,7 +5,7 @@ from importlib import import_module from django import conf from django.contrib import admin from django.test import TestCase, override_settings -from django.utils._os import upath +from django.utils._os import npath, upath from django.utils.autoreload import gen_filenames LOCALE_PATH = os.path.join(os.path.dirname(__file__), 'locale') @@ -58,9 +58,10 @@ class TestFilenameGenerator(TestCase): Test that gen_filenames also yields from locale dirs in installed apps. """ filenames = list(gen_filenames()) - self.assertIn(os.path.join(os.path.dirname(admin.__file__), 'locale', - 'nl', 'LC_MESSAGES', 'django.mo'), - filenames) + self.assertIn( + os.path.join(os.path.dirname(upath(admin.__file__)), 'locale', 'nl', 'LC_MESSAGES', 'django.mo'), + filenames + ) @override_settings(USE_I18N=False) def test_no_i18n(self): @@ -70,9 +71,9 @@ class TestFilenameGenerator(TestCase): """ filenames = list(gen_filenames()) self.assertNotIn( - os.path.join(os.path.dirname(conf.__file__), 'locale', 'nl', - 'LC_MESSAGES', 'django.mo'), - filenames) + os.path.join(os.path.dirname(upath(conf.__file__)), 'locale', 'nl', 'LC_MESSAGES', 'django.mo'), + filenames + ) def test_only_new_files(self): """ @@ -91,7 +92,7 @@ class TestFilenameGenerator(TestCase): try: _, filename = os.path.split(filepath) import_module('.%s' % filename.replace('.py', ''), package='utils_tests') - self.assertIn(filepath, gen_filenames()) + self.assertIn(npath(filepath), gen_filenames()) finally: os.close(fd) os.remove(filepath)