Commit a8fe1241 authored by Aymeric Augustin's avatar Aymeric Augustin Committed by Aymeric Augustin
Browse files

Normalized usage of the tempfile module.

Specifically stopped using the dir argument.
parent 93440075
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -3315,8 +3315,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()
+3 −10
Original line number Diff line number Diff line
@@ -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)

+5 −5
Original line number Diff line number Diff line
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')))


+4 −2
Original line number Diff line number Diff line
@@ -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
+10 −9
Original line number Diff line number Diff line
@@ -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())
Loading