Skip to content

Commit eb4530a

Browse files
mpownbyMatt OwnbyMatt Ownby
authored
Save mutation stats for use with CI/CD pipeline (#460)
* save mutation stats after printing them so a CI/CD pipeline can optionally take action on the results * added Click command to export cicd mutation data from a previous invocation of 'mutmut run' --------- Co-authored-by: Matt Ownby <matt@Mac-mini-2.local> Co-authored-by: Matt Ownby <mownby@solventum.com>
1 parent d625274 commit eb4530a

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/mutmut/__main__.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,50 @@ def save_stats():
899899
stats_time=mutmut.stats_time,
900900
), f, indent=4)
901901

902+
def save_cicd_stats(source_file_mutation_data_by_path):
903+
s = calculate_summary_stats(source_file_mutation_data_by_path)
904+
with open('mutants/mutmut-cicd-stats.json', 'w') as f:
905+
json.dump(dict(
906+
killed=s.killed,
907+
survived=s.survived,
908+
total=s.total,
909+
no_tests=s.no_tests,
910+
skipped=s.skipped,
911+
suspicious=s.suspicious,
912+
timeout=s.timeout,
913+
check_was_interrupted_by_user=s.check_was_interrupted_by_user,
914+
segfault=s.segfault
915+
), f, indent=4)
916+
917+
# exports CI/CD stats to block pull requests from merging if mutation score is too low, or used in other ways in CI/CD pipelines
918+
@cli.command()
919+
def export_cicd_stats():
920+
ensure_config_loaded()
921+
922+
source_file_mutation_data_by_path: Dict[str, SourceFileMutationData] = {}
923+
924+
for path in walk_source_files():
925+
if mutmut.config.should_ignore_for_mutation(path):
926+
continue
927+
928+
meta_path = Path('mutants') / (str(path) + '.meta')
929+
if not meta_path.exists():
930+
continue
931+
932+
m = SourceFileMutationData(path=path)
933+
m.load()
934+
if not m.exit_code_by_key:
935+
continue
936+
937+
source_file_mutation_data_by_path[str(path)] = m
938+
939+
if not source_file_mutation_data_by_path:
940+
print('No previous mutation data found. Run "mutmut run" first.')
941+
return
942+
943+
save_cicd_stats(source_file_mutation_data_by_path)
944+
print('Saved CI/CD stats to mutants/mutmut-cicd-stats.json')
945+
902946

903947
def collect_source_file_mutation_data(*, mutant_names):
904948
source_file_mutation_data_by_path: Dict[str, SourceFileMutationData] = {}

0 commit comments

Comments
 (0)