summaryrefslogtreecommitdiff
path: root/testutils.py
diff options
context:
space:
mode:
authorClaudiu Popa <cpopa@cloudbasesolutions.com>2014-11-16 16:12:07 +0200
committerClaudiu Popa <cpopa@cloudbasesolutions.com>2014-11-16 16:12:07 +0200
commit4d5afa47bb733b8f47003729693e9f01c4c449fe (patch)
tree3f0d16790f14dc478e3b8dc3f7c1bba919f01205 /testutils.py
parent62e61f55d3053fe5d50f8f1029d234bc7046911b (diff)
downloadpylint-4d5afa47bb733b8f47003729693e9f01c4c449fe.tar.gz
Move create_file_backed_module to testutils.
Diffstat (limited to 'testutils.py')
-rw-r--r--testutils.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/testutils.py b/testutils.py
index a892258..c0334af 100644
--- a/testutils.py
+++ b/testutils.py
@@ -19,16 +19,18 @@ from __future__ import print_function
import collections
import contextlib
import functools
+import os
import sys
import re
import unittest
+import tempfile
import tokenize
from glob import glob
from os import linesep, getcwd, sep
from os.path import abspath, basename, dirname, isdir, join, splitext
-
+from astroid import test_utils
from pylint import checkers
from pylint.utils import PyLintASTWalker
@@ -372,3 +374,23 @@ def make_tests(input_dir, msg_dir, filter_rgx, callbacks):
def tokenize_str(code):
return list(tokenize.generate_tokens(StringIO(code).readline))
+
+@contextlib.contextmanager
+def create_file_backed_module(code):
+ # 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)
+
+ try:
+ module = test_utils.build_module(code)
+ module.file = tmp
+ yield module
+ finally:
+ os.close(fd)
+ os.remove(tmp)