|
1 | 1 | """Code for pytest-rts plugin logic""" |
2 | | -import logging |
3 | 2 | import os |
4 | | -import sqlite3 |
5 | 3 |
|
6 | 4 | import pytest |
| 5 | +from _pytest.config import Config |
| 6 | +from _pytest.config.argparsing import Parser |
7 | 7 |
|
8 | | -from pytest_rts.pytest.init_phase_plugin import InitPhasePlugin |
9 | | -from pytest_rts.pytest.normal_phase_plugin import NormalPhasePlugin |
10 | | -from pytest_rts.pytest.update_phase_plugin import UpdatePhasePlugin |
11 | | -from pytest_rts.utils.git import ( |
12 | | - get_current_head_hash, |
13 | | - is_git_repo, |
14 | | - repo_has_commits, |
15 | | -) |
16 | | -from pytest_rts.utils.selection import ( |
17 | | - get_tests_and_data_committed, |
18 | | - get_tests_and_data_current, |
19 | | -) |
20 | | -from pytest_rts.utils.mappinghelper import MappingHelper |
21 | | -from pytest_rts.utils.testgetter import TestGetter |
| 8 | +from pytest_rts.pytest.runner_plugin import RunnerPlugin |
| 9 | +from pytest_rts.utils.common import get_existing_tests |
22 | 10 |
|
23 | | -DB_FILE_NAME = "mapping.db" |
24 | 11 |
|
25 | | - |
26 | | -class MappingConn: # pylint: disable=too-few-public-methods |
27 | | - """Mapping connection""" |
28 | | - |
29 | | - _conn = None |
30 | | - |
31 | | - @classmethod |
32 | | - def conn(cls): |
33 | | - """SQLite connection""" |
34 | | - if not cls._conn: |
35 | | - cls._conn = sqlite3.connect(DB_FILE_NAME) |
36 | | - return cls._conn |
37 | | - |
38 | | - |
39 | | -def pytest_addoption(parser): |
| 12 | +def pytest_addoption(parser: Parser) -> None: |
40 | 13 | """Register pytest flags""" |
41 | | - parser.addoption("--rts", action="store_true", default=False, help="run rts") |
| 14 | + group = parser.getgroup("pytest-rts") |
| 15 | + group.addoption("--rts", action="store_true", default=False, help="Run pytest-rts") |
| 16 | + group.addoption( |
| 17 | + "--rts-coverage-db", |
| 18 | + action="store", |
| 19 | + default="", |
| 20 | + help="Coverage file pytest-rts", |
| 21 | + ) |
42 | 22 |
|
43 | 23 |
|
44 | | -def pytest_configure(config): |
| 24 | +def pytest_configure(config: Config) -> None: |
45 | 25 | """Register RTS plugins based on state""" |
46 | | - logger = logging.getLogger() |
47 | | - logging.basicConfig(format="%(message)s", level=logging.INFO) |
48 | | - |
49 | 26 | if not config.option.rts: |
50 | 27 | return |
51 | 28 |
|
52 | | - if not is_git_repo(): |
53 | | - logger.info( |
54 | | - "Not a git repository! pytest-rts is disabled. Run git init before using pytest-rts." |
55 | | - ) |
56 | | - return |
57 | | - |
58 | | - if not repo_has_commits(): |
59 | | - logger.info( |
60 | | - "No commits yet! pytest-rts is disabled. Create a git commit before using pytest-rts." |
61 | | - ) |
62 | | - return |
63 | | - |
64 | | - init_required = not os.path.isfile(DB_FILE_NAME) |
65 | | - |
66 | | - mapping_helper = MappingHelper(MappingConn.conn()) |
67 | | - test_getter = TestGetter(MappingConn.conn()) |
68 | | - |
69 | | - if init_required: |
70 | | - logger.info("No mapping database detected, starting initialization...") |
71 | | - config.pluginmanager.register( |
72 | | - InitPhasePlugin(mapping_helper), "rts-init-plugin" |
73 | | - ) |
74 | | - return |
75 | | - |
76 | | - workdir_data = get_tests_and_data_current(mapping_helper, test_getter) |
77 | | - |
78 | | - logger.info("WORKING DIRECTORY CHANGES") |
79 | | - logger.info("Found %s changed test files", workdir_data.changed_testfiles_amount) |
80 | | - logger.info("Found %s changed src files", workdir_data.changed_srcfiles_amount) |
81 | | - logger.info("Found %s tests to execute\n", len(workdir_data.test_set)) |
82 | | - |
83 | | - if workdir_data.test_set: |
84 | | - logger.info( |
85 | | - "Running WORKING DIRECTORY test set and exiting without updating..." |
86 | | - ) |
87 | | - config.pluginmanager.register( |
88 | | - NormalPhasePlugin(workdir_data.test_set, test_getter) |
89 | | - ) |
90 | | - return |
91 | | - |
92 | | - logger.info("No WORKING DIRECTORY tests to run, checking COMMITTED changes...") |
93 | | - |
94 | | - current_hash = get_current_head_hash() |
95 | | - previous_hash = mapping_helper.last_update_hash |
96 | | - if current_hash == previous_hash: |
97 | | - pytest.exit("Database is updated to the current commit state", 0) |
98 | | - |
99 | | - logger.info("Comparison: %s\n", " => ".join([current_hash, previous_hash])) |
100 | | - |
101 | | - committed_data = get_tests_and_data_committed(mapping_helper, test_getter) |
102 | | - |
103 | | - logger.info("COMMITTED CHANGES") |
104 | | - logger.info("Found %s changed test files", committed_data.changed_testfiles_amount) |
105 | | - logger.info("Found %s changed src files", committed_data.changed_srcfiles_amount) |
106 | | - logger.info( |
107 | | - "Found %s newly added tests", |
108 | | - committed_data.new_tests_amount, |
109 | | - ) |
110 | | - logger.info("Found %s tests to execute\n", len(committed_data.test_set)) |
111 | | - |
112 | | - if committed_data.warning_needed: |
113 | | - logger.info( |
114 | | - "WARNING: New lines were added to the following files but no new tests discovered:" |
115 | | - ) |
116 | | - logger.info("\n".join(committed_data.files_to_warn)) |
117 | | - |
118 | | - logger.info("=> Executing tests (if any) and updating database") |
119 | | - mapping_helper.set_last_update_hash(current_hash) |
120 | | - |
121 | | - mapping_helper.update_mapping(committed_data.update_data) |
122 | | - |
123 | | - if committed_data.test_set: |
124 | | - config.pluginmanager.register( |
125 | | - UpdatePhasePlugin(committed_data.test_set, mapping_helper, test_getter) |
126 | | - ) |
127 | | - return |
128 | | - |
129 | | - pytest.exit("No tests to run", 0) |
| 29 | + if not config.option.rts_coverage_db: |
| 30 | + pytest.exit("No coverage file provided", 2) |
130 | 31 |
|
| 32 | + if not os.path.exists(config.option.rts_coverage_db): |
| 33 | + pytest.exit("Provided coverage file does not exist", 2) |
131 | 34 |
|
132 | | -def pytest_unconfigure(config): |
133 | | - """Cleanup after pytest run""" |
134 | | - if config.option.rts: |
135 | | - MappingConn.conn().commit() |
136 | | - MappingConn.conn().close() |
| 35 | + existing_tests = get_existing_tests(config.option.rts_coverage_db) |
| 36 | + config.pluginmanager.register(RunnerPlugin(existing_tests)) |
0 commit comments