Skip to content

Commit

Permalink
SONARCS-579 Fail the analysis when custom Roslyn rules are enabled an…
Browse files Browse the repository at this point in the history
…d MSBuild 12 is used
  • Loading branch information
dbolkensteyn committed Feb 12, 2016
1 parent b831231 commit efcb512
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/org/sonar/plugins/csharp/CSharpSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ private void analyze(boolean includeRules) {
LOG.warn("* Use MSBuild 14 to get the best analysis results *");
LOG.warn("* The use of MSBuild 12 or the sonar-runner to analyze C# projects is DEPRECATED *");
LOG.warn("**********************************************************************************");

ImmutableMultimap<String, ActiveRule> activeRoslynRulesByPartialRepoKey = RoslynProfileExporter.activeRoslynRulesByPartialRepoKey(ruleProfile.getActiveRules());
if (activeRoslynRulesByPartialRepoKey.keySet().size() > 1) {
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.");
}
}

String analysisSettings = analysisSettings(true, settings.getBoolean("sonar.cs.ignoreHeaderComments"), includeRules, ruleProfile, filesToAnalyze());
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/org/sonar/plugins/csharp/CSharpSensorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import java.io.File;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -88,6 +90,7 @@ public class CSharpSensorTest {
private Issue issue5;
private ActiveRule parametersActiveRule;
private ActiveRule customRoslynActiveRule;
private List<ActiveRule> allEnabledRules = Lists.newArrayList();

@Test
public void shouldExecuteOnProject() {
Expand Down Expand Up @@ -183,7 +186,9 @@ public void init() {

RulesProfile rulesProfile = mock(RulesProfile.class);
when(rulesProfile.getActiveRulesByRepository("csharpsquid")).thenReturn(ImmutableList.of(templateActiveRule, parametersActiveRule));
when(rulesProfile.getActiveRules()).thenReturn(ImmutableList.of(templateActiveRule, parametersActiveRule, customRoslynActiveRule));
allEnabledRules.add(templateActiveRule);
allEnabledRules.add(parametersActiveRule);
when(rulesProfile.getActiveRules()).thenReturn(allEnabledRules);

settings = mock(Settings.class);
sensor =
Expand All @@ -196,6 +201,10 @@ public void init() {
context = mock(SensorContext.class);
}

private void enableCustomRoslynRules() {
allEnabledRules.add(customRoslynActiveRule);
}

@Test
public void metrics() {
sensor.analyse(project, context);
Expand Down Expand Up @@ -271,6 +280,8 @@ public void escapesAnalysisInput() throws Exception {

@Test
public void roslynReportIsProcessed() {
enableCustomRoslynRules();

when(settings.getString("sonar.cs.roslyn.reportFilePath")).thenReturn(new File("src/test/resources/CSharpSensorTest/roslyn-report.json").getAbsolutePath());
sensor.analyse(project, context);

Expand Down Expand Up @@ -321,6 +332,8 @@ public void roslynEmptyReportShouldNotFail() {

@Test
public void failWithDuplicateRuleKey() {
enableCustomRoslynRules();

String ruleKey = parametersActiveRule.getRuleKey();
when(customRoslynActiveRule.getRuleKey()).thenReturn(ruleKey);

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

@Test
public void failWithCustomRoslynRulesAndMSBuild12() {
enableCustomRoslynRules();
when(settings.getString("sonar.cs.roslyn.reportFilePath")).thenReturn(null);

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.");
sensor.analyse(project, context);
}

}

0 comments on commit efcb512

Please sign in to comment.