Skip to content

Commit

Permalink
Normalized usage of the tempfile module.
Browse files Browse the repository at this point in the history
Specifically stopped using the dir argument.
  • Loading branch information
aaugustin committed Feb 23, 2015
1 parent 9344007 commit a8fe124
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 36 deletions.
3 changes: 1 addition & 2 deletions tests/admin_views/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3315,8 +3315,7 @@ def setUp(self):
# 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()
Expand Down
13 changes: 3 additions & 10 deletions tests/file_uploads/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ def test_simple_upload(self):
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)

Expand Down Expand Up @@ -262,11 +260,8 @@ def test_filename_overflow(self):
"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)

Expand All @@ -291,10 +286,8 @@ def test_file_content(self):

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)

Expand Down
10 changes: 5 additions & 5 deletions tests/gis_tests/geoapp/tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -219,10 +219,10 @@ def test_dumpdata_loaddata_cycle(self):
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')))


Expand Down
6 changes: 4 additions & 2 deletions tests/model_regress/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ def test_unpickling_when_appregistrynotready(self):
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
Expand Down
19 changes: 10 additions & 9 deletions tests/utils_tests/test_autoreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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

Expand Down Expand Up @@ -88,12 +89,12 @@ def test_only_new_files(self):
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())
16 changes: 8 additions & 8 deletions tests/view_tests/tests/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import re
import shutil
import sys
from tempfile import NamedTemporaryFile, mkdtemp, mkstemp
import tempfile
from unittest import skipIf

from django.core import mail
Expand Down Expand Up @@ -142,8 +142,8 @@ def test_template_exceptions(self):
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',
Expand All @@ -155,9 +155,9 @@ def test_template_loader_postmortem(self):
@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=[{
Expand All @@ -170,7 +170,7 @@ def test_template_loader_postmortem_notreadable(self):
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=[{
Expand Down Expand Up @@ -295,7 +295,7 @@ def test_eol_support(self):
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)

Expand Down

0 comments on commit a8fe124

Please sign in to comment.