21
21
22
22
import com .google .common .collect .Iterables ;
23
23
import java .io .File ;
24
+ import java .io .FileInputStream ;
24
25
import java .io .IOException ;
26
+ import java .io .InputStream ;
27
+ import java .io .InputStreamReader ;
25
28
import java .nio .file .Files ;
26
29
import java .nio .file .Path ;
27
30
import java .nio .file .Paths ;
31
+ import java .util .Arrays ;
28
32
import java .util .List ;
33
+ import java .util .Set ;
29
34
import java .util .regex .Matcher ;
30
35
import java .util .regex .Pattern ;
31
36
import java .util .stream .Collectors ;
32
37
import java .util .stream .Stream ;
33
38
import org .apache .commons .io .FileUtils ;
34
39
import org .junit .Test ;
40
+ import org .sonarsource .analyzer .commons .internal .json .simple .JSONArray ;
41
+ import org .sonarsource .analyzer .commons .internal .json .simple .JSONObject ;
42
+ import org .sonarsource .analyzer .commons .internal .json .simple .parser .JSONParser ;
43
+ import org .sonarsource .analyzer .commons .internal .json .simple .parser .ParseException ;
35
44
36
45
import static java .nio .charset .StandardCharsets .UTF_8 ;
37
46
import static org .assertj .core .api .Assertions .assertThat ;
47
+ import static org .assertj .core .api .Assertions .fail ;
38
48
39
49
public class CheckListTest {
40
50
@@ -48,7 +58,7 @@ public class CheckListTest {
48
58
@ Test
49
59
public void count () {
50
60
int count = 0 ;
51
- List <File > files = (List <File >) FileUtils .listFiles (new File ("src/main/java/org/sonar/python/checks/" ), new String [] {"java" }, true );
61
+ List <File > files = (List <File >) FileUtils .listFiles (new File ("src/main/java/org/sonar/python/checks/" ), new String []{"java" }, true );
52
62
for (File file : files ) {
53
63
if (file .getName ().endsWith ("Check.java" ) && !file .getName ().startsWith ("Abstract" )) {
54
64
count ++;
@@ -67,8 +77,8 @@ public void test() {
67
77
for (Class cls : checks ) {
68
78
String testName = '/' + cls .getName ().replace ('.' , '/' ) + "Test.class" ;
69
79
assertThat (getClass ().getResource (testName ))
70
- .overridingErrorMessage ("No test for " + cls .getSimpleName ())
71
- .isNotNull ();
80
+ .overridingErrorMessage ("No test for " + cls .getSimpleName ())
81
+ .isNotNull ();
72
82
}
73
83
}
74
84
@@ -94,6 +104,76 @@ public void validate_sqKey_field_in_json() throws IOException {
94
104
}
95
105
}
96
106
107
+ @ Test
108
+ public void test_no_deprecated_rule_in_default_profile () throws IOException , ParseException {
109
+ try (Stream <Path > fileStream = Files .find (METADATA_DIR , 1 , (path , attr ) -> path .toString ().endsWith (".json" ))) {
110
+ List <Path > jsonList = fileStream .collect (Collectors .toList ());
111
+
112
+ Path sonarWayProfilePath = jsonList .stream ().filter (path -> path .toString ().endsWith ("Sonar_way_profile.json" )).findFirst ().get ();
113
+ List <String > keysInDefaultProfile = getKeysInDefaultProfile (sonarWayProfilePath );
114
+
115
+ Set <String > deprecatedKeys = jsonList .stream ()
116
+ .filter (path -> !path .toString ().endsWith ("Sonar_way_profile.json" ))
117
+ .filter (path1 -> {
118
+ try {
119
+ return isDeprecated (path1 );
120
+ } catch (Exception e ) {
121
+ fail (String .format ("Exception when deserializing JSON file \" %s\" " , path1 .getFileName ().toString ()));
122
+ return false ;
123
+ }
124
+ })
125
+ .map (Path ::getFileName )
126
+ .map (Path ::toString )
127
+ .map (name -> name .replaceAll ("\\ .json$" , "" ))
128
+ .collect (Collectors .toSet ());
129
+
130
+ assertThat (keysInDefaultProfile ).isNotEmpty ();
131
+ assertThat (deprecatedKeys ).isNotEmpty ();
132
+ assertThat (keysInDefaultProfile ).doesNotContainAnyElementsOf (deprecatedKeys );
133
+ }
134
+ }
135
+
136
+ @ Test
137
+ public void test_locally_deprecated_rules_stay_deprecated () throws IOException , ParseException {
138
+ // Some rules have been deprecated only for Python. When executed, rule-api reverts those rule to "ready" status, which is incorrect.
139
+ // This test is here to ensure it doesn't happen.
140
+ List <String > locallyDeprecatedRules = Arrays .asList ("S1523" , "S4721" );
141
+ try (Stream <Path > fileStream = Files .find (METADATA_DIR , 1 , (path , attr ) -> path .toString ().endsWith (".json" ))) {
142
+ Set <String > deprecatedKeys = fileStream
143
+ .filter (path -> !path .toString ().endsWith ("Sonar_way_profile.json" ))
144
+ .filter (path1 -> {
145
+ try {
146
+ return isDeprecated (path1 );
147
+ } catch (Exception e ) {
148
+ fail (String .format ("Exception when deserializing JSON file \" %s\" " , path1 .getFileName ().toString ()));
149
+ return false ;
150
+ }
151
+ })
152
+ .map (Path ::getFileName )
153
+ .map (Path ::toString )
154
+ .map (name -> name .replaceAll ("\\ .json$" , "" ))
155
+ .collect (Collectors .toSet ());
156
+
157
+ assertThat (deprecatedKeys ).containsAll (locallyDeprecatedRules );
158
+ }
159
+ }
160
+
161
+ private static boolean isDeprecated (Path path ) throws IOException , ParseException {
162
+ InputStream in = new FileInputStream (path .toFile ());
163
+ JSONParser jsonParser = new JSONParser ();
164
+ JSONObject ruleJson = (JSONObject ) jsonParser .parse (new InputStreamReader (in , UTF_8 ));
165
+ Object status = ruleJson .get ("status" );
166
+ return status .equals ("deprecated" );
167
+ }
168
+
169
+ private static List <String > getKeysInDefaultProfile (Path sonarWayPath ) throws IOException , ParseException {
170
+ InputStream in = new FileInputStream (sonarWayPath .toFile ());
171
+ JSONParser jsonParser = new JSONParser ();
172
+ JSONObject sonarWayJson = (JSONObject ) jsonParser .parse (new InputStreamReader (in , UTF_8 ));
173
+ JSONArray sonarWayKeys = (JSONArray ) sonarWayJson .get ("ruleKeys" );
174
+ return (List <String >) sonarWayKeys .stream ().sorted ().collect (Collectors .toList ());
175
+ }
176
+
97
177
private static String extractSqKey (Path jsonFile ) {
98
178
try {
99
179
String content = new String (Files .readAllBytes (jsonFile ), UTF_8 );
0 commit comments