From e3953de9003c69f3b2485b7f8f56057a0cd4ef1f Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 21 Feb 2015 19:18:54 +0100 Subject: [PATCH] [1.8.x] Normalized usage of the tempfile module. Specifically stopped using the dir argument. Backport of a8fe12417f778a76837f8e4f8503779f52a396ba from master --- tests/admin_views/tests.py | 3 +-- tests/file_uploads/tests.py | 13 +++---------- tests/gis_tests/geoapp/tests.py | 10 +++++----- tests/model_regress/test_pickle.py | 6 ++++-- tests/utils_tests/test_autoreload.py | 19 ++++++++++--------- tests/view_tests/tests/test_debug.py | 16 ++++++++-------- 6 files changed, 31 insertions(+), 36 deletions(-) diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 2e016515cf..e05a2598b5 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -3322,8 +3322,7 @@ class AdminInlineFileUploadTest(TestCase): # Set up test Picture and Gallery. # These must be set up here instead of in fixtures in order to allow Picture # to use a NamedTemporaryFile. - tdir = tempfile.gettempdir() - file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir) + file1 = tempfile.NamedTemporaryFile(suffix=".file1") file1.write(b'a' * (2 ** 21)) filename = file1.name file1.close() diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py index 1338add474..4888ca569a 100644 --- a/tests/file_uploads/tests.py +++ b/tests/file_uploads/tests.py @@ -50,10 +50,8 @@ class FileUploadTests(TestCase): self.assertEqual(response.status_code, 200) def test_large_upload(self): - tdir = tempfile.gettempdir() - file = tempfile.NamedTemporaryFile - with file(suffix=".file1", dir=tdir) as file1, file(suffix=".file2", dir=tdir) as file2: + with file(suffix=".file1") as file1, file(suffix=".file2") as file2: file1.write(b'a' * (2 ** 21)) file1.seek(0) @@ -262,11 +260,8 @@ class FileUploadTests(TestCase): "Got a long file name (%s characters)." % len(got)) def test_file_content(self): - tdir = tempfile.gettempdir() - file = tempfile.NamedTemporaryFile - with file(suffix=".ctype_extra", dir=tdir) as no_content_type, \ - file(suffix=".ctype_extra", dir=tdir) as simple_file: + with file(suffix=".ctype_extra") as no_content_type, file(suffix=".ctype_extra") as simple_file: no_content_type.write(b'no content') no_content_type.seek(0) @@ -291,10 +286,8 @@ class FileUploadTests(TestCase): def test_content_type_extra(self): """Uploaded files may have content type parameters available.""" - tdir = tempfile.gettempdir() - file = tempfile.NamedTemporaryFile - with file(suffix=".ctype_extra", dir=tdir) as no_content_type, file(suffix=".ctype_extra", dir=tdir) as simple_file: + with file(suffix=".ctype_extra") as no_content_type, file(suffix=".ctype_extra") as simple_file: no_content_type.write(b'something') no_content_type.seek(0) diff --git a/tests/gis_tests/geoapp/tests.py b/tests/gis_tests/geoapp/tests.py index e49dd5118c..0acc2bd210 100644 --- a/tests/gis_tests/geoapp/tests.py +++ b/tests/gis_tests/geoapp/tests.py @@ -1,7 +1,7 @@ from __future__ import unicode_literals import re -from tempfile import NamedTemporaryFile +import tempfile from django.contrib.gis import gdal from django.contrib.gis.geos import HAS_GEOS @@ -219,10 +219,10 @@ class GeoModelTest(TestCase): self.assertIn('"point": "%s"' % houston.point.ewkt, result) # Reload now dumped data - with NamedTemporaryFile(mode='w', suffix='.json') as tempfile: - tempfile.write(result) - tempfile.seek(0) - call_command('loaddata', tempfile.name, verbosity=0) + with tempfile.NamedTemporaryFile(mode='w', suffix='.json') as tmp: + tmp.write(result) + tmp.seek(0) + call_command('loaddata', tmp.name, verbosity=0) self.assertListEqual(original_data, list(City.objects.all().order_by('name'))) diff --git a/tests/model_regress/test_pickle.py b/tests/model_regress/test_pickle.py index aa7002ab27..996c2221d6 100644 --- a/tests/model_regress/test_pickle.py +++ b/tests/model_regress/test_pickle.py @@ -80,14 +80,16 @@ print(article.headline)""" article_text="This is an article", ) - with NamedTemporaryFile(mode='w+', suffix=".py", dir='.') as script: + with NamedTemporaryFile(mode='w+', suffix=".py") as script: script.write(script_template % pickle.dumps(a)) script.flush() + pythonpath = [os.path.dirname(script.name)] + sys.path env = { # Needed to run test outside of tests directory - str('PYTHONPATH'): os.pathsep.join(sys.path), + str('PYTHONPATH'): os.pathsep.join(pythonpath), # Needed on Windows because http://bugs.python.org/issue8557 str('PATH'): os.environ['PATH'], + str('TMPDIR'): os.environ['TMPDIR'], str('LANG'): os.environ.get('LANG', ''), } if 'SYSTEMROOT' in os.environ: # Windows http://bugs.python.org/issue20614 diff --git a/tests/utils_tests/test_autoreload.py b/tests/utils_tests/test_autoreload.py index 3fd5e426e3..ff4cf63e1c 100644 --- a/tests/utils_tests/test_autoreload.py +++ b/tests/utils_tests/test_autoreload.py @@ -5,6 +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.test.utils import extend_sys_path from django.utils._os import npath, upath from django.utils.autoreload import gen_filenames @@ -88,12 +89,12 @@ class TestFilenameGenerator(TestCase): self.assertFalse(any(f.endswith('.pyc') for f in gen_filenames())) def test_deleted_removed(self): - fd, filepath = tempfile.mkstemp(dir=os.path.dirname(upath(__file__)), suffix='.py') - try: - _, filename = os.path.split(filepath) - import_module('.%s' % filename.replace('.py', ''), package='utils_tests') - self.assertIn(npath(filepath), gen_filenames()) - finally: - os.close(fd) - os.remove(filepath) - self.assertNotIn(filepath, gen_filenames()) + dirname = tempfile.mkdtemp() + filename = os.path.join(dirname, 'test_deleted_removed_module.py') + with open(filename, 'w'): + pass + with extend_sys_path(dirname): + import_module('test_deleted_removed_module') + self.assertIn(npath(filename), gen_filenames()) + os.unlink(filename) + self.assertNotIn(filename, gen_filenames()) diff --git a/tests/view_tests/tests/test_debug.py b/tests/view_tests/tests/test_debug.py index 6c04940cf5..650cc5d268 100644 --- a/tests/view_tests/tests/test_debug.py +++ b/tests/view_tests/tests/test_debug.py @@ -9,7 +9,7 @@ import os import re import shutil import sys -from tempfile import NamedTemporaryFile, mkdtemp, mkstemp +import tempfile from unittest import skipIf from django.core import mail @@ -142,8 +142,8 @@ class DebugViewTests(TestCase): def test_template_loader_postmortem(self): """Tests for not existing file""" template_name = "notfound.html" - with NamedTemporaryFile(prefix=template_name) as tempfile: - tempdir = os.path.dirname(tempfile.name) + with tempfile.NamedTemporaryFile(prefix=template_name) as tmpfile: + tempdir = os.path.dirname(tmpfile.name) template_path = os.path.join(tempdir, template_name) with override_settings(TEMPLATES=[{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', @@ -155,9 +155,9 @@ class DebugViewTests(TestCase): @skipIf(sys.platform == "win32", "Python on Windows doesn't have working os.chmod() and os.access().") def test_template_loader_postmortem_notreadable(self): """Tests for not readable file""" - with NamedTemporaryFile() as tempfile: - template_name = tempfile.name - tempdir = os.path.dirname(tempfile.name) + with tempfile.NamedTemporaryFile() as tmpfile: + template_name = tmpfile.name + tempdir = os.path.dirname(tmpfile.name) template_path = os.path.join(tempdir, template_name) os.chmod(template_path, 0o0222) with override_settings(TEMPLATES=[{ @@ -170,7 +170,7 @@ class DebugViewTests(TestCase): def test_template_loader_postmortem_notafile(self): """Tests for not being a file""" try: - template_path = mkdtemp() + template_path = tempfile.mkdtemp() template_name = os.path.basename(template_path) tempdir = os.path.dirname(template_path) with override_settings(TEMPLATES=[{ @@ -295,7 +295,7 @@ class ExceptionReporterTests(TestCase): reporter = ExceptionReporter(None, None, None, None) for newline in ['\n', '\r\n', '\r']: - fd, filename = mkstemp(text=False) + fd, filename = tempfile.mkstemp(text=False) os.write(fd, force_bytes(newline.join(LINES) + newline)) os.close(fd)