17
17
package org .sonar .python .checks .utils ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
- import static org .mockito .Mockito .mock ;
21
20
22
- import java .io .IOException ;
23
- import java .nio .file .Files ;
24
21
import org .junit .jupiter .api .Test ;
25
22
import org .mockito .Mockito ;
26
- import org .sonar .plugins .python .api .PythonFile ;
27
- import org .sonar .plugins .python .api .PythonVisitorContext ;
23
+ import org .sonar .plugins .python .api .SubscriptionContext ;
28
24
import org .sonar .plugins .python .api .project .configuration .AwsLambdaHandlerInfo ;
29
25
import org .sonar .plugins .python .api .project .configuration .ProjectConfiguration ;
30
- import org .sonar .plugins .python .api .tree .FileInput ;
31
26
import org .sonar .plugins .python .api .tree .FunctionDef ;
32
27
import org .sonar .plugins .python .api .tree .Name ;
33
28
import org .sonar .plugins .python .api .types .v2 .FunctionType ;
34
29
import org .sonar .plugins .python .api .types .v2 .PythonType ;
35
30
import org .sonar .python .semantic .v2 .callgraph .CallGraph ;
36
- import org .sonar .python .tree .FileInputImpl ;
37
31
38
32
class AwsLambdaChecksUtilsTest {
39
33
@ Test
40
34
void isLambdaHandlerTest_direct () {
41
- var pythonVisitorContext = pythonVisitorContext (CallGraph .EMPTY );
35
+ var subscriptionContext = subscriptionContext (CallGraph .EMPTY );
42
36
43
37
var functionDef = functionDef ("a.b.c" );
44
- assertThat (AwsLambdaChecksUtils .isLambdaHandler (pythonVisitorContext , functionDef )).isFalse ();
38
+ assertThat (AwsLambdaChecksUtils .isLambdaHandler (subscriptionContext , functionDef )).isFalse ();
45
39
46
- pythonVisitorContext .projectConfiguration ().awsProjectConfiguration ().awsLambdaHandlers ().add (new AwsLambdaHandlerInfo ("a.b.c" ));
47
- assertThat (AwsLambdaChecksUtils .isLambdaHandler (pythonVisitorContext , functionDef )).isTrue ();
40
+ subscriptionContext .projectConfiguration ().awsProjectConfiguration ().awsLambdaHandlers ()
41
+ .add (new AwsLambdaHandlerInfo ("a.b.c" ));
42
+ assertThat (AwsLambdaChecksUtils .isLambdaHandler (subscriptionContext , functionDef )).isTrue ();
48
43
49
44
var functionDefWithUnknownType = functionDef (PythonType .UNKNOWN );
50
- assertThat (AwsLambdaChecksUtils .isLambdaHandler (pythonVisitorContext , functionDefWithUnknownType )).isFalse ();
45
+ assertThat (AwsLambdaChecksUtils .isLambdaHandler (subscriptionContext , functionDefWithUnknownType )).isFalse ();
51
46
}
52
47
53
48
@ Test
54
49
void isLambdaHandlerTest_callGraph () {
55
50
var callGraph = new CallGraph .Builder ()
56
- .addUsage ("lambda.handler" , "a.b.c" )
57
- .addUsage ("a.b.c" , "e.f.g" )
58
- .build ();
51
+ .addUsage ("lambda.handler" , "a.b.c" )
52
+ .addUsage ("a.b.c" , "e.f.g" )
53
+ .build ();
59
54
60
- var pythonVisitorContext = pythonVisitorContext (callGraph );
55
+ var subscriptionContext = subscriptionContext (callGraph );
61
56
62
57
var functionDef = functionDef ("e.f.g" );
63
58
64
- assertThat (AwsLambdaChecksUtils .isLambdaHandler (pythonVisitorContext , functionDef )).isFalse ();
59
+ assertThat (AwsLambdaChecksUtils .isLambdaHandler (subscriptionContext , functionDef )).isFalse ();
65
60
66
- pythonVisitorContext .projectConfiguration ().awsProjectConfiguration ().awsLambdaHandlers ().add (new AwsLambdaHandlerInfo ("lambda.handler" ));
67
- assertThat (AwsLambdaChecksUtils .isLambdaHandler (pythonVisitorContext , functionDef )).isTrue ();
61
+ subscriptionContext .projectConfiguration ().awsProjectConfiguration ().awsLambdaHandlers ()
62
+ .add (new AwsLambdaHandlerInfo ("lambda.handler" ));
63
+ assertThat (AwsLambdaChecksUtils .isLambdaHandler (subscriptionContext , functionDef )).isTrue ();
68
64
}
69
65
70
- private static PythonVisitorContext pythonVisitorContext (CallGraph callGraph ) {
71
- PythonFile pythonFile = pythonFile ("test.py" );
72
- FileInput fileInput = mock (FileInputImpl .class );
73
-
74
- return new PythonVisitorContext .Builder (fileInput , pythonFile )
75
- .projectConfiguration (new ProjectConfiguration ())
76
- .callGraph (callGraph )
77
- .build ();
66
+ @ Test
67
+ void isOnlyLambdaHandlerTest () {
68
+ var callGraph = new CallGraph .Builder ()
69
+ .addUsage ("lambda.handler" , "a.b.c" )
70
+ .build ();
71
+
72
+ var subscriptionContext = subscriptionContext (callGraph );
73
+ subscriptionContext .projectConfiguration ().awsProjectConfiguration ().awsLambdaHandlers ()
74
+ .add (new AwsLambdaHandlerInfo ("lambda.handler" ));
75
+
76
+ var handlerFunction = functionDef ("lambda.handler" );
77
+ assertThat (AwsLambdaChecksUtils .isOnlyLambdaHandler (subscriptionContext , handlerFunction )).isTrue ();
78
+
79
+ var calledFunction = functionDef ("a.b.c" );
80
+ assertThat (AwsLambdaChecksUtils .isOnlyLambdaHandler (subscriptionContext , calledFunction )).isFalse ();
81
+ assertThat (AwsLambdaChecksUtils .isLambdaHandler (subscriptionContext , calledFunction )).isTrue ();
82
+
83
+ var unknownTypeFunction = functionDef (PythonType .UNKNOWN );
84
+ assertThat (AwsLambdaChecksUtils .isOnlyLambdaHandler (subscriptionContext , unknownTypeFunction )).isFalse ();
78
85
}
79
86
87
+ private static SubscriptionContext subscriptionContext (CallGraph callGraph ) {
88
+ var subscriptionContext = Mockito .mock (org .sonar .plugins .python .api .SubscriptionContext .class );
89
+ Mockito .when (subscriptionContext .projectConfiguration ()).thenReturn (new ProjectConfiguration ());
90
+ Mockito .when (subscriptionContext .callGraph ()).thenReturn (callGraph );
91
+
92
+ return subscriptionContext ;
93
+ }
80
94
81
95
private static FunctionDef functionDef (String name ) {
82
96
FunctionType functionNameType = Mockito .mock (FunctionType .class );
@@ -92,16 +106,4 @@ private static FunctionDef functionDef(PythonType type) {
92
106
Mockito .when (functionDef .name ()).thenReturn (functionName );
93
107
return functionDef ;
94
108
}
95
-
96
- private static PythonFile pythonFile (String fileName ) {
97
- PythonFile pythonFile = Mockito .mock (PythonFile .class );
98
- Mockito .when (pythonFile .fileName ()).thenReturn (fileName );
99
- try {
100
- Mockito .when (pythonFile .uri ()).thenReturn (Files .createTempFile (fileName , "py" ).toUri ());
101
- } catch (IOException e ) {
102
- throw new IllegalStateException ("Cannot create temporary file" );
103
- }
104
- return pythonFile ;
105
- }
106
-
107
109
}
0 commit comments