1+ import mock
12from libcst ._exceptions import ParserSyntaxError
23
34from codemodder .codemods .libcst_transformer import (
78from codemodder .context import CodemodExecutionContext
89from codemodder .file_context import FileContext
910from core_codemods .defectdojo .results import DefectDojoResult
11+ from core_codemods .sonar .results import SonarResult
12+
13+ FILE_PATH = mock .MagicMock ()
14+ DEFECTDOJO_RESULTS = [
15+ DefectDojoResult .from_result (
16+ {
17+ "id" : 1 ,
18+ "title" : "python.django.security.audit.secure-cookies.django-secure-set-cookie" ,
19+ "file_path" : FILE_PATH ,
20+ "line" : 2 ,
21+ },
22+ )
23+ ]
24+
25+ SONAR_RESULTS = [
26+ SonarResult .from_result (
27+ {
28+ "rule" : "python:S1716" ,
29+ "textRange" : {
30+ "startLine" : 1 ,
31+ "endLine" : 1 ,
32+ "startOffset" : 13 ,
33+ "endOffset" : 14 ,
34+ },
35+ "component" : FILE_PATH ,
36+ "flows" : [
37+ {
38+ "locations" : [
39+ {
40+ "component" : FILE_PATH ,
41+ "textRange" : {
42+ "startLine" : 1 ,
43+ "endLine" : 1 ,
44+ "startOffset" : 8 ,
45+ "endOffset" : 9 ,
46+ },
47+ }
48+ ]
49+ }
50+ ],
51+ "status" : "OPEN" ,
52+ }
53+ )
54+ ]
1055
1156
1257def _apply_and_assert (mocker , transformer ):
13- file_context = FileContext ("home" , mocker . MagicMock () )
58+ file_context = FileContext ("home" , FILE_PATH )
1459 execution_context = CodemodExecutionContext (
1560 directory = mocker .MagicMock (),
1661 dry_run = True ,
@@ -30,21 +75,11 @@ def _apply_and_assert(mocker, transformer):
3075 assert file_context .unfixed_findings == []
3176
3277
33- def _apply_and_assert_with_tool (mocker , transformer , reason ):
34- file_path = mocker .MagicMock ()
78+ def _apply_and_assert_with_tool (mocker , transformer , reason , results ):
3579 file_context = FileContext (
3680 "home" ,
37- file_path ,
38- results = [
39- DefectDojoResult .from_finding (
40- {
41- "id" : 1 ,
42- "title" : "python.django.security.audit.secure-cookies.django-secure-set-cookie" ,
43- "file_path" : file_path ,
44- "line" : 2 ,
45- },
46- )
47- ],
81+ FILE_PATH ,
82+ results = results ,
4883 )
4984 execution_context = CodemodExecutionContext (
5085 directory = mocker .MagicMock (),
@@ -54,7 +89,7 @@ def _apply_and_assert_with_tool(mocker, transformer, reason):
5489 repo_manager = mocker .MagicMock (),
5590 path_include = [],
5691 path_exclude = [],
57- tool_result_files_map = {"sonar " : ["results .json" ]},
92+ tool_result_files_map = {"DOESNTMATTER " : ["testing .json" ]},
5893 )
5994 pipeline = LibcstTransformerPipeline (transformer )
6095 pipeline .apply (
@@ -84,18 +119,31 @@ def test_transformer_error(mocker, caplog):
84119 assert "Failed to transform file" in caplog .text
85120
86121
87- def test_parse_error_with_tool (mocker , caplog ):
122+ def test_parse_error_with_defectdojo (mocker , caplog ):
88123 mocker .patch (
89124 "codemodder.codemods.libcst_transformer.cst.parse_module" ,
90125 side_effect = ParserSyntaxError ,
91126 )
92127 transformer = mocker .MagicMock (spec = LibcstResultTransformer )
93- _apply_and_assert_with_tool (mocker , transformer , "Failed to parse file" )
128+ _apply_and_assert_with_tool (
129+ mocker , transformer , "Failed to parse file" , DEFECTDOJO_RESULTS
130+ )
94131 assert "Failed to parse file" in caplog .text
95132
96133
97- def test_transformer_error_with_tool (mocker , caplog ):
134+ def test_transformer_error_with_defectdojo (mocker , caplog ):
98135 transformer = mocker .MagicMock (spec = LibcstResultTransformer )
99136 transformer .transform .side_effect = ParserSyntaxError
100- _apply_and_assert_with_tool (mocker , transformer , "Failed to transform file" )
137+ _apply_and_assert_with_tool (
138+ mocker , transformer , "Failed to transform file" , DEFECTDOJO_RESULTS
139+ )
140+ assert "Failed to transform file" in caplog .text
141+
142+
143+ def test_transformer_error_with_sonar (mocker , caplog ):
144+ transformer = mocker .MagicMock (spec = LibcstResultTransformer )
145+ transformer .transform .side_effect = ParserSyntaxError
146+ _apply_and_assert_with_tool (
147+ mocker , transformer , "Failed to transform file" , SONAR_RESULTS
148+ )
101149 assert "Failed to transform file" in caplog .text
0 commit comments