|
1 | 1 | """Code for pytest-rts plugin logic""" |
2 | | -import logging |
3 | 2 | import os |
4 | 3 |
|
5 | 4 | import pytest |
6 | 5 |
|
7 | | -from pytest_rts.pytest.init_phase_plugin import InitPhasePlugin |
8 | | -from pytest_rts.pytest.run_phase_plugin import RunPhasePlugin |
9 | | -from pytest_rts.utils.common import ( |
10 | | - DB_FILE_PREFIX, |
11 | | - get_coverage_file_filename, |
12 | | - get_existing_tests, |
13 | | - get_tests_committed, |
14 | | - get_tests_current, |
15 | | -) |
16 | | -from pytest_rts.utils.git import ( |
17 | | - get_current_head_hash, |
18 | | - is_git_repo, |
19 | | - repo_has_commits, |
20 | | -) |
| 6 | +from pytest_rts.pytest.runner_plugin import RunnerPlugin |
| 7 | +from pytest_rts.utils.common import get_existing_tests |
21 | 8 |
|
22 | 9 |
|
23 | 10 | def pytest_addoption(parser): |
24 | 11 | """Register pytest flags""" |
25 | 12 | group = parser.getgroup("pytest-rts") |
26 | | - group.addoption("--rts", action="store_true", default=False, help="run rts") |
| 13 | + group.addoption("--rts", action="store_true", default=False, help="Run pytest-rts") |
27 | 14 | group.addoption( |
28 | | - "--committed", |
29 | | - action="store_true", |
30 | | - default=False, |
31 | | - help="Check committed changes", |
| 15 | + "--rts-coverage-db", |
| 16 | + action="store", |
| 17 | + default="", |
| 18 | + help="Coverage file pytest-rts", |
32 | 19 | ) |
33 | 20 |
|
34 | 21 |
|
35 | 22 | def pytest_configure(config): |
36 | 23 | """Register RTS plugins based on state""" |
37 | | - logger = logging.getLogger() |
38 | | - logging.basicConfig(format="%(message)s", level=logging.INFO) |
39 | | - |
40 | 24 | if not config.option.rts: |
41 | 25 | return |
42 | 26 |
|
43 | | - if not is_git_repo(): |
44 | | - logger.info( |
45 | | - "Not a git repository! pytest-rts is disabled. Run git init before using pytest-rts." |
46 | | - ) |
47 | | - return |
48 | | - |
49 | | - if not repo_has_commits(): |
50 | | - logger.info( |
51 | | - "No commits yet! pytest-rts is disabled. Create a git commit before using pytest-rts." |
52 | | - ) |
53 | | - return |
54 | | - |
55 | | - init_required = not os.path.isfile(get_coverage_file_filename()) |
56 | | - |
57 | | - if init_required: |
58 | | - logger.info("No mapping database detected, starting initialization...") |
59 | | - config.pluginmanager.register(InitPhasePlugin(), "rts-init-plugin") |
60 | | - return |
61 | | - |
62 | | - workdir_changes = not config.option.committed |
63 | | - existing_tests = get_existing_tests() |
64 | | - |
65 | | - if workdir_changes: |
66 | | - logger.info("Checking working directory changes.") |
67 | | - workdir_tests = get_tests_current() |
68 | | - config.pluginmanager.register( |
69 | | - RunPhasePlugin(workdir_tests, existing_tests), "rts-workdir-plugin" |
70 | | - ) |
71 | | - return |
72 | | - |
73 | | - logger.info("Checking committed changes.") |
74 | | - previous_hash = get_coverage_file_filename().split(".")[1] |
75 | | - if previous_hash == get_current_head_hash(): |
76 | | - pytest.exit(0, "Database was initialized at this commit. No changes detected.") |
77 | | - |
78 | | - committed_tests = get_tests_committed(previous_hash) |
79 | | - |
80 | | - config.pluginmanager.register( |
81 | | - RunPhasePlugin(committed_tests, existing_tests), "rts-committed-plugin" |
82 | | - ) |
83 | | - return |
| 27 | + if not config.option.rts_coverage_db: |
| 28 | + pytest.exit(2, "No coverage file provided") |
84 | 29 |
|
| 30 | + if not os.path.exists(config.option.rts_coverage_db): |
| 31 | + pytest.exit(2, "Provided coverage file does not exist") |
85 | 32 |
|
86 | | -def pytest_unconfigure(config): |
87 | | - """Cleanup after pytest run""" |
88 | | - if config.option.rts: |
89 | | - if config.pluginmanager.hasplugin("rts-init-plugin"): |
90 | | - os.rename(".coverage", f"{DB_FILE_PREFIX}.{get_current_head_hash()}") |
| 33 | + existing_tests = get_existing_tests(config.option.rts_coverage_db) |
| 34 | + config.pluginmanager.register(RunnerPlugin(existing_tests)) |
0 commit comments