Search in sources :

Example 11 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder in project sonarqube by SonarSource.

the class DefaultSensorContextTest method prepare.

@Before
public void prepare() throws Exception {
    activeRules = new ActiveRulesBuilder().build();
    fs = new DefaultFileSystem(temp.newFolder().toPath());
    MetricFinder metricFinder = mock(MetricFinder.class);
    when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
    when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
    settings = new MapSettings();
    sensorStorage = mock(SensorStorage.class);
    analysisMode = mock(AnalysisMode.class);
    runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER);
    adaptor = new DefaultSensorContext(mock(InputModule.class), settings, fs, activeRules, analysisMode, sensorStorage, runtime);
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) MapSettings(org.sonar.api.config.MapSettings) MetricFinder(org.sonar.api.batch.measure.MetricFinder) AnalysisMode(org.sonar.api.batch.AnalysisMode) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) Before(org.junit.Before)

Example 12 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder in project sonarqube by SonarSource.

the class SensorOptimizerTest method should_optimize_on_repository.

@Test
public void should_optimize_on_repository() {
    DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor().createIssuesForRuleRepositories("squid");
    assertThat(optimizer.shouldExecute(descriptor)).isFalse();
    ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("repo1", "foo")).activate().build();
    optimizer = new SensorOptimizer(fs, activeRules, settings);
    assertThat(optimizer.shouldExecute(descriptor)).isFalse();
    activeRules = new ActiveRulesBuilder().create(RuleKey.of("repo1", "foo")).activate().create(RuleKey.of("squid", "rule")).activate().build();
    optimizer = new SensorOptimizer(fs, activeRules, settings);
    assertThat(optimizer.shouldExecute(descriptor)).isTrue();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) DefaultSensorDescriptor(org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Test(org.junit.Test)

Example 13 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder in project sonarlint-core by SonarSource.

the class SonarQubeActiveRulesProvider method provide.

public ActiveRules provide(Sonarlint.Rules storageRules, Sonarlint.QProfiles qProfiles, StorageReader storageReader, Rules rules, ConnectedAnalysisConfiguration analysisConfiguration, Languages languages) {
    if (activeRules == null) {
        Map<String, String> qProfilesByLanguage = loadQualityProfilesFromStorage(qProfiles, storageReader, analysisConfiguration);
        ActiveRulesBuilder builder = new ActiveRulesBuilder();
        for (Map.Entry<String, String> entry : qProfilesByLanguage.entrySet()) {
            String language = entry.getKey();
            if (languages.get(language) == null) {
                continue;
            }
            String qProfileKey = entry.getValue();
            QProfile qProfile = qProfiles.getQprofilesByKeyOrThrow(qProfileKey);
            if (qProfile.getActiveRuleCount() == 0) {
                LOG.debug("  * {}: {} (0 rules)", language, qProfileKey);
                continue;
            }
            Sonarlint.ActiveRules activeRulesFromStorage = storageReader.readActiveRules(qProfileKey);
            LOG.debug("  * {}: {} ({} rules)", language, qProfileKey, activeRulesFromStorage.getActiveRulesByKeyMap().size());
            for (ActiveRule activeRule : activeRulesFromStorage.getActiveRulesByKeyMap().values()) {
                createNewActiveRule(builder, activeRule, storageRules, language, rules);
            }
        }
        activeRules = builder.build();
    }
    return activeRules;
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRule(org.sonarsource.sonarlint.core.proto.Sonarlint.ActiveRules.ActiveRule) Sonarlint(org.sonarsource.sonarlint.core.proto.Sonarlint) Map(java.util.Map) QProfile(org.sonarsource.sonarlint.core.proto.Sonarlint.QProfiles.QProfile)

Example 14 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder in project sonarlint-core by SonarSource.

the class SensorOptimizerTest method should_optimize_on_repository.

@Test
public void should_optimize_on_repository() {
    DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor().createIssuesForRuleRepositories("squid");
    assertThat(optimizer.shouldExecute(descriptor)).isFalse();
    ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("repo1", "foo")).activate().build();
    optimizer = new SensorOptimizer(fs, activeRules, settings.asConfig());
    assertThat(optimizer.shouldExecute(descriptor)).isFalse();
    activeRules = new ActiveRulesBuilder().create(RuleKey.of("repo1", "foo")).activate().create(RuleKey.of("squid", "rule")).activate().build();
    optimizer = new SensorOptimizer(fs, activeRules, settings.asConfig());
    assertThat(optimizer.shouldExecute(descriptor)).isTrue();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) DefaultSensorDescriptor(org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Test(org.junit.Test)

Example 15 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder in project sonarlint-core by SonarSource.

the class StandaloneActiveRulesProvider method createActiveRules.

private ActiveRules createActiveRules() {
    ActiveRulesBuilder builder = new ActiveRulesBuilder();
    ListMultimap<String, RulesProfile> profilesByLanguage = profilesByLanguage(profileDefinitions);
    for (String language : profilesByLanguage.keySet()) {
        List<RulesProfile> defs = profilesByLanguage.get(language);
        registerProfilesForLanguage(builder, language, defs);
    }
    for (Repository repo : ruleDefsLoader.getContext().repositories()) {
        for (Rule rule : repo.rules()) {
            if (rule.activatedByDefault()) {
                NewActiveRule newAr = builder.create(RuleKey.of(repo.key(), rule.key())).setLanguage(repo.language()).setName(rule.name()).setSeverity(rule.severity()).setInternalKey(rule.internalKey());
                for (Param param : rule.params()) {
                    newAr.setParam(param.key(), param.defaultValue());
                }
                newAr.activate();
            }
        }
    }
    return builder.build();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) Repository(org.sonar.api.server.rule.RulesDefinition.Repository) RulesProfile(org.sonar.api.profiles.RulesProfile) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRuleParam(org.sonar.api.rules.ActiveRuleParam) Param(org.sonar.api.server.rule.RulesDefinition.Param) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) Rule(org.sonar.api.server.rule.RulesDefinition.Rule) ActiveRule(org.sonar.api.rules.ActiveRule)

Aggregations

ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)22 Test (org.junit.Test)11 ActiveRules (org.sonar.api.batch.rule.ActiveRules)9 NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)9 Before (org.junit.Before)6 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)5 DefaultSensorDescriptor (org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor)4 RulesProfile (org.sonar.api.profiles.RulesProfile)4 Map (java.util.Map)3 InputFile (org.sonar.api.batch.fs.InputFile)3 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)3 MapSettings (org.sonar.api.config.internal.MapSettings)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 MetricFinder (org.sonar.api.batch.measure.MetricFinder)2 CheckFactory (org.sonar.api.batch.rule.CheckFactory)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2 SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)2 File (java.io.File)1