Skip to content

Commit efcb512

Browse files
committed
SONARCS-579 Fail the analysis when custom Roslyn rules are enabled and MSBuild 12 is used
1 parent b831231 commit efcb512

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/main/java/org/sonar/plugins/csharp/CSharpSensor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ private void analyze(boolean includeRules) {
119119
LOG.warn("* Use MSBuild 14 to get the best analysis results *");
120120
LOG.warn("* The use of MSBuild 12 or the sonar-runner to analyze C# projects is DEPRECATED *");
121121
LOG.warn("**********************************************************************************");
122+
123+
ImmutableMultimap<String, ActiveRule> activeRoslynRulesByPartialRepoKey = RoslynProfileExporter.activeRoslynRulesByPartialRepoKey(ruleProfile.getActiveRules());
124+
if (activeRoslynRulesByPartialRepoKey.keySet().size() > 1) {
125+
throw new IllegalArgumentException("Custom and 3rd party Roslyn analyzers are only by MSBuild 14. Either use MSBuild 14, or disable the custom/3rd party Roslyn analyzers in your quality profile.");
126+
}
122127
}
123128

124129
String analysisSettings = analysisSettings(true, settings.getBoolean("sonar.cs.ignoreHeaderComments"), includeRules, ruleProfile, filesToAnalyze());

src/test/java/org/sonar/plugins/csharp/CSharpSensorTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import com.google.common.base.Charsets;
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.collect.ImmutableSet;
25+
import com.google.common.collect.Lists;
2526
import com.google.common.io.Files;
2627
import java.io.File;
28+
import java.util.List;
2729
import org.junit.Before;
2830
import org.junit.Test;
2931
import org.junit.rules.ExpectedException;
@@ -88,6 +90,7 @@ public class CSharpSensorTest {
8890
private Issue issue5;
8991
private ActiveRule parametersActiveRule;
9092
private ActiveRule customRoslynActiveRule;
93+
private List<ActiveRule> allEnabledRules = Lists.newArrayList();
9194

9295
@Test
9396
public void shouldExecuteOnProject() {
@@ -183,7 +186,9 @@ public void init() {
183186

184187
RulesProfile rulesProfile = mock(RulesProfile.class);
185188
when(rulesProfile.getActiveRulesByRepository("csharpsquid")).thenReturn(ImmutableList.of(templateActiveRule, parametersActiveRule));
186-
when(rulesProfile.getActiveRules()).thenReturn(ImmutableList.of(templateActiveRule, parametersActiveRule, customRoslynActiveRule));
189+
allEnabledRules.add(templateActiveRule);
190+
allEnabledRules.add(parametersActiveRule);
191+
when(rulesProfile.getActiveRules()).thenReturn(allEnabledRules);
187192

188193
settings = mock(Settings.class);
189194
sensor =
@@ -196,6 +201,10 @@ public void init() {
196201
context = mock(SensorContext.class);
197202
}
198203

204+
private void enableCustomRoslynRules() {
205+
allEnabledRules.add(customRoslynActiveRule);
206+
}
207+
199208
@Test
200209
public void metrics() {
201210
sensor.analyse(project, context);
@@ -271,6 +280,8 @@ public void escapesAnalysisInput() throws Exception {
271280

272281
@Test
273282
public void roslynReportIsProcessed() {
283+
enableCustomRoslynRules();
284+
274285
when(settings.getString("sonar.cs.roslyn.reportFilePath")).thenReturn(new File("src/test/resources/CSharpSensorTest/roslyn-report.json").getAbsolutePath());
275286
sensor.analyse(project, context);
276287

@@ -321,6 +332,8 @@ public void roslynEmptyReportShouldNotFail() {
321332

322333
@Test
323334
public void failWithDuplicateRuleKey() {
335+
enableCustomRoslynRules();
336+
324337
String ruleKey = parametersActiveRule.getRuleKey();
325338
when(customRoslynActiveRule.getRuleKey()).thenReturn(ruleKey);
326339

@@ -331,4 +344,13 @@ public void failWithDuplicateRuleKey() {
331344
sensor.analyse(project, context);
332345
}
333346

347+
@Test
348+
public void failWithCustomRoslynRulesAndMSBuild12() {
349+
enableCustomRoslynRules();
350+
when(settings.getString("sonar.cs.roslyn.reportFilePath")).thenReturn(null);
351+
352+
thrown.expectMessage("Custom and 3rd party Roslyn analyzers are only by MSBuild 14. Either use MSBuild 14, or disable the custom/3rd party Roslyn analyzers in your quality profile.");
353+
sensor.analyse(project, context);
354+
}
355+
334356
}

0 commit comments

Comments
 (0)