Skip to content

Commit

Permalink
[#2977] Remove ckan.tests.helpers.temp_file context manager.
Browse files Browse the repository at this point in the history
In most cases one can simply use `tempfile.NamedTemporaryFile`
instead.
  • Loading branch information
torfsen committed Sep 12, 2016
1 parent 6379585 commit 4fcc8c1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 28 deletions.
25 changes: 0 additions & 25 deletions ckan/tests/helpers.py
Expand Up @@ -27,7 +27,6 @@
import logging
import os
import re
import tempfile

import webtest
import nose.tools
Expand Down Expand Up @@ -666,27 +665,3 @@ def clear(self):
Clear all captured log messages.
'''
self.messages = collections.defaultdict(list)


@contextlib.contextmanager
def temp_file(*args, **kwargs):
u'''
Context manager that provides a temporary file.
The temporary file is named and open. It is automatically deleted
when the context manager is left if it still exists at that point.
Any arguments are passed on to
:py:func:`tempfile.NamedTemporaryFile`.
'''
kwargs['delete'] = False
f = tempfile.NamedTemporaryFile(*args, **kwargs)
try:
yield f
finally:
f.close()
try:
os.remove(f.name)
except OSError as e:
if e.errno != errno.ENOENT:
raise
7 changes: 4 additions & 3 deletions ckan/tests/lib/test_cli.py
Expand Up @@ -6,6 +6,7 @@
import os.path
from StringIO import StringIO
import sys
import tempfile

from nose.tools import (assert_raises, eq_ as eq, ok_ as ok, assert_in,
assert_not_in, assert_not_equal as neq, assert_false as nok)
Expand Down Expand Up @@ -320,7 +321,7 @@ def test_worker_default_queue(self):
'''
Test ``jobs worker`` with the default queue.
'''
with helpers.temp_file() as f:
with tempfile.NamedTemporaryFile(delete=False) as f:
self.enqueue(os.remove, args=[f.name])
paster(u'jobs', u'worker', u'--burst')
all_jobs = self.all_jobs()
Expand All @@ -331,8 +332,8 @@ def test_worker_specific_queues(self):
'''
Test ``jobs worker`` with specific queues.
'''
with helpers.temp_file() as f:
with helpers.temp_file() as g:
with tempfile.NamedTemporaryFile(delete=False) as f:
with tempfile.NamedTemporaryFile(delete=False) as g:
job1 = self.enqueue()
job2 = self.enqueue(queue=u'q2')
self.enqueue(os.remove, args=[f.name], queue=u'q3')
Expand Down

0 comments on commit 4fcc8c1

Please sign in to comment.