Making it possible for static analysis tests to be skipped if the python version doesn't match.

This commit is contained in:
Dave Halter
2015-12-22 17:37:28 +01:00
parent 515d096d33
commit 6bad5a924b
4 changed files with 32 additions and 11 deletions

View File

@@ -89,11 +89,15 @@ class StaticAnalysisCase(object):
The tests also start with `#!`, like the goto_definition tests. The tests also start with `#!`, like the goto_definition tests.
""" """
def __init__(self, path): def __init__(self, path):
self.skip = False
self._path = path self._path = path
with open(path) as f: with open(path) as f:
self._source = f.read() 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): def collect_comparison(self):
cases = [] cases = []
for line_nr, line in enumerate(self._source.splitlines(), 1): for line_nr, line in enumerate(self._source.splitlines(), 1):

View File

@@ -112,6 +112,7 @@ Tests look like this::
import os import os
import re import re
import sys import sys
import operator
from ast import literal_eval from ast import literal_eval
from io import StringIO from io import StringIO
from functools import reduce from functools import reduce
@@ -233,6 +234,26 @@ class IntegrationTestCase(object):
return compare_cb(self, compare, sorted(wanted)) 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): def collect_file_tests(lines, lines_to_execute):
makecase = lambda t: IntegrationTestCase(t, correct, line_nr, column, makecase = lambda t: IntegrationTestCase(t, correct, line_nr, column,
start, line, path=None, skip=skip) start, line, path=None, skip=skip)
@@ -259,15 +280,7 @@ def collect_file_tests(lines, lines_to_execute):
yield makecase(TEST_DEFINITIONS) yield makecase(TEST_DEFINITIONS)
correct = None correct = None
else: else:
# check for python minimal version number skip = skip or skip_python_version(line)
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)
try: try:
r = re.search(r'(?:^|(?<=\s))#([?!<])\s*([^\n]*)', line) r = re.search(r'(?:^|(?<=\s))#([?!<])\s*([^\n]*)', line)
# test_type is ? for completion and ! for goto_assignments # test_type is ? for completion and ! for goto_assignments

View File

@@ -1,6 +1,7 @@
""" """
Some special cases of Python 2. Some special cases of Python 2.
""" """
# python <= 2.7
# print is syntax: # print is syntax:
print 1 print 1

View File

@@ -40,7 +40,10 @@ def test_completion(case, monkeypatch):
def test_static_analysis(static_analysis_case): 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): def test_refactor(refactor_case):