private static ActiveRules load(ActiveRulesLoader loader, QualityProfiles qProfiles) { Collection<String> qProfileKeys = getKeys(qProfiles); Set<RuleKey> loadedRulesKey = new HashSet<>(); ActiveRulesBuilder builder = new ActiveRulesBuilder(); for (String qProfileKey : qProfileKeys) { Collection<LoadedActiveRule> qProfileRules = load(loader, qProfileKey); for (LoadedActiveRule r : qProfileRules) { if (!loadedRulesKey.contains(r.getRuleKey())) { loadedRulesKey.add(r.getRuleKey()); builder.addRule(transform(r, qProfileKey)); } } } return builder.build(); }
private SensorContextTester(Path moduleBaseDir) { this.settings = new MapSettings(); this.fs = new DefaultFileSystem(moduleBaseDir).setEncoding(Charset.defaultCharset()); this.activeRules = new ActiveRulesBuilder().build(); this.sensorStorage = new InMemorySensorStorage(); this.project = new DefaultInputProject(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.module = new DefaultInputModule(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.runtime = SonarRuntimeImpl.forSonarQube(ApiVersion.load(System2.INSTANCE), SonarQubeSide.SCANNER); }
@Test public void no_rules() { ActiveRulesBuilder builder = new ActiveRulesBuilder(); ActiveRules rules = builder.build(); assertThat(rules.findAll()).isEmpty(); }
@Before public void prepare() throws Exception { fs = new DefaultFileSystem(temp.newFolder().toPath()); settings = new MapSettings(); optimizer = new ModuleSensorOptimizer(fs, new ActiveRulesBuilder().build(), settings.asConfig()); }
@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); hierarchy = new DefaultInputModuleHierarchy(new DefaultInputModule(ProjectDefinition.create() .setWorkDir(temp.newFolder()) .setBaseDir(temp.newFolder()).setKey("foo"))); adaptor = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, analysisMode, sensorStorage, runtime); }
@Test public void should_optimize_on_repository() { DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor() .createIssuesForRuleRepositories("squid"); assertThat(optimizer.shouldExecute(descriptor)).isFalse(); ActiveRules activeRules = new ActiveRulesBuilder() .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("repo1", "foo")).build()) .build(); optimizer = new ModuleSensorOptimizer(fs, activeRules, settings.asConfig()); assertThat(optimizer.shouldExecute(descriptor)).isFalse(); activeRules = new ActiveRulesBuilder() .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("repo1", "foo")).build()) .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "rule")).build()) .build(); optimizer = new ModuleSensorOptimizer(fs, activeRules, settings.asConfig()); assertThat(optimizer.shouldExecute(descriptor)).isTrue(); }
@Test public void testActiveRules() { NewActiveRule activeRule = new NewActiveRule.Builder() .setRuleKey(RuleKey.of("foo", "bar")) .build(); ActiveRules activeRules = new ActiveRulesBuilder().addRule(activeRule).build(); tester.setActiveRules(activeRules); assertThat(tester.activeRules().findAll()).hasSize(1); }
@Test public void fail_to_add_twice_the_same_rule() { ActiveRulesBuilder builder = new ActiveRulesBuilder(); NewActiveRule rule = new NewActiveRule.Builder() .setRuleKey(RuleKey.of("squid", "S0001")) .build(); builder.addRule(rule); thrown.expect(IllegalStateException.class); thrown.expectMessage("Rule 'squid:S0001' is already activated"); builder.addRule(rule); } }
.build(); ActiveRules activeRules = new ActiveRulesBuilder() .addRule(activeRule)
private static ActiveRules load(ActiveRulesLoader loader, QualityProfiles qProfiles) { Collection<String> qProfileKeys = getKeys(qProfiles); Set<RuleKey> loadedRulesKey = new HashSet<>(); ActiveRulesBuilder builder = new ActiveRulesBuilder(); for (String qProfileKey : qProfileKeys) { Collection<LoadedActiveRule> qProfileRules = load(loader, qProfileKey); for (LoadedActiveRule r : qProfileRules) { if (!loadedRulesKey.contains(r.getRuleKey())) { loadedRulesKey.add(r.getRuleKey()); builder.addRule(transform(r, qProfileKey)); } } } return builder.build(); }
private SensorContextTester(Path moduleBaseDir) { this.settings = new MapSettings(); this.fs = new DefaultFileSystem(moduleBaseDir).setEncoding(Charset.defaultCharset()); this.activeRules = new ActiveRulesBuilder().build(); this.sensorStorage = new InMemorySensorStorage(); this.project = new DefaultInputProject(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.module = new DefaultInputModule(ProjectDefinition.create().setKey("projectKey").setBaseDir(moduleBaseDir.toFile()).setWorkDir(moduleBaseDir.resolve(".sonar").toFile())); this.runtime = SonarRuntimeImpl.forSonarQube(ApiVersion.load(System2.INSTANCE), SonarQubeSide.SCANNER); }
private SensorContextTester(File moduleBaseDir) { this.settings = new Settings(); this.fs = new DefaultFileSystem(moduleBaseDir); this.activeRules = new ActiveRulesBuilder().build(); this.analysisMode = new MockAnalysisMode(); this.sensorStorage = new InMemorySensorStorage(); }
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, qProfile.getName()); continue; } Sonarlint.ActiveRules activeRulesFromStorage = storageReader.readActiveRules(qProfileKey); LOG.debug(" * {}: '{}' ({} rules)", language, qProfile.getName(), activeRulesFromStorage.getActiveRulesByKeyMap().size()); for (ActiveRule activeRule : activeRulesFromStorage.getActiveRulesByKeyMap().values()) { createNewActiveRule(builder, activeRule, storageRules, language, rules); } } activeRules = builder.build(); } return activeRules; }
private GoSensor getSensor(String... activeRuleArray) { Set<String> activeRuleSet = new HashSet<>(Arrays.asList(activeRuleArray)); List<Class> ruleClasses = GoChecks.getChecks(); List<String> allKeys = ruleClasses.stream().map(ruleClass -> ((org.sonar.check.Rule) ruleClass.getAnnotations()[0]).key()).collect(Collectors.toList()); ActiveRulesBuilder rulesBuilder = new ActiveRulesBuilder(); allKeys.forEach(key -> { NewActiveRule newActiveRule = rulesBuilder.create(RuleKey.of(GoRulesDefinition.REPOSITORY_KEY, key)); if (activeRuleSet.contains(key)) { newActiveRule.activate(); if (key.equals("S1451")) { newActiveRule.setParam("headerFormat", "some header format"); } } }); ActiveRules activeRules = rulesBuilder.build(); CheckFactory checkFactory = new CheckFactory(activeRules); Checks<Check> checks = checkFactory.create(GoRulesDefinition.REPOSITORY_KEY); checks.addAnnotatedChecks((Iterable) ruleClasses); return new GoSensor(checkFactory, fileLinesContextFactory); }
private static ActiveRules transform(Collection<LoadedActiveRule> loadedRules) { ActiveRulesBuilder builder = new ActiveRulesBuilder(); for (LoadedActiveRule activeRule : loadedRules) { NewActiveRule newActiveRule = builder.create(activeRule.getRuleKey()); newActiveRule.setName(activeRule.getName()); newActiveRule.setSeverity(activeRule.getSeverity()); newActiveRule.setLanguage(activeRule.getLanguage()); newActiveRule.setInternalKey(activeRule.getInternalKey()); newActiveRule.setTemplateRuleKey(activeRule.getTemplateRuleKey()); // load parameters if (activeRule.getParams() != null) { for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) { newActiveRule.setParam(params.getKey(), params.getValue()); } } newActiveRule.activate(); } return builder.build(); }
private ActiveRules load(ProjectRepositories ref) { ActiveRulesBuilder builder = new ActiveRulesBuilder(); for (ActiveRule activeRule : ref.activeRules()) { NewActiveRule newActiveRule = builder.create(RuleKey.of(activeRule.repositoryKey(), activeRule.ruleKey())); newActiveRule.setName(activeRule.name()); newActiveRule.setSeverity(activeRule.severity()); newActiveRule.setLanguage(activeRule.language()); newActiveRule.setInternalKey(activeRule.internalKey()); newActiveRule.setTemplateRuleKey(activeRule.templateRuleKey()); // load parameters for (Entry<String, String> param : activeRule.params().entrySet()) { newActiveRule.setParam(param.getKey(), param.getValue()); } newActiveRule.activate(); } return builder.build(); } }
@Before public void setActiveRules() { context.setActiveRules( new ActiveRulesBuilder() .create(RuleKey.of("PerlCritic", "TestingAndDebugging::RequireUseStrict")).activate() .build() ); }
private StandaloneActiveRules createActiveRules() { ActiveRulesBuilder activeBuilder = new ActiveRulesBuilder(); ActiveRulesBuilder inactiveBuilder = new ActiveRulesBuilder();