22import csv
33import logging
44import os
5+ from typing import Dict , List , Set
56
67from custom_json_diff .lib .custom_diff import compare_dicts , perform_bom_diff , report_results
78from custom_json_diff .lib .custom_diff_classes import Options
8- from custom_json_diff .lib .utils import json_dump , json_load
9+ from custom_json_diff .lib .utils import json_dump ,json_load
10+
11+ from generate import filter_repos
912
1013logging .disable (logging .INFO )
1114
@@ -15,7 +18,7 @@ def build_args():
1518 parser .add_argument (
1619 '--directories' ,
1720 '-d' ,
18- default = ['/home/runner/work/ original_snapshots' , '/home/runner/work /new_snapshots' ],
21+ default = [f'/ { os . getenv ( "GITHUB_WORKSPACE" ) } / original_snapshots' , f'/ { os . getenv ( "GITHUB_WORKSPACE" ) } /new_snapshots' ],
1922 help = 'Directories containing the snapshots to compare' ,
2023 nargs = 2
2124 )
@@ -25,16 +28,36 @@ def build_args():
2528 action = "store_true" ,
2629 help = "Migrate legacy snapshots to 1.6 format"
2730 )
31+ parser .add_argument (
32+ '--projects' ,
33+ '-p' ,
34+ help = 'Filter to these projects.' ,
35+ dest = 'projects' ,
36+ )
37+ parser .add_argument (
38+ '--types' ,
39+ '-t' ,
40+ help = 'Filter to these project types.' ,
41+ dest = 'project_types' ,
42+ )
43+ parser .add_argument (
44+ '--skip-projects' ,
45+ '-s' ,
46+ help = 'Skip these projects'
47+ )
2848 return parser .parse_args ()
2949
3050
31- def compare_snapshot (dir1 , dir2 , options , repo , migrate_legacy ):
51+ def compare_snapshot (dir1 : str , dir2 : str , options : Options , repo : Dict , migrate_legacy : bool ):
3252 bom_1 = f"{ dir1 } /{ repo ['project' ]} -bom.json"
3353 bom_2 = f"{ dir2 } /{ repo ['project' ]} -bom.json"
3454 if migrate_legacy :
3555 bom_data = migrate_to_1_6 (bom_1 )
3656 bom_1 = bom_1 .replace ("bom.json" , "bom.migrated.json" )
3757 json_dump (bom_1 , bom_data )
58+ bom_data = migrate_to_1_6 (bom_1 )
59+ bom_2 = bom_2 .replace ("bom.json" , "bom.migrated.json" )
60+ json_dump (bom_2 , bom_data )
3861 options .file_1 = bom_1
3962 options .file_2 = bom_2
4063 options .output = f'{ dir2 } /{ repo ["project" ]} -diff.json'
@@ -45,12 +68,11 @@ def compare_snapshot(dir1, dir2, options, repo, migrate_legacy):
4568 status , result_summary = perform_bom_diff (j1 , j2 )
4669 report_results (status , result_summary , options , j1 , j2 )
4770 return status , f"{ repo ['project' ]} failed." , result_summary
48- return status , None , None
49-
71+ return status , f"{ repo ['project' ]} succeeded." , {}
5072
51- def perform_snapshot_tests (dir1 , dir2 , migrate_legacy ):
52- repo_data = read_csv ()
5373
74+ def perform_snapshot_tests (dir1 : str , dir2 : str , projects : List , project_types : Set , migrate_legacy : bool , skipped_projects ):
75+ repo_data = read_csv (projects , project_types , skipped_projects )
5476 options = Options (
5577 allow_new_versions = True ,
5678 allow_new_data = True ,
@@ -62,11 +84,9 @@ def perform_snapshot_tests(dir1, dir2, migrate_legacy):
6284 failed_diffs = {}
6385 for repo in repo_data :
6486 status , result , summary = compare_snapshot (dir1 , dir2 , options , repo , migrate_legacy )
65- if result :
66- print (result )
87+ print (result )
6788 if status :
6889 failed_diffs [repo ["project" ]] = summary
69-
7090 if failed_diffs :
7191 diff_file = os .path .join (dir2 , 'diffs.json' )
7292 print ("Snapshot tests failed." )
@@ -94,14 +114,21 @@ def migrate_to_1_6(bom_file):
94114 return bom_data
95115
96116
97- def read_csv ():
117+ def read_csv (projects , project_types , skipped_projects ):
98118 csv_file = os .path .join (os .path .dirname (os .path .realpath (__file__ )), "repos.csv" )
99119 with open (csv_file , 'r' , encoding = 'utf-8' ) as f :
100120 reader = csv .DictReader (f )
101121 repo_data = list (reader )
102- return repo_data
122+ return filter_repos ( repo_data , projects , project_types , skipped_projects )
103123
104124
105125if __name__ == '__main__' :
106126 args = build_args ()
107- perform_snapshot_tests (args .directories [0 ], args .directories [1 ], args .migrate_legacy )
127+ if args .project_types :
128+ if ',' in args .project_types :
129+ project_types = set (args .project_types .split (',' ))
130+ else :
131+ project_types = {args .project_types }
132+ else :
133+ project_types = set ()
134+ perform_snapshot_tests (args .directories [0 ], args .directories [1 ], args .projects , project_types , args .migrate_legacy , args .skip_projects )
0 commit comments