Skip to content

Commit 7d40a75

Browse files
committed
feat: run new tests based on pytest-cov
run only newly added tests that pytest-cov did not pick up when coverage file was made
1 parent af328ba commit 7d40a75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+89
-1899
lines changed

pytest_rts/plugin.py

Lines changed: 13 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,34 @@
11
"""Code for pytest-rts plugin logic"""
2-
import logging
32
import os
43

54
import pytest
65

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
218

229

2310
def pytest_addoption(parser):
2411
"""Register pytest flags"""
2512
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")
2714
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",
3219
)
3320

3421

3522
def pytest_configure(config):
3623
"""Register RTS plugins based on state"""
37-
logger = logging.getLogger()
38-
logging.basicConfig(format="%(message)s", level=logging.INFO)
39-
4024
if not config.option.rts:
4125
return
4226

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")
8429

30+
if not os.path.exists(config.option.rts_coverage_db):
31+
pytest.exit(2, "Provided coverage file does not exist")
8532

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))

pytest_rts/pytest/init_phase_plugin.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

pytest_rts/pytest/run_phase_plugin.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

pytest_rts/pytest/runner_plugin.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""This module contains code for running a specific test set"""
2+
# pylint: disable=too-few-public-methods
3+
from pytest_rts.pytest.fake_item import FakeItem
4+
from pytest_rts.utils.common import filter_pytest_items
5+
6+
7+
class RunnerPlugin:
8+
"""Plugin class for pytest"""
9+
10+
def __init__(self, existing_tests):
11+
"""Set existing tests"""
12+
self.existing_tests = existing_tests
13+
14+
def pytest_collection_modifyitems(
15+
self, session, config, items
16+
): # pylint: disable=unused-argument
17+
"""Select only specific tests for running"""
18+
original_length = len(items)
19+
items[:] = filter_pytest_items(items, self.existing_tests)
20+
session.config.hook.pytest_deselected(
21+
items=([FakeItem(session.config)] * (original_length - len(items)))
22+
)

pytest_rts/tests/conftest.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

pytest_rts/tests/evaluation/capture_specific_plugin.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)