1+ import pytest
2+ import difflib
3+ import recategorize
4+ import jsonschema
5+ from pathlib import Path
6+ import argparse
7+ import sys
8+
9+ TEST_DATA_DIR = Path (__file__ ).resolve ().parent / 'test-data'
10+
11+ class TestsInputs :
12+ def test_invalid_codeql_config (self ):
13+ with pytest .raises (SystemExit ):
14+ recategorize .main (argparse .Namespace (
15+ coding_standards_schema_file = Path .cwd (),
16+ sarif_schema_file = Path .cwd (),
17+ coding_standards_config_file = TEST_DATA_DIR / 'invalid-coding-standards-config.yml'
18+ ))
19+
20+ def test_valid_codeql_config (self ):
21+ with (TEST_DATA_DIR / 'valid-sarif.json' ).open (mode = 'r' ) as sarif_in :
22+ recategorize .main (argparse .Namespace (
23+ coding_standards_schema_file = Path .cwd (),
24+ sarif_schema_file = Path .cwd (),
25+ coding_standards_config_file = TEST_DATA_DIR / 'valid-coding-standards-config.yml' ,
26+ sarif_in = sarif_in ,
27+ sarif_out = sys .stdout ,
28+ dump_json_patch = None
29+ ))
30+
31+ def test_invalid_sarif_file (self ):
32+ with pytest .raises (SystemExit ):
33+ with (TEST_DATA_DIR / 'invalid-sarif.json' ).open (mode = 'r' ) as sarif_in :
34+ recategorize .main (argparse .Namespace (
35+ coding_standards_schema_file = Path .cwd (),
36+ sarif_schema_file = Path .cwd (),
37+ coding_standards_config_file = TEST_DATA_DIR / 'valid-coding-standards-config.yml' ,
38+ sarif_in = sarif_in
39+ ))
40+
41+ def test_valid_sarif_file (self ):
42+ with (TEST_DATA_DIR / 'valid-sarif.json' ).open (mode = 'r' ) as sarif_in :
43+ recategorize .main (argparse .Namespace (
44+ coding_standards_schema_file = Path .cwd (),
45+ sarif_schema_file = Path .cwd (),
46+ coding_standards_config_file = TEST_DATA_DIR / 'valid-coding-standards-config.yml' ,
47+ sarif_in = sarif_in ,
48+ sarif_out = sys .stdout ,
49+ dump_json_patch = None
50+ ))
51+
52+ def test_invalid_yaml (self ):
53+ with pytest .raises (SystemExit ):
54+ recategorize .main (argparse .Namespace (
55+ coding_standards_schema_file = Path .cwd (),
56+ sarif_schema_file = Path .cwd (),
57+ coding_standards_config_file = TEST_DATA_DIR / 'invalid-yaml.yml'
58+ ))
59+
60+ def test_invalid_json_for_schema (self ):
61+ with pytest .raises (SystemExit ):
62+ recategorize .main (argparse .Namespace (
63+ coding_standards_schema_file = TEST_DATA_DIR / 'invalid-json.json'
64+ ))
65+
66+ class TestUnsupportedSchemas :
67+ def test_unsupported_sarif_schema (self ):
68+ with pytest .raises (SystemExit ):
69+ recategorize .main (argparse .Namespace (
70+ coding_standards_schema_file = Path .cwd (),
71+ sarif_schema_file = TEST_DATA_DIR / 'unsupported-sarif-schema-2.0.0.json' ,
72+ coding_standards_config_file = Path .cwd ()
73+ ))
74+ def test_unsupported_coding_standards_config_schema (self ):
75+ with pytest .raises (SystemExit ):
76+ recategorize .main (argparse .Namespace (
77+ coding_standards_schema_file = Path .cwd (),
78+ sarif_schema_file = TEST_DATA_DIR / 'unsupported-coding-standards-schema-0.0.1.json' ,
79+ coding_standards_config_file = Path .cwd ()
80+ ))
81+
82+ class TestRecategorization :
83+ def test_recategorization (self , tmp_path ):
84+ with (TEST_DATA_DIR / 'valid-sarif.json' ).open (mode = 'r' ) as sarif_in :
85+ with (tmp_path / 'sarif.json' ).open (mode = 'w' ) as sarif_out :
86+ recategorize .main (argparse .Namespace (
87+ coding_standards_schema_file = Path .cwd (),
88+ sarif_schema_file = Path .cwd (),
89+ coding_standards_config_file = TEST_DATA_DIR / 'valid-coding-standards-config.yml' ,
90+ sarif_in = sarif_in ,
91+ sarif_out = sarif_out ,
92+ dump_json_patch = tmp_path / 'json-patch.json'
93+ ))
94+
95+ expected_patch = (TEST_DATA_DIR / 'json-patch.expected' ).read_text ()
96+ actual_patch = (tmp_path / 'json-patch.json' ).read_text ()
97+ assert (expected_patch == actual_patch )
98+
99+ expected_sarif = (TEST_DATA_DIR / 'valid-sarif-recategorized.expected' ).read_text ()
100+ actual_sarif = (tmp_path / 'sarif.json' ).read_text ()
101+ assert (expected_sarif == actual_sarif )
0 commit comments