From d4623d13b731efef499b49576f883a5578297fdd Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 9 Jun 2014 10:28:39 +0200 Subject: [PATCH] [1.7.x] Fixed formtools tests with Python 2 Fixes #19905 again. Backport of 1aaf4053f5 from master. --- .../tests/wizard/namedwizardtests/tests.py | 13 ++++++++----- .../formtools/tests/wizard/wizardtests/tests.py | 14 ++++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py b/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py index bd86dd0f45..4180dfffb5 100644 --- a/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py +++ b/django/contrib/formtools/tests/wizard/namedwizardtests/tests.py @@ -5,6 +5,7 @@ import copy from django.core.urlresolvers import reverse from django.http import QueryDict from django.test import TestCase +from django.utils._os import upath from django.contrib.auth.models import User from django.contrib.auth.tests.utils import skipIfCustomUser @@ -16,6 +17,8 @@ from django.contrib.formtools.tests.wizard.test_forms import get_request, Step1, from .forms import temp_storage +# On Python 2, __file__ may end with .pyc +THIS_FILE = upath(__file__).rstrip("c") UPLOADED_FILE_NAME = 'tests.py' @@ -142,7 +145,7 @@ class NamedWizardTests(object): self.assertEqual(response.context['wizard']['steps'].current, 'form2') post_data = self.wizard_step_data[1] - with open(__file__, 'rb') as post_file: + with open(THIS_FILE, 'rb') as post_file: post_data['form2-file1'] = post_file response = self.client.post( reverse(self.wizard_urlname, @@ -154,7 +157,7 @@ class NamedWizardTests(object): self.assertEqual(response.context['wizard']['steps'].current, 'form3') # Check that the file got uploaded properly. - with open(__file__, 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2: + with open(THIS_FILE, 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2: self.assertEqual(f.read(), f2.read()) response = self.client.post( @@ -197,7 +200,7 @@ class NamedWizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] - with open(__file__, 'rb') as post_file: + with open(THIS_FILE, 'rb') as post_file: post_data['form2-file1'] = post_file response = self.client.post( reverse(self.wizard_urlname, @@ -211,7 +214,7 @@ class NamedWizardTests(object): response = self.client.get(step2_url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['wizard']['steps'].current, 'form2') - with open(__file__, 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2: + with open(THIS_FILE, 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2: self.assertEqual(f.read(), f2.read()) response = self.client.post( @@ -260,7 +263,7 @@ class NamedWizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] - with open(__file__, 'rb') as post_file: + with open(THIS_FILE, 'rb') as post_file: post_data['form2-file1'] = post_file response = self.client.post( reverse(self.wizard_urlname, diff --git a/django/contrib/formtools/tests/wizard/wizardtests/tests.py b/django/contrib/formtools/tests/wizard/wizardtests/tests.py index c56ce847d6..114de922e0 100644 --- a/django/contrib/formtools/tests/wizard/wizardtests/tests.py +++ b/django/contrib/formtools/tests/wizard/wizardtests/tests.py @@ -16,6 +16,8 @@ from django.contrib.formtools.tests.models import Poet, Poem from .forms import temp_storage +# On Python 2, __file__ may end with .pyc +THIS_FILE = upath(__file__.rstrip("c")) UPLOADED_FILE_NAME = 'tests.py' @@ -116,14 +118,14 @@ class WizardTests(object): self.assertEqual(response.context['wizard']['steps'].current, 'form2') post_data = self.wizard_step_data[1] - with open(upath(__file__), 'rb') as post_file: + with open(upath(THIS_FILE), 'rb') as post_file: post_data['form2-file1'] = post_file response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['wizard']['steps'].current, 'form3') # Check that the file got uploaded properly. - with open(upath(__file__), 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2: + with open(THIS_FILE, 'rb') as f, temp_storage.open(UPLOADED_FILE_NAME) as f2: self.assertEqual(f.read(), f2.read()) response = self.client.post(self.wizard_url, self.wizard_step_data[2]) @@ -153,7 +155,7 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] - with open(upath(__file__), 'rb') as post_file: + with open(THIS_FILE, 'rb') as post_file: post_data['form2-file1'] = post_file response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200) @@ -185,7 +187,7 @@ class WizardTests(object): self.assertEqual(response.status_code, 200) post_data = self.wizard_step_data[1] - with open(upath(__file__), 'rb') as post_file: + with open(THIS_FILE, 'rb') as post_file: post_data['form2-file1'] = post_file response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200) @@ -213,7 +215,7 @@ class WizardTests(object): self.assertEqual(response.context['wizard']['steps'].current, 'form2') post_data = self.wizard_step_data[1] - with open(upath(__file__), 'rb') as post_file: + with open(THIS_FILE, 'rb') as post_file: post_data['form2-file1'] = post_file response = self.client.post(self.wizard_url, post_data) self.assertEqual(response.status_code, 200) @@ -332,7 +334,7 @@ class WizardTestKwargs(TestCase): self.wizard_step_data[0]['form1-user'] = self.testuser.pk def test_template(self): - templates = os.path.join(os.path.dirname(upath(__file__)), 'templates') + templates = os.path.join(os.path.dirname(THIS_FILE), 'templates') with self.settings( TEMPLATE_DIRS=list(settings.TEMPLATE_DIRS) + [templates]): response = self.client.get(self.wizard_url)