Skip to content

Commit 2a8300b

Browse files
Merge pull request #93 from F-Secure/simplify-rts
feat: execution of newly added tests
2 parents be1c42e + e41128f commit 2a8300b

Some content is hidden

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

49 files changed

+181
-2635
lines changed

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@ More detailed usage is described in the [tutorial][tutorial]
2626
- install `pytest-rts`
2727
3. Switch to directory with target project
2828
4. Install all the dependencies needed for testing (should be installed into the same pytest-rts virtual environment)
29-
5. Execute `pytest --rts` which will run the entire test suite and build a mapping database
29+
5. Execute `pytest --cov=<path to code> --cov-context=test` which will run the entire test suite and build a mapping database with [pytest-cov](https://github.com/pytest-dev/pytest-cov)
30+
6. Rename the coverage file produced by `pytest-cov` to your liking. Example: `mv .coverage pytest-rts-coverage`
3031

31-
#### Running tests related to the changes
32+
#### Running new tests
3233

33-
1. execute `pytest --rts` after doing changes
34-
35-
#### Running evaluation code
36-
37-
1. execute `pytest_rts_eval` in target project directory
34+
1. execute `pytest --rts --rts-coverage-db=<your coverage file>` after adding new tests
3835

3936
## <a name="dev"></a> Development
4037

pytest.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[pytest]
2-
# Prevent helper project tests being collected as tests for pytest-rts
2+
pytester_example_dir = ./pytest_rts/tests/helper_project
3+
addopts = -p pytester
34
norecursedirs = pytest_rts/tests/helper_project

pytest_rts/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""pytest-rts: avoid already imported warning: PYTEST_DONT_REWRITE"""
2-
__version__ = "1.2.1"
2+
__version__ = "2.0.0"

pytest_rts/collect.py

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

pytest_rts/plugin.py

Lines changed: 20 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,36 @@
11
"""Code for pytest-rts plugin logic"""
2-
import logging
32
import os
4-
import sqlite3
53

64
import pytest
5+
from _pytest.config import Config
6+
from _pytest.config.argparsing import Parser
77

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
2210

23-
DB_FILE_NAME = "mapping.db"
2411

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:
4013
"""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+
)
4222

4323

44-
def pytest_configure(config):
24+
def pytest_configure(config: Config) -> None:
4525
"""Register RTS plugins based on state"""
46-
logger = logging.getLogger()
47-
logging.basicConfig(format="%(message)s", level=logging.INFO)
48-
4926
if not config.option.rts:
5027
return
5128

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

32+
if not os.path.exists(config.option.rts_coverage_db):
33+
pytest.exit("Provided coverage file does not exist", 2)
13134

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

pytest_rts/pytest/collect_plugin.py

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

pytest_rts/pytest/fake_item.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""This module contains a fake pytest item class"""
2+
from _pytest.config import Config
23

34

45
class FakeItem: # pylint: disable=too-few-public-methods
56
"""Fake class"""
67

7-
def __init__(self, config):
8+
def __init__(self, config: Config) -> None:
89
self.config = config

pytest_rts/pytest/init_phase_plugin.py

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

pytest_rts/pytest/mapper_plugin.py

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

pytest_rts/pytest/normal_phase_plugin.py

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

0 commit comments

Comments
 (0)