Chromium Code Reviews| Index: expect_tests/pipeline.py |
| diff --git a/expect_tests/pipeline.py b/expect_tests/pipeline.py |
| index 41fa94c43ac58e0316793fa2e04cf3fefd71e8fd..f8532d7650c415d8ffd8f221fa1dca0a8d05014e 100644 |
| --- a/expect_tests/pipeline.py |
| +++ b/expect_tests/pipeline.py |
| @@ -2,13 +2,14 @@ |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| -import Queue |
| +import ConfigParser |
| import glob |
| import imp |
| import inspect |
| import logging |
| import multiprocessing |
| import os |
| +import Queue |
| import re |
| import signal |
| import sys |
| @@ -26,6 +27,9 @@ from expect_tests.unittest_helper import _is_unittest, UnittestTestCase |
| from expect_tests import util |
| +CONFIG_FILE_NAME='.expect_tests.cfg' |
| + |
| + |
| class ResetableStringIO(object): |
| def __init__(self): |
| self._stream = StringIO() |
| @@ -55,11 +59,10 @@ def get_package_path(package_name, path): |
| return package_path if ispkg else None |
| -def get_blacklist(path): |
| +def get_config(path): |
| """Get blacklisted subdirectories |
| - Reads the file called __expect_tests_skip in provided path, remove |
| - comments, and returns content. |
| + Reads the config file in provided path, and returns content. |
| A comment starts with #, on its own line (no trailing comment). |
| @@ -70,19 +73,25 @@ def get_blacklist(path): |
| blacklist (set of str): name of blacklisted subdirectories. |
| """ |
| black_list = set() |
| - black_list_filename = os.path.join(path, '__expect_tests_skip') |
| - if os.path.isfile(black_list_filename): |
| - with open(black_list_filename, 'r') as f: |
| - for line in f: |
| - stripped = line.strip() |
| - if stripped and not stripped.startswith('#'): |
| - black_list.add(stripped) |
| + python_path = '' |
| + black_list_filename = os.path.join(path, CONFIG_FILE_NAME) |
|
dnj
2014/09/19 21:15:59
If you're generalizing this to config file, the va
pgervais
2014/09/19 22:06:26
Done.
|
| + parser = ConfigParser.ConfigParser() |
| + parser.read([black_list_filename]) |
| + |
| + if not parser.has_section('expect_tests'): |
| + return black_list, python_path |
| - return black_list |
| + if parser.has_option('expect_tests', 'skip'): |
| + black_list = parser.get('expect_tests', 'skip').splitlines() |
| + if parser.has_option('expect_tests', 'pythonpath'): |
| + python_path = [os.path.normpath(os.path.join(path, s.strip())) |
| + for s |
| + in parser.get('expect_tests', 'pythonpath').splitlines()] |
| + return black_list, python_path |
| -def walk_package(package_name, path): |
| - """Yield all test files inside a single package. |
| +def walk_package(package_name, path=None): |
| + """Return all test files inside a single package. |
| In all cases, this function returns the full package name of files ending |
| in '_test.py' found either under the package called <package_name>. |
| @@ -91,7 +100,7 @@ def walk_package(package_name, path): |
| Provided that <path> is in sys.path, calling __import__ with one of the |
| strings returned by this function works. |
| - If a __expect_tests_skip file is present somewhere in the search hierarchy, |
| + If a config file is present somewhere in the search hierarchy, |
| it is interpreted as a list of subdirectories to ignore. This is the way to |
| make this function ignore some subpackages. |
| @@ -103,16 +112,24 @@ def walk_package(package_name, path): |
| It is not necessary to change sys.path for the present function to work (it |
| does not actually import anything). |
| + Args: |
| + package_name (str): name of the package, as expected by import. |
| + path (str): path containing the above module (optional) |
| + |
| + Returns: |
| + test_modules (list of str): name of modules containing tests. Each element is |
| + a period-separated string ending with '_test', |
| + e.g. shiny_package.subpackage.feature_test |
| + pythonpath (list of str): paths to add to sys.path, as requested in |
| + the config files. |
| + |
| Example: |
| - modules = walk_package('shiny_package', 'some/directory') |
| - sys.path.insert(0, 'some/directory') |
| + modules, pythonpath = walk_package('shiny_package', 'some/directory') |
| + sys.path[:] = ['some/directory'] + pythonpath + sys.path |
| __import__(modules[0]) |
| the first line assumes that the directory 'some/directory' is in the |
| current working directory. |
| - |
| - modules[0] is a period-separated string ending with '_test', |
| - e.g. shiny_package.subpackage.feature_test |
| """ |
| assert package_name, 'walk_package needs a package_name.' |
| @@ -120,24 +137,28 @@ def walk_package(package_name, path): |
| # interface. |
| subpackage_name = package_name.split('.')[-1] |
| - ret = [] |
| + pythonpath = [] |
| + test_modules = [] |
| package_path = get_package_path(subpackage_name, path) |
| assert package_path, 'no package found.' |
| # Look for direct subpackages and test files. |
| - black_list = get_blacklist(package_path) |
| + black_list, extra_paths = get_config(package_path) |
| + pythonpath.extend(extra_paths) |
| for filename in filter(lambda x: x not in black_list, |
| os.listdir(package_path)): |
| if os.path.isfile(os.path.join(package_path, filename, '__init__.py')): |
| - ret += walk_package( |
| + extra_test_modules, extra_paths = walk_package( |
| (package_name + '.' if package_name else '') |
| + filename, package_path) |
| + test_modules.extend(extra_test_modules) |
| + pythonpath.extend(extra_paths) |
| continue |
| if filename.endswith('_test.py'): |
| - ret.append( |
| + test_modules.append( |
| (package_name + '.' if package_name else '') |
| + inspect.getmodulename(filename)) |
| - return ret |
| + return test_modules, pythonpath |
| def load_module(modname): |
| @@ -168,7 +189,7 @@ def get_test_gens_directory(path): |
| sys.path.insert(0, os.path.abspath(path)) |
| test_gens = [] |
| - black_list = get_blacklist(path) |
| + black_list, _ = get_config(path) |
| for filename in filter(lambda x: x not in black_list, os.listdir(path)): |
| abs_filename = os.path.join(path, filename) |
| @@ -203,7 +224,12 @@ def get_test_gens_package(package, update_syspath=True): |
| sys.path.insert(0, path) |
| package_name = os.path.split(package.rstrip(os.path.sep))[-1] |
| - for modname in walk_package(package_name, path): |
| + test_modules, pythonpath = walk_package(package_name, path) |
| + |
| + if pythonpath: |
| + sys.path[:] = pythonpath + sys.path |
| + |
| + for modname in test_modules: |
| mod = load_module(modname) |
| for obj in mod.__dict__.values(): |
| if util.is_test_generator(obj): |