-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
45 changed files
with
89 additions
and
1,899 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,34 @@ | ||
"""Code for pytest-rts plugin logic""" | ||
import logging | ||
import os | ||
|
||
import pytest | ||
|
||
from pytest_rts.pytest.init_phase_plugin import InitPhasePlugin | ||
from pytest_rts.pytest.run_phase_plugin import RunPhasePlugin | ||
from pytest_rts.utils.common import ( | ||
DB_FILE_PREFIX, | ||
get_coverage_file_filename, | ||
get_existing_tests, | ||
get_tests_committed, | ||
get_tests_current, | ||
) | ||
from pytest_rts.utils.git import ( | ||
get_current_head_hash, | ||
is_git_repo, | ||
repo_has_commits, | ||
) | ||
from pytest_rts.pytest.runner_plugin import RunnerPlugin | ||
from pytest_rts.utils.common import get_existing_tests | ||
|
||
|
||
def pytest_addoption(parser): | ||
"""Register pytest flags""" | ||
group = parser.getgroup("pytest-rts") | ||
group.addoption("--rts", action="store_true", default=False, help="run rts") | ||
group.addoption("--rts", action="store_true", default=False, help="Run pytest-rts") | ||
group.addoption( | ||
"--committed", | ||
action="store_true", | ||
default=False, | ||
help="Check committed changes", | ||
"--rts-coverage-db", | ||
action="store", | ||
default="", | ||
help="Coverage file pytest-rts", | ||
) | ||
|
||
|
||
def pytest_configure(config): | ||
"""Register RTS plugins based on state""" | ||
logger = logging.getLogger() | ||
logging.basicConfig(format="%(message)s", level=logging.INFO) | ||
|
||
if not config.option.rts: | ||
return | ||
|
||
if not is_git_repo(): | ||
logger.info( | ||
"Not a git repository! pytest-rts is disabled. Run git init before using pytest-rts." | ||
) | ||
return | ||
|
||
if not repo_has_commits(): | ||
logger.info( | ||
"No commits yet! pytest-rts is disabled. Create a git commit before using pytest-rts." | ||
) | ||
return | ||
|
||
init_required = not os.path.isfile(get_coverage_file_filename()) | ||
|
||
if init_required: | ||
logger.info("No mapping database detected, starting initialization...") | ||
config.pluginmanager.register(InitPhasePlugin(), "rts-init-plugin") | ||
return | ||
|
||
workdir_changes = not config.option.committed | ||
existing_tests = get_existing_tests() | ||
|
||
if workdir_changes: | ||
logger.info("Checking working directory changes.") | ||
workdir_tests = get_tests_current() | ||
config.pluginmanager.register( | ||
RunPhasePlugin(workdir_tests, existing_tests), "rts-workdir-plugin" | ||
) | ||
return | ||
|
||
logger.info("Checking committed changes.") | ||
previous_hash = get_coverage_file_filename().split(".")[1] | ||
if previous_hash == get_current_head_hash(): | ||
pytest.exit(0, "Database was initialized at this commit. No changes detected.") | ||
|
||
committed_tests = get_tests_committed(previous_hash) | ||
|
||
config.pluginmanager.register( | ||
RunPhasePlugin(committed_tests, existing_tests), "rts-committed-plugin" | ||
) | ||
return | ||
if not config.option.rts_coverage_db: | ||
pytest.exit(2, "No coverage file provided") | ||
|
||
if not os.path.exists(config.option.rts_coverage_db): | ||
pytest.exit(2, "Provided coverage file does not exist") | ||
|
||
def pytest_unconfigure(config): | ||
"""Cleanup after pytest run""" | ||
if config.option.rts: | ||
if config.pluginmanager.hasplugin("rts-init-plugin"): | ||
os.rename(".coverage", f"{DB_FILE_PREFIX}.{get_current_head_hash()}") | ||
existing_tests = get_existing_tests(config.option.rts_coverage_db) | ||
config.pluginmanager.register(RunnerPlugin(existing_tests)) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""This module contains code for running a specific test set""" | ||
# pylint: disable=too-few-public-methods | ||
from pytest_rts.pytest.fake_item import FakeItem | ||
from pytest_rts.utils.common import filter_pytest_items | ||
|
||
|
||
class RunnerPlugin: | ||
"""Plugin class for pytest""" | ||
|
||
def __init__(self, existing_tests): | ||
"""Set existing tests""" | ||
self.existing_tests = existing_tests | ||
|
||
def pytest_collection_modifyitems( | ||
self, session, config, items | ||
): # pylint: disable=unused-argument | ||
"""Select only specific tests for running""" | ||
original_length = len(items) | ||
items[:] = filter_pytest_items(items, self.existing_tests) | ||
session.config.hook.pytest_deselected( | ||
items=([FakeItem(session.config)] * (original_length - len(items))) | ||
) |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.