diff options
| author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2014-11-16 16:12:07 +0200 |
|---|---|---|
| committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2014-11-16 16:12:07 +0200 |
| commit | e2ad67904f2e3a8fabf15630f00a72586b3168b1 (patch) | |
| tree | a296858d7c579a178493b12b1edb06ef8a519f84 /testutils.py | |
| parent | 637718a2f73787fc02b8a769a763049bb0f241eb (diff) | |
| download | pylint-git-e2ad67904f2e3a8fabf15630f00a72586b3168b1.tar.gz | |
Move create_file_backed_module to testutils.
Diffstat (limited to 'testutils.py')
| -rw-r--r-- | testutils.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/testutils.py b/testutils.py index a89225830..c0334af0e 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) |
