EsqlSensor implements Sensor { public void execute(SensorContext context) { LOG.warn("ESQL sensor execute"); List<TreeVisitor> treeVisitors = Lists.newArrayList(); treeVisitors.addAll(getTreeVisitors(context)); treeVisitors.addAll(checks.visitorChecks()); for (TreeVisitor check : treeVisitors) { if (check instanceof ParsingErrorCheck) { parsingErrorRuleKey = checks.ruleKeyFor((EsqlCheck) check); break; } } Iterable<InputFile> inputFiles = fileSystem.inputFiles(mainFilePredicate); Collection<String> files = StreamSupport.stream(inputFiles.spliterator(), false) .map(InputFile::toString) .collect(Collectors.toList()); ProgressReport progressReport = new ProgressReport("Report about progress of ESQL analyzer", TimeUnit.SECONDS.toMillis(10)); progressReport.start(files); analyseFiles(context, treeVisitors, inputFiles, progressReport); executeCoverageSensors(context); } EsqlSensor(
            CheckFactory checkFactory, FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem, NoSonarFilter noSonarFilter);  EsqlSensor(
            CheckFactory checkFactory, FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem, NoSonarFilter noSonarFilter,
            @Nullable CustomEsqlRulesDefinition[] customRulesDefinition
    ); @Override void describe(SensorDescriptor descriptor); void execute(SensorContext context); List<TreeVisitor> getTreeVisitors(SensorContext context); void highlightSymbols(InputFile inputFile, TreeVisitorContext treeVisitorContext, SensorContext context); void executeCoverageSensors(SensorContext context);  }
@Test public void should_analyse() { String relativePath = "cpd/Person.esql"; inputFile(relativePath); createSensor().execute(context); String key = "moduleKey:" + relativePath; assertThat(context.measure(key, CoreMetrics.NCLOC).value()).isEqualTo(18); assertThat(context.measure(key, CoreMetrics.FUNCTIONS).value()).isEqualTo(1); assertThat(context.measure(key, CoreMetrics.STATEMENTS).value()).isEqualTo(8); assertThat(context.measure(key, CoreMetrics.COMPLEXITY).value()).isEqualTo(7); assertThat(context.measure(key, CoreMetrics.COMMENT_LINES).value()).isEqualTo(3); }
@Test public void parsing_error() { String relativePath = "cpd/parsingError.esql"; inputFile(relativePath); String parsingErrorCheckKey = "ParsingError"; ActiveRules activeRules = (new ActiveRulesBuilder()) .addRule(new NewActiveRule.Builder().setName("ParsingError").setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, parsingErrorCheckKey)).build()) .build(); checkFactory = new CheckFactory(activeRules); context.setActiveRules(activeRules); createSensor().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(1); Issue issue = issues.iterator().next(); assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(3); assertThat(issue.primaryLocation().message()).isEqualTo("Parse error"); assertThat(context.allAnalysisErrors()).hasSize(1); }
@Test public void analysis_with_no_issue_should_not_add_error_to_context() { inputFile("file.esql"); createSensor().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(0); assertThat(context.allAnalysisErrors()).isEmpty(); }
@Test public void analysis_with_issues_should_not_add_error_to_context() { inputFile("file.esql"); ActiveRules activeRules = (new ActiveRulesBuilder()) .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, "MissingNewlineAtEndOfFile")).build()) .build(); checkFactory = new CheckFactory(activeRules); createSensor().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(1); assertThat(context.allAnalysisErrors()).isEmpty(); }
@Test public void save_issue() throws Exception { inputFile("file.esql"); ActiveRules activeRules = (new ActiveRulesBuilder()) .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, "MissingNewlineAtEndOfFile")).build()) .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, "InitializeVariables")).build()) .build(); checkFactory = new CheckFactory(activeRules); createSensor().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(2); }
@Test public void custom_rule() throws Exception { inputFile("file.esql"); ActiveRules activeRules = (new ActiveRulesBuilder()) .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("customKey", "key")).build()).build(); checkFactory = new CheckFactory(activeRules); createSensorWithCustomRules().execute(context); Collection<Issue> issues = context.allIssues(); assertThat(issues).hasSize(1); Issue issue = issues.iterator().next(); assertThat(issue.gap()).isEqualTo(42); assertThat(issue.primaryLocation().message()).isEqualTo("Message of custom rule"); assertThat(issue.primaryLocation().textRange()) .isEqualTo(new DefaultTextRange(new DefaultTextPointer(1, 0), new DefaultTextPointer(1, 24))); }