Skip to content

Commit 6a61dd7

Browse files
committed
SONARPY-192 Load default profile fron JSON file
1 parent 0b8c772 commit 6a61dd7

2 files changed

Lines changed: 96 additions & 9 deletions

File tree

sonar-python-plugin/src/main/java/org/sonar/plugins/python/PythonProfile.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@
1919
*/
2020
package org.sonar.plugins.python;
2121

22+
import com.google.common.io.Resources;
23+
import com.google.gson.Gson;
24+
import java.io.IOException;
25+
import java.net.URL;
26+
import java.nio.charset.StandardCharsets;
27+
import java.util.Set;
2228
import org.sonar.api.profiles.ProfileDefinition;
2329
import org.sonar.api.profiles.RulesProfile;
30+
import org.sonar.api.rules.Rule;
2431
import org.sonar.api.rules.RuleFinder;
2532
import org.sonar.api.utils.ValidationMessages;
2633
import org.sonar.python.checks.CheckList;
27-
import org.sonar.squidbridge.annotations.AnnotationBasedProfileBuilder;
2834

2935
public class PythonProfile extends ProfileDefinition {
3036

@@ -35,13 +41,31 @@ public PythonProfile(RuleFinder ruleFinder) {
3541
}
3642

3743
@Override
38-
public RulesProfile createProfile(ValidationMessages validation) {
39-
AnnotationBasedProfileBuilder annotationBasedProfileBuilder = new AnnotationBasedProfileBuilder(ruleFinder);
40-
return annotationBasedProfileBuilder.build(
41-
CheckList.REPOSITORY_KEY,
42-
CheckList.SONAR_WAY_PROFILE,
43-
Python.KEY,
44-
CheckList.getChecks(),
45-
validation);
44+
public RulesProfile createProfile(ValidationMessages messages) {
45+
RulesProfile profile = RulesProfile.create(CheckList.SONAR_WAY_PROFILE, Python.KEY);
46+
47+
loadActiveKeysFromJsonProfile(profile);
48+
return profile;
49+
}
50+
51+
private void loadActiveKeysFromJsonProfile(RulesProfile rulesProfile) {
52+
for (String ruleKey : activatedRuleKeys()) {
53+
Rule rule = ruleFinder.findByKey(CheckList.REPOSITORY_KEY, ruleKey);
54+
rulesProfile.activateRule(rule, null);
55+
}
56+
}
57+
58+
private static Set<String> activatedRuleKeys() {
59+
URL profileUrl = PythonProfile.class.getResource("/org/sonar/l10n/py/rules/python/Sonar_way_profile.json");
60+
try {
61+
Gson gson = new Gson();
62+
return gson.fromJson(Resources.toString(profileUrl, StandardCharsets.UTF_8), Profile.class).ruleKeys;
63+
} catch (IOException e) {
64+
throw new IllegalStateException("Failed to read: " + profileUrl, e);
65+
}
66+
}
67+
68+
private static class Profile {
69+
Set<String> ruleKeys;
4670
}
4771
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2017 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.plugins.python;
21+
22+
import org.junit.Test;
23+
import org.mockito.invocation.InvocationOnMock;
24+
import org.mockito.stubbing.Answer;
25+
import org.sonar.api.profiles.RulesProfile;
26+
import org.sonar.api.rules.Rule;
27+
import org.sonar.api.rules.RuleFinder;
28+
import org.sonar.api.utils.ValidationMessages;
29+
import org.sonar.python.checks.CheckList;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.Matchers.anyString;
33+
import static org.mockito.Mockito.mock;
34+
import static org.mockito.Mockito.when;
35+
36+
public class PythonProfileTest {
37+
38+
@Test
39+
public void should_create_sonar_way_profile() {
40+
ValidationMessages validation = ValidationMessages.create();
41+
42+
RuleFinder ruleFinder = ruleFinder();
43+
PythonProfile definition = new PythonProfile(ruleFinder);
44+
RulesProfile profile = definition.createProfile(validation);
45+
46+
assertThat(profile.getLanguage()).isEqualTo(Python.KEY);
47+
assertThat(profile.getName()).isEqualTo(CheckList.SONAR_WAY_PROFILE);
48+
assertThat(profile.getActiveRules()).extracting("repositoryKey").containsOnly(CheckList.REPOSITORY_KEY);
49+
assertThat(validation.hasErrors()).isFalse();
50+
assertThat(profile.getActiveRules().size()).isGreaterThan(25);
51+
}
52+
53+
static RuleFinder ruleFinder() {
54+
return when(mock(RuleFinder.class).findByKey(anyString(), anyString())).thenAnswer(new Answer<Rule>() {
55+
@Override
56+
public Rule answer(InvocationOnMock invocation) {
57+
Object[] arguments = invocation.getArguments();
58+
return Rule.create((String) arguments[0], (String) arguments[1], (String) arguments[1]);
59+
}
60+
}).getMock();
61+
}
62+
63+
}

0 commit comments

Comments
 (0)