11"""Code for pytest-rts plugin logic"""
22import logging
33import os
4- import sqlite3
54
65import pytest
76
87from 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
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+ )
1116from pytest_rts .utils .git import (
1217 get_current_head_hash ,
1318 is_git_repo ,
1419 repo_has_commits ,
1520)
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
22-
23- DB_FILE_NAME = "mapping.db"
24-
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
3721
3822
3923def pytest_addoption (parser ):
4024 """Register pytest flags"""
41- parser .addoption ("--rts" , action = "store_true" , default = False , help = "run rts" )
25+ group = parser .getgroup ("pytest-rts" )
26+ group .addoption ("--rts" , action = "store_true" , default = False , help = "run rts" )
27+ group .addoption (
28+ "--committed" ,
29+ action = "store_true" ,
30+ default = False ,
31+ help = "Check committed changes" ,
32+ )
4233
4334
4435def pytest_configure (config ):
@@ -61,76 +52,39 @@ def pytest_configure(config):
6152 )
6253 return
6354
64- init_required = not os .path .isfile (DB_FILE_NAME )
65-
66- mapping_helper = MappingHelper (MappingConn .conn ())
67- test_getter = TestGetter (MappingConn .conn ())
55+ init_required = not os .path .isfile (get_coverage_file_filename ())
6856
6957 if init_required :
7058 logger .info ("No mapping database detected, starting initialization..." )
71- config .pluginmanager .register (
72- InitPhasePlugin (mapping_helper ), "rts-init-plugin"
73- )
59+ config .pluginmanager .register (InitPhasePlugin (), "rts-init-plugin" )
7460 return
7561
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 ))
62+ workdir_changes = not config .option .committed
63+ existing_tests = get_existing_tests ()
8264
83- if workdir_data .test_set :
84- logger .info (
85- "Running WORKING DIRECTORY test set and exiting without updating..."
86- )
65+ if workdir_changes :
66+ logger .info ("Checking working directory changes." )
67+ workdir_tests = get_tests_current ()
8768 config .pluginmanager .register (
88- NormalPhasePlugin ( workdir_data . test_set , test_getter )
69+ RunPhasePlugin ( workdir_tests , existing_tests ), "rts-workdir-plugin"
8970 )
9071 return
9172
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 )
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." )
9877
99- logger . info ( "Comparison: %s \n " , " => " . join ([ current_hash , previous_hash ]) )
78+ committed_tests = get_tests_committed ( previous_hash )
10079
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 ,
80+ config .pluginmanager .register (
81+ RunPhasePlugin (committed_tests , existing_tests ), "rts-committed-plugin"
10982 )
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 )
83+ return
13084
13185
13286def pytest_unconfigure (config ):
13387 """Cleanup after pytest run"""
13488 if config .option .rts :
135- MappingConn . conn (). commit ()
136- MappingConn . conn (). close ( )
89+ if config . pluginmanager . hasplugin ( "rts-init-plugin" ):
90+ os . rename ( ".coverage" , f" { DB_FILE_PREFIX } . { get_current_head_hash () } " )
0 commit comments