Skip to content

Commit 5283f25

Browse files
pynicolasalban-auzeill
authored andcommitted
SONARPY-254 Stop testing SQ version against 6.0 or 6.2
1 parent fb15e0d commit 5283f25

8 files changed

Lines changed: 28 additions & 211 deletions

File tree

its/plugin/src/test/java/com/sonar/python/it/plugin/CoverageTest.java

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,12 @@ private void basic_coverage_reports(String utReportPath) {
5858
.setProperty("sonar.python.coverage.overallReportPath", "it-coverage.xml");
5959
orchestrator.executeBuild(build);
6060

61-
ImmutableMap<String, Integer> values;
62-
int linesToCover = 8;
63-
64-
if (is_before_sonar_6_2()) {
65-
values = new Builder<String, Integer>()
66-
.put("lines_to_cover", linesToCover)
67-
.put("coverage", 80)
68-
.put("line_coverage", 75)
69-
.put("branch_coverage", 100)
70-
.put("it_coverage", 40)
71-
.put("it_line_coverage", 50)
72-
.put("it_branch_coverage", 0)
73-
.put("overall_coverage", 40)
74-
.put("overall_line_coverage", 50)
75-
.put("overall_branch_coverage", 0)
76-
.build();
77-
78-
} else {
79-
values = new Builder<String, Integer>()
80-
.put("lines_to_cover", linesToCover)
61+
ImmutableMap<String, Integer> values = new Builder<String, Integer>()
62+
.put("lines_to_cover", 8)
8163
.put("coverage", 90)
8264
.put("line_coverage", 87)
8365
.put("branch_coverage", 100)
8466
.build();
85-
}
8667

8768
Tests.assertProjectMeasures(PROJECT_KEY, values);
8869
}
@@ -97,28 +78,11 @@ public void force_zero_coverage_for_untouched_files() throws Exception {
9778
.setProperty("sonar.python.coverage.forceZeroCoverage", "true");
9879
orchestrator.executeBuild(build);
9980

100-
ImmutableMap<String, Integer> values;
101-
102-
if (is_before_sonar_6_2()) {
103-
values = new ImmutableMap.Builder<String, Integer>()
104-
.put("coverage", 50)
105-
.put("line_coverage", 42)
106-
.put("branch_coverage", 100)
107-
.put("it_coverage", 25)
108-
.put("it_line_coverage", 28)
109-
.put("it_branch_coverage", 0)
110-
.put("overall_coverage", 25)
111-
.put("overall_line_coverage", 28)
112-
.put("overall_branch_coverage", 0)
113-
.build();
114-
115-
} else {
116-
values = new ImmutableMap.Builder<String, Integer>()
81+
ImmutableMap<String, Integer> values = new ImmutableMap.Builder<String, Integer>()
11782
.put("coverage", 56)
11883
.put("line_coverage", 50)
11984
.put("branch_coverage", 100)
12085
.build();
121-
}
12286

12387
Tests.assertProjectMeasures(PROJECT_KEY, values);
12488
}
@@ -131,27 +95,15 @@ public void force_zero_coverage_with_no_report() throws Exception {
13195
orchestrator.executeBuild(build);
13296

13397
Map<String, Integer> expected = new HashMap<>();
134-
if (is_before_sonar_6_2()) {
135-
expected.put("coverage", 0);
136-
expected.put("line_coverage", 0);
137-
expected.put("branch_coverage", null);
138-
expected.put("it_coverage", 0);
139-
expected.put("it_line_coverage", 0);
140-
expected.put("it_branch_coverage", null);
141-
expected.put("overall_coverage", 0);
142-
expected.put("overall_line_coverage", 0);
143-
expected.put("overall_branch_coverage", null);
144-
} else {
145-
expected.put("coverage", 0);
146-
expected.put("line_coverage", 0);
147-
expected.put("branch_coverage", null);
148-
expected.put("it_coverage", null);
149-
expected.put("it_line_coverage", null);
150-
expected.put("it_branch_coverage", null);
151-
expected.put("overall_coverage", null);
152-
expected.put("overall_line_coverage", null);
153-
expected.put("overall_branch_coverage", null);
154-
}
98+
expected.put("coverage", 0);
99+
expected.put("line_coverage", 0);
100+
expected.put("branch_coverage", null);
101+
expected.put("it_coverage", null);
102+
expected.put("it_line_coverage", null);
103+
expected.put("it_branch_coverage", null);
104+
expected.put("overall_coverage", null);
105+
expected.put("overall_line_coverage", null);
106+
expected.put("overall_branch_coverage", null);
155107
Tests.assertProjectMeasures(PROJECT_KEY, expected);
156108
}
157109

@@ -176,7 +128,4 @@ public void empty_coverage_report() throws Exception {
176128
assertThat(getProjectMeasure(PROJECT_KEY, "overall_coverage")).isNull();
177129
}
178130

179-
private static boolean is_before_sonar_6_2() {
180-
return !orchestrator.getConfiguration().getSonarVersion().isGreaterThanOrEquals("6.2");
181-
}
182131
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.sonar.api.SonarProduct;
2525
import org.sonar.api.config.PropertyDefinition;
2626
import org.sonar.api.resources.Qualifiers;
27-
import org.sonar.api.utils.Version;
2827
import org.sonar.plugins.python.coverage.PythonCoverageSensor;
2928
import org.sonar.plugins.python.pylint.PylintConfiguration;
3029
import org.sonar.plugins.python.pylint.PylintImportSensor;
@@ -144,9 +143,9 @@ public void define(Context context) {
144143
PythonProfile.class,
145144

146145
PythonSquidSensor.class,
147-
new PythonRuleRepository(context.getSonarQubeVersion()));
146+
PythonRuleRepository.class);
148147

149-
if (!context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(6, 0)) || context.getRuntime().getProduct() != SonarProduct.SONARLINT) {
148+
if (context.getRuntime().getProduct() != SonarProduct.SONARLINT) {
150149
context.addExtensions(
151150
PylintConfiguration.class,
152151
PylintSensor.class,

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.stream.Collectors;
2727
import java.util.stream.StreamSupport;
2828
import org.sonar.api.server.rule.RulesDefinition;
29-
import org.sonar.api.utils.Version;
3029
import org.sonar.python.checks.CheckList;
3130
import org.sonarsource.analyzer.commons.RuleMetadataLoader;
3231

@@ -38,12 +37,6 @@ public class PythonRuleRepository implements RulesDefinition {
3837

3938
private static final Set<String> TEMPLATE_RULE_KEYS = new HashSet<>(Arrays.asList("XPath", "CommentRegularExpression"));
4039

41-
private final Version sonarRuntimeVersion;
42-
43-
public PythonRuleRepository(Version sonarRuntimeVersion) {
44-
this.sonarRuntimeVersion = sonarRuntimeVersion;
45-
}
46-
4740
@Override
4841
public void define(Context context) {
4942
NewRepository repository = context
@@ -59,12 +52,8 @@ public void define(Context context) {
5952
repository.done();
6053
}
6154

62-
private RuleMetadataLoader getRuleMetadataLoader() {
63-
if (sonarRuntimeVersion.isGreaterThanOrEqual(Version.create(6, 0))) {
64-
return new RuleMetadataLoader(RESOURCE_FOLDER, PythonProfile.PROFILE_LOCATION);
65-
} else {
66-
return new RuleMetadataLoader(RESOURCE_FOLDER);
67-
}
55+
private static RuleMetadataLoader getRuleMetadataLoader() {
56+
return new RuleMetadataLoader(RESOURCE_FOLDER, PythonProfile.PROFILE_LOCATION);
6857
}
6958

7059
private static List<Class> getCheckClasses() {

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.sonar.api.measures.FileLinesContextFactory;
4141
import org.sonar.api.measures.Metric;
4242
import org.sonar.api.rule.RuleKey;
43-
import org.sonar.api.utils.Version;
4443
import org.sonar.api.utils.log.Logger;
4544
import org.sonar.api.utils.log.Loggers;
4645
import org.sonar.plugins.python.coverage.PythonCoverageSensor;
@@ -62,8 +61,6 @@ public class PythonScanner {
6261
private static final Number[] FUNCTIONS_DISTRIB_BOTTOM_LIMITS = {1, 2, 4, 6, 8, 10, 12, 20, 30};
6362
private static final Number[] FILES_DISTRIB_BOTTOM_LIMITS = {0, 5, 10, 20, 30, 60, 90};
6463

65-
private static final Version V6_0 = Version.create(6, 0);
66-
6764
private final SensorContext context;
6865
private final Parser<Grammar> parser;
6966
private final List<InputFile> inputFiles;
@@ -95,7 +92,7 @@ public void scanFiles() {
9592
}
9693

9794
private void scanFile(InputFile inputFile) {
98-
PythonFile pythonFile = SonarQubePythonFile.create(inputFile, context);
95+
PythonFile pythonFile = SonarQubePythonFile.create(inputFile);
9996
PythonVisitorContext visitorContext;
10097
try {
10198
visitorContext = new PythonVisitorContext(parser.parse(pythonFile.content()), pythonFile);
@@ -219,6 +216,6 @@ private void saveFilesComplexityDistribution(InputFile inputFile, FileMetrics fi
219216
}
220217

221218
private static boolean isSonarLint(SensorContext context) {
222-
return context.getSonarQubeVersion().isGreaterThanOrEqual(V6_0) && context.runtime().getProduct() == SonarProduct.SONARLINT;
219+
return context.runtime().getProduct() == SonarProduct.SONARLINT;
223220
}
224221
}

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

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,19 @@
2020
package org.sonar.plugins.python;
2121

2222
import java.io.IOException;
23-
import java.nio.charset.Charset;
24-
import java.nio.file.Files;
2523
import org.sonar.api.batch.fs.InputFile;
26-
import org.sonar.api.batch.sensor.SensorContext;
27-
import org.sonar.api.utils.Version;
2824
import org.sonar.python.PythonFile;
2925

3026
public abstract class SonarQubePythonFile implements PythonFile {
3127

32-
private static final Version V6_0 = Version.create(6, 0);
33-
private static final Version V6_2 = Version.create(6, 2);
34-
3528
private final InputFile inputFile;
3629

3730
private SonarQubePythonFile(InputFile inputFile) {
3831
this.inputFile = inputFile;
3932
}
4033

41-
public static PythonFile create(InputFile inputFile, SensorContext context) {
42-
Version version = context.getSonarQubeVersion();
43-
if (version.isGreaterThanOrEqual(V6_2)) {
44-
return new Sq62File(inputFile);
45-
}
46-
if (version.isGreaterThanOrEqual(V6_0)) {
47-
return new Sq60File(inputFile);
48-
}
49-
return new Sq56File(inputFile, context.fileSystem().encoding());
34+
public static PythonFile create(InputFile inputFile) {
35+
return new Sq62File(inputFile);
5036
}
5137

5238
@Override
@@ -58,43 +44,6 @@ public InputFile inputFile() {
5844
return inputFile;
5945
}
6046

61-
private static String contentForCharset(InputFile inputFile, Charset charset) {
62-
try {
63-
return new String(Files.readAllBytes(inputFile.path()), charset);
64-
} catch (IOException e) {
65-
throw new IllegalStateException("Could not read content of input file " + inputFile, e);
66-
}
67-
}
68-
69-
private static class Sq56File extends SonarQubePythonFile {
70-
71-
private final Charset fileSystemEncoding;
72-
73-
public Sq56File(InputFile inputFile, Charset fileSystemEncoding) {
74-
super(inputFile);
75-
this.fileSystemEncoding = fileSystemEncoding;
76-
}
77-
78-
@Override
79-
public String content() {
80-
return contentForCharset(inputFile(), fileSystemEncoding);
81-
}
82-
83-
}
84-
85-
private static class Sq60File extends SonarQubePythonFile {
86-
87-
public Sq60File(InputFile inputFile) {
88-
super(inputFile);
89-
}
90-
91-
@Override
92-
public String content() {
93-
return contentForCharset(inputFile(), inputFile().charset());
94-
}
95-
96-
}
97-
9847
private static class Sq62File extends SonarQubePythonFile {
9948

10049
public Sq62File(InputFile inputFile) {

sonar-python-plugin/src/test/java/org/sonar/plugins/python/PythonPluginTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public class PythonPluginTest {
3333

3434
@Test
3535
public void testGetExtensions() {
36-
Version v56 = Version.create(5, 6);
3736
Version v60 = Version.create(6, 0);
38-
assertThat(extensions(SonarRuntimeImpl.forSonarQube(v56, SonarQubeSide.SERVER))).hasSize(19);
3937
assertThat(extensions(SonarRuntimeImpl.forSonarQube(v60, SonarQubeSide.SERVER))).hasSize(19);
4038
assertThat(extensions(SonarRuntimeImpl.forSonarLint(v60))).hasSize(14);
4139
}

sonar-python-plugin/src/test/java/org/sonar/plugins/python/PythonRuleRepositoryTest.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.List;
2323
import org.junit.Test;
2424
import org.sonar.api.server.rule.RulesDefinition;
25-
import org.sonar.api.utils.Version;
2625
import org.sonar.python.checks.CheckList;
2726

2827
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,33 +30,22 @@ public class PythonRuleRepositoryTest {
3130

3231
@Test
3332
public void createRulesTest() {
34-
RulesDefinition.Repository repository = buildRepository(Version.parse("5.6"));
33+
RulesDefinition.Repository repository = buildRepository();
3534

3635
assertThat(repository.language()).isEqualTo("py");
3736
assertThat(repository.name()).isEqualTo("SonarAnalyzer");
3837

3938
List<RulesDefinition.Rule> rules = repository.rules();
4039
assertThat(rules).isNotNull();
4140
assertThat(rules).hasSize(54);
42-
}
43-
44-
@Test
45-
public void sonarqube56() {
46-
RulesDefinition.Repository repository = buildRepository(Version.parse("5.6"));
47-
assertThat(repository.rule("S1578").activatedByDefault()).isFalse();
48-
assertThat(repository.rule("BackticksUsage").activatedByDefault()).isFalse();
49-
}
5041

51-
@Test
52-
public void sonarlint() {
53-
RulesDefinition.Repository repository = buildRepository(Version.parse("6.0"));
5442
assertThat(repository.rule("S1578").activatedByDefault()).isFalse();
5543
assertThat(repository.rule("BackticksUsage").activatedByDefault()).isTrue();
5644
}
5745

5846
@Test
5947
public void ruleTemplates() {
60-
RulesDefinition.Repository repository = buildRepository(Version.parse("6.7"));
48+
RulesDefinition.Repository repository = buildRepository();
6149
assertThat(repository.rule("S100").template()).isFalse();
6250
assertThat(repository.rule("CommentRegularExpression").template()).isTrue();
6351
assertThat(repository.rule("XPath").template()).isTrue();
@@ -70,8 +58,8 @@ public void ruleTemplates() {
7058
assertThat(templateCount).isEqualTo(2);
7159
}
7260

73-
private RulesDefinition.Repository buildRepository(Version sonarRuntimeVersion) {
74-
PythonRuleRepository ruleRepository = new PythonRuleRepository(sonarRuntimeVersion);
61+
private RulesDefinition.Repository buildRepository() {
62+
PythonRuleRepository ruleRepository = new PythonRuleRepository();
7563
RulesDefinition.Context context = new RulesDefinition.Context();
7664
ruleRepository.define(context);
7765
return context.repository(CheckList.REPOSITORY_KEY);

0 commit comments

Comments
 (0)