summaryrefslogtreecommitdiff
path: root/testutils.py
diff options
context:
space:
mode:
authorClaudiu Popa <cpopa@cloudbasesolutions.com>2014-11-16 23:33:04 +0200
committerClaudiu Popa <cpopa@cloudbasesolutions.com>2014-11-16 23:33:04 +0200
commit609bafff654d45f8beb812860146730dee34721a (patch)
tree869b5e68a64fee7205e766ccafdba84fba2d2c71 /testutils.py
parente2ad67904f2e3a8fabf15630f00a72586b3168b1 (diff)
downloadpylint-git-609bafff654d45f8beb812860146730dee34721a.tar.gz
Add create_tempfile function to testutils and use it in create_file_backed_module.
Diffstat (limited to 'testutils.py')
-rw-r--r--testutils.py32
1 files changed, 22 insertions, 10 deletions
diff --git a/testutils.py b/testutils.py
index c0334af0e..54e508541 100644
--- a/testutils.py
+++ b/testutils.py
@@ -376,21 +376,33 @@ def tokenize_str(code):
return list(tokenize.generate_tokens(StringIO(code).readline))
@contextlib.contextmanager
-def create_file_backed_module(code):
+def create_tempfile(content=None):
+ """Create a new temporary file.
+
+ If *content* parameter is given, then it will be written
+ in the temporary file, before passing it back.
+ This is a context manager and should be used with a *with* statement.
+ """
# Can't use tempfile.NamedTemporaryFile here
# because on Windows the file must be closed before writing to it,
# see http://bugs.python.org/issue14243
fd, tmp = tempfile.mkstemp()
- if sys.version_info >= (3, 0):
- # erff
- os.write(fd, bytes(code, 'ascii'))
- else:
- os.write(fd, code)
-
+ if content:
+ if sys.version_info >= (3, 0):
+ # erff
+ os.write(fd, bytes(content, 'ascii'))
+ else:
+ os.write(fd, content)
try:
- module = test_utils.build_module(code)
- module.file = tmp
- yield module
+ yield tmp
finally:
os.close(fd)
os.remove(tmp)
+
+@contextlib.contextmanager
+def create_file_backed_module(code):
+ """Create an astroid module for the given code, backed by a real file."""
+ with create_tempfile() as temp:
+ module = test_utils.build_module(code)
+ module.file = temp
+ yield module