From 6bad5a924b8fcbe1e68ec298434405f5e03b7722 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Tue, 22 Dec 2015 17:37:28 +0100 Subject: [PATCH] Making it possible for static analysis tests to be skipped if the python version doesn't match. --- test/conftest.py | 6 +++++- test/run.py | 31 ++++++++++++++++++++++--------- test/static_analysis/python2.py | 1 + test/test_integration.py | 5 ++++- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index cbec5206..7c7cb925 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -89,11 +89,15 @@ class StaticAnalysisCase(object): The tests also start with `#!`, like the goto_definition tests. """ def __init__(self, path): - self.skip = False self._path = path with open(path) as f: self._source = f.read() + self.skip = False + for line in self._source.splitlines(): + self.skip = self.skip or run.skip_python_version(line) + + def collect_comparison(self): cases = [] for line_nr, line in enumerate(self._source.splitlines(), 1): diff --git a/test/run.py b/test/run.py index a1db7bdb..943a824e 100755 --- a/test/run.py +++ b/test/run.py @@ -112,6 +112,7 @@ Tests look like this:: import os import re import sys +import operator from ast import literal_eval from io import StringIO from functools import reduce @@ -233,6 +234,26 @@ class IntegrationTestCase(object): return compare_cb(self, compare, sorted(wanted)) +def skip_python_version(line): + comp_map = { + '==': 'eq', + '<=': 'le', + '>=': 'ge', + '<': 'gk', + '>': 'lt', + } + # check for python minimal version number + match = re.match(r" *# *python *([<>]=?|==) *(\d+(?:\.\d+)?)$", line) + if match: + minimal_python_version = tuple( + map(int, match.group(2).split("."))) + operation = getattr(operator, comp_map[match.group(1)]) + if not operation(sys.version_info, minimal_python_version): + return "Minimal python version %s" % match.group(1) + + return None + + def collect_file_tests(lines, lines_to_execute): makecase = lambda t: IntegrationTestCase(t, correct, line_nr, column, start, line, path=None, skip=skip) @@ -259,15 +280,7 @@ def collect_file_tests(lines, lines_to_execute): yield makecase(TEST_DEFINITIONS) correct = None else: - # check for python minimal version number - match = re.match(r" *# *python *>= *(\d+(?:\.\d+)?)$", line) - if match: - minimal_python_version = tuple( - map(int, match.group(1).split("."))) - if sys.version_info >= minimal_python_version: - skip = None - else: - skip = "Minimal python version %s" % match.groups(1) + skip = skip or skip_python_version(line) try: r = re.search(r'(?:^|(?<=\s))#([?!<])\s*([^\n]*)', line) # test_type is ? for completion and ! for goto_assignments diff --git a/test/static_analysis/python2.py b/test/static_analysis/python2.py index ad871f1c..4d896e3e 100644 --- a/test/static_analysis/python2.py +++ b/test/static_analysis/python2.py @@ -1,6 +1,7 @@ """ Some special cases of Python 2. """ +# python <= 2.7 # print is syntax: print 1 diff --git a/test/test_integration.py b/test/test_integration.py index edf6ab62..edca7114 100644 --- a/test/test_integration.py +++ b/test/test_integration.py @@ -40,7 +40,10 @@ def test_completion(case, monkeypatch): def test_static_analysis(static_analysis_case): - static_analysis_case.run(assert_static_analysis) + if static_analysis_case.skip is not None: + pytest.skip(static_analysis_case.skip) + else: + static_analysis_case.run(assert_static_analysis) def test_refactor(refactor_case):