|
| 1 | +""" |
| 2 | +Custom lint tests. |
| 3 | +""" |
| 4 | + |
| 5 | +import subprocess |
| 6 | +from pathlib import Path |
| 7 | +from typing import Dict, Set |
| 8 | + |
| 9 | +import pytest |
| 10 | +import yaml |
| 11 | + |
| 12 | + |
| 13 | +def _travis_ci_patterns() -> Set[str]: |
| 14 | + """ |
| 15 | + Return the CI patterns given in the ``.travis.yml`` file. |
| 16 | + """ |
| 17 | + travis_file = Path(__file__).parent.parent / '.travis.yml' |
| 18 | + travis_contents = travis_file.read_text() |
| 19 | + travis_dict = yaml.load(travis_contents) |
| 20 | + travis_matrix = travis_dict['env']['matrix'] |
| 21 | + |
| 22 | + ci_patterns = set() # type: Set[str] |
| 23 | + for matrix_item in travis_matrix: |
| 24 | + key, value = matrix_item.split('=') |
| 25 | + assert key == 'CI_PATTERN' |
| 26 | + assert value not in ci_patterns |
| 27 | + # Special case for running no tests. |
| 28 | + if value != "''": |
| 29 | + ci_patterns.add(value) |
| 30 | + |
| 31 | + return ci_patterns |
| 32 | + |
| 33 | + |
| 34 | +def _tests_from_pattern(ci_pattern: str) -> Set[str]: |
| 35 | + """ |
| 36 | + From a CI pattern, get all tests ``pytest`` would collect. |
| 37 | + """ |
| 38 | + tests = set([]) # type: Set[str] |
| 39 | + args = ['pytest', '--collect-only', ci_pattern, '-q'] |
| 40 | + result = subprocess.run(args=args, stdout=subprocess.PIPE) |
| 41 | + output = result.stdout |
| 42 | + for line in output.splitlines(): |
| 43 | + if line and not line.startswith(b'no tests ran in'): |
| 44 | + tests.add(line.decode()) |
| 45 | + |
| 46 | + return tests |
| 47 | + |
| 48 | + |
| 49 | +def test_ci_patterns_valid() -> None: |
| 50 | + """ |
| 51 | + All of the CI patterns in ``.travis.yml`` match at least one test in the |
| 52 | + test suite. |
| 53 | + """ |
| 54 | + ci_patterns = _travis_ci_patterns() |
| 55 | + |
| 56 | + for ci_pattern in ci_patterns: |
| 57 | + pattern = 'tests/mock_vws/' + ci_pattern |
| 58 | + collect_only_result = pytest.main(['--collect-only', pattern]) |
| 59 | + |
| 60 | + message = '"{ci_pattern}" does not match any tests.'.format( |
| 61 | + ci_pattern=ci_pattern, |
| 62 | + ) |
| 63 | + assert collect_only_result == 0, message |
| 64 | + |
| 65 | + |
| 66 | +def test_tests_collected_once() -> None: |
| 67 | + """ |
| 68 | + Each test in the test suite is collected exactly once. |
| 69 | +
|
| 70 | + This does not necessarily mean that they are run - they may be skipped. |
| 71 | + """ |
| 72 | + ci_patterns = _travis_ci_patterns() |
| 73 | + tests_to_patterns: Dict[str, Set[str]] = {} |
| 74 | + for pattern in ci_patterns: |
| 75 | + pattern = 'tests/mock_vws/' + pattern |
| 76 | + tests = _tests_from_pattern(ci_pattern=pattern) |
| 77 | + for test in tests: |
| 78 | + if test in tests_to_patterns: |
| 79 | + tests_to_patterns[test].add(pattern) |
| 80 | + else: |
| 81 | + tests_to_patterns[test] = set([pattern]) |
| 82 | + |
| 83 | + for test_name, patterns in tests_to_patterns.items(): |
| 84 | + message = ( |
| 85 | + 'Test "{test_name}" will be run once for each pattern in ' |
| 86 | + '{patterns}. ' |
| 87 | + 'Each test should be run only once.' |
| 88 | + ).format( |
| 89 | + test_name=test_name, |
| 90 | + patterns=patterns, |
| 91 | + ) |
| 92 | + assert len(patterns) == 1, message |
| 93 | + |
| 94 | + all_tests = _tests_from_pattern(ci_pattern='tests/') |
| 95 | + assert tests_to_patterns.keys() - all_tests == set() |
| 96 | + assert all_tests - tests_to_patterns.keys() == set() |
| 97 | + |
| 98 | + |
| 99 | +def test_init_files() -> None: |
| 100 | + """ |
| 101 | + ``__init__`` files exist where they should do. |
| 102 | +
|
| 103 | + If ``__init__`` files are missing, linters may not run on all files that |
| 104 | + they should run on. |
| 105 | + """ |
| 106 | + directories = (Path('src'), Path('tests')) |
| 107 | + |
| 108 | + for directory in directories: |
| 109 | + files = directory.glob('**/*.py') |
| 110 | + for python_file in files: |
| 111 | + parent = python_file.parent |
| 112 | + expected_init = parent / '__init__.py' |
| 113 | + assert expected_init.exists() |
0 commit comments