Skip to content

Commit

Permalink
feat: run new tests based on pytest-cov
Browse files Browse the repository at this point in the history
run only newly added tests that pytest-cov did not pick up when coverage file was made
  • Loading branch information
guotin committed Mar 17, 2021
1 parent af328ba commit 7d40a75
Show file tree
Hide file tree
Showing 45 changed files with 89 additions and 1,899 deletions.
82 changes: 13 additions & 69 deletions pytest_rts/plugin.py
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))
19 changes: 0 additions & 19 deletions pytest_rts/pytest/init_phase_plugin.py

This file was deleted.

29 changes: 0 additions & 29 deletions pytest_rts/pytest/run_phase_plugin.py

This file was deleted.

22 changes: 22 additions & 0 deletions pytest_rts/pytest/runner_plugin.py
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.
45 changes: 0 additions & 45 deletions pytest_rts/tests/conftest.py

This file was deleted.

18 changes: 0 additions & 18 deletions pytest_rts/tests/evaluation/capture_specific_plugin.py

This file was deleted.

Loading

0 comments on commit 7d40a75

Please sign in to comment.