Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARGO-94 Get rid of slang-testing #64

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ junit-jupiter = "5.11.4"
slf4j-api = "1.7.36"
xerces = "2.12.2"
awaitility = "4.2.2"
classgraph = "4.8.162"

[libraries]
sonar-plugin-api = { group = "org.sonarsource.api.plugin", name = "sonar-plugin-api", version.ref = "plugin-api" }
sonar-analyzer-commons = { group = "org.sonarsource.analyzer-commons", name = "sonar-analyzer-commons", version.ref = "analyzer-commons" }
sonar-analyzer-test-commons = { group = "org.sonarsource.analyzer-commons", name = "sonar-analyzer-test-commons", version.ref = "analyzer-commons" }
slang-api = { group = "org.sonarsource.slang", name = "slang-api", version.ref = "slang-dependencies" }
slang-checks = { group = "org.sonarsource.slang", name = "slang-checks", version.ref = "slang-dependencies" }
slang-plugin = { group = "org.sonarsource.slang", name = "slang-plugin", version.ref = "slang-dependencies" }
Expand All @@ -29,15 +31,14 @@ sonarlint-core = { group = "org.sonarsource.sonarlint.core", name = "sonarlint-c
sonar-lint-rpc-java-client = { module = "org.sonarsource.sonarlint.core:sonarlint-rpc-java-client", version.ref = "sonarlint" }
sonar-lint-rpc-impl = { module = "org.sonarsource.sonarlint.core:sonarlint-rpc-impl", version.ref = "sonarlint" }
sonar-ws = { group = "org.sonarsource.sonarqube", name = "sonar-ws", version.ref = "sonarqube" }
slang-antlr = { group = "org.sonarsource.slang", name = "slang-antlr", version.ref = "slang-dependencies" }
slang-testing = { group = "org.sonarsource.slang", name = "slang-testing", version.ref = "slang-dependencies" }
mockito-core = { group = "org.mockito", name = "mockito-core", version.ref = "mockito-core" }
assertj-core = { group = "org.assertj", name = "assertj-core", version.ref = "assertj-core" }
junit-jupiter-api = { group = "org.junit.jupiter", name = "junit-jupiter-api", version.ref = "junit-jupiter" }
junit-jupiter-engine = { group = "org.junit.jupiter", name = "junit-jupiter-engine", version.ref = "junit-jupiter" }
slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "slf4j-api" }
xerces = { group = "xerces", name = "xercesImpl", version.ref = "xerces" }
awaitility = { module = "org.awaitility:awaitility", version.ref = "awaitility"}
awaitility = { group = "org.awaitility", name = "awaitility", version.ref = "awaitility"}
classgraph = { group = "io.github.classgraph", name = "classgraph", version.ref = "classgraph"}

[plugins]
shadow = { id = "com.gradleup.shadow", version = "8.3.5" }
4 changes: 2 additions & 2 deletions sonar-go-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ dependencies {

runtimeOnly(files(project.project(":sonar-go-to-slang").buildDir))

testImplementation(libs.slang.antlr)
testImplementation(libs.assertj.core)
testImplementation(libs.mockito.core)
testImplementation(libs.slang.testing)
testImplementation(libs.sonar.analyzer.test.commons)
testImplementation(libs.classgraph)
testImplementation(libs.junit.jupiter.api)
testImplementation(libs.sonar.plugin.api.impl)
testImplementation(libs.sonar.plugin.api.test.fixtures)
Expand Down
141 changes: 140 additions & 1 deletion sonar-go-plugin/src/test/java/org/sonar/go/checks/GoVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,156 @@
*/
package org.sonar.go.checks;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
import javax.annotation.Nullable;
import org.sonar.go.converter.GoConverter;
import org.sonarsource.analyzer.commons.checks.verifier.SingleFileVerifier;
import org.sonarsource.slang.api.ASTConverter;
import org.sonarsource.slang.api.HasTextRange;
import org.sonarsource.slang.api.TextPointer;
import org.sonarsource.slang.api.TextRange;
import org.sonarsource.slang.api.TopLevelTree;
import org.sonarsource.slang.api.Tree;
import org.sonarsource.slang.checks.api.CheckContext;
import org.sonarsource.slang.checks.api.InitContext;
import org.sonarsource.slang.checks.api.SecondaryLocation;
import org.sonarsource.slang.checks.api.SlangCheck;
import org.sonarsource.slang.visitors.TreeContext;
import org.sonarsource.slang.visitors.TreeVisitor;

import static java.nio.charset.StandardCharsets.UTF_8;

public class GoVerifier {
private static final Path BASE_DIR = Paths.get("src", "test", "resources", "checks");
private static final ASTConverter CONVERTER = new GoConverter(Paths.get("build", "tmp").toFile());

public static void verify(String fileName, SlangCheck check) {
org.sonarsource.slang.testing.Verifier.verify(CONVERTER, BASE_DIR.resolve(fileName), check);
verify(CONVERTER, BASE_DIR.resolve(fileName), check);
}

public static void verify(ASTConverter converter, Path path, SlangCheck check) {
createVerifier(converter, path, check).assertOneOrMoreIssues();
}

public static void verifyNoIssue(ASTConverter converter, Path path, SlangCheck check) {
createVerifier(converter, path, check).assertNoIssues();
}

private static SingleFileVerifier createVerifier(ASTConverter converter, Path path, SlangCheck check) {

SingleFileVerifier verifier = SingleFileVerifier.create(path, UTF_8);

String testFileContent = readFile(path);
Tree root = converter.parse(testFileContent, null);

((TopLevelTree) root).allComments()
.forEach(comment -> {
TextPointer start = comment.textRange().start();
verifier.addComment(start.line(), start.lineOffset() + 1, comment.text(), 2, 0);
});

TestContext ctx = new TestContext(verifier, path.getFileName().toString(), testFileContent);
check.initialize(ctx);
ctx.scan(root);

return verifier;
}

private static String readFile(Path path) {
try {
return new String(Files.readAllBytes(path), UTF_8);
} catch (IOException e) {
throw new IllegalStateException("Cannot read " + path, e);
}
}

private static class TestContext extends TreeContext implements InitContext, CheckContext {

private final TreeVisitor<TestContext> visitor;
private final SingleFileVerifier verifier;
private final String filename;
private String testFileContent;

public TestContext(SingleFileVerifier verifier, String filename, String testFileContent) {
this.verifier = verifier;
this.filename = filename;
this.testFileContent = testFileContent;
visitor = new TreeVisitor<>();
}

public void scan(@Nullable Tree root) {
visitor.scan(this, root);
}

@Override
public <T extends Tree> void register(Class<T> cls, BiConsumer<CheckContext, T> consumer) {
visitor.register(cls, (ctx, node) -> consumer.accept(this, node));
}

@Override
public void reportIssue(HasTextRange toHighlight, String message) {
reportIssue(toHighlight, message, Collections.emptyList());
}

@Override
public void reportIssue(HasTextRange toHighlight, String message, SecondaryLocation secondaryLocation) {
reportIssue(toHighlight, message, Collections.singletonList(secondaryLocation));
}

@Override
public String filename() {
return filename;
}

@Override
public String fileContent() {
return testFileContent;
}

@Override
public void reportIssue(TextRange textRange, String message) {
reportIssue(textRange, message, Collections.emptyList(), null);
}

@Override
public void reportIssue(HasTextRange toHighlight, String message, List<SecondaryLocation> secondaryLocations) {
reportIssue(toHighlight, message, secondaryLocations, null);
}

@Override
public void reportIssue(HasTextRange toHighlight, String message, List<SecondaryLocation> secondaryLocations, @Nullable Double gap) {
reportIssue(toHighlight.textRange(), message, secondaryLocations, gap);
}

public void reportFileIssue(String message) {
reportFileIssue(message, null);
}

@Override
public void reportFileIssue(String message, @Nullable Double gap) {
verifier.reportIssue(message).onFile().withGap(gap);
}

private void reportIssue(TextRange textRange, String message, List<SecondaryLocation> secondaryLocations, @Nullable Double gap) {
TextPointer start = textRange.start();
TextPointer end = textRange.end();
SingleFileVerifier.Issue issue = verifier
.reportIssue(message)
.onRange(start.line(), start.lineOffset() + 1, end.line(), end.lineOffset())
.withGap(gap);
secondaryLocations.forEach(secondary -> issue.addSecondary(
secondary.textRange.start().line(),
secondary.textRange.start().lineOffset() + 1,
secondary.textRange.end().line(),
secondary.textRange.end().lineOffset(),
secondary.message));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.go.coverage.GoCoverSensor.Coverage;
import org.sonar.go.coverage.GoCoverSensor.CoverageStat;
import org.sonar.go.coverage.GoCoverSensor.FileCoverage;
import org.sonar.go.coverage.GoCoverSensor.LineCoverage;
import org.sonarsource.slang.testing.ThreadLocalLogTester;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -49,7 +49,7 @@ class GoCoverSensorTest {
static final Path COVERAGE_DIR = Paths.get("src", "test", "resources", "coverage");

@RegisterExtension
public ThreadLocalLogTester logTester = new ThreadLocalLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);

@Test
void test_descriptor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
import org.slf4j.event.Level;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.notifications.AnalysisWarnings;
import org.sonarsource.slang.testing.ThreadLocalLogTester;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

class AbstractReportSensorTest {

@RegisterExtension
public ThreadLocalLogTester logTester = new ThreadLocalLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);

@Test
void report_consumer_logs_io_exception() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.ExternalIssue;
import org.sonar.api.rules.RuleType;
import org.sonarsource.slang.testing.ThreadLocalLogTester;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.go.externalreport.AbstractReportSensor.GENERIC_ISSUE_KEY;
Expand All @@ -45,7 +45,7 @@ void setup() {
}

@RegisterExtension
public ThreadLocalLogTester logTester = new ThreadLocalLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);

@Test
void test_descriptor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.ExternalIssue;
import org.sonar.api.rules.RuleType;
import org.sonarsource.slang.testing.ThreadLocalLogTester;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.go.externalreport.AbstractReportSensor.GENERIC_ISSUE_KEY;
Expand All @@ -44,7 +44,7 @@ void setup() {
}

@RegisterExtension
public ThreadLocalLogTester logTester = new ThreadLocalLogTester();
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);

@Test
void test_descriptor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.ExternalIssue;
import org.sonar.api.rules.RuleType;
import org.sonarsource.slang.testing.ThreadLocalLogTester;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.go.externalreport.AbstractReportSensor.GENERIC_ISSUE_KEY;
Expand All @@ -39,14 +39,14 @@ class GoVetReportSensorTest {

private final List<String> analysisWarnings = new ArrayList<>();

@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);

@BeforeEach
void setup() {
analysisWarnings.clear();
}

@RegisterExtension
public ThreadLocalLogTester logTester = new ThreadLocalLogTester();

@Test
void test_descriptor() {
DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.ExternalIssue;
import org.sonar.api.rules.RuleType;
import org.sonarsource.slang.testing.ThreadLocalLogTester;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.go.externalreport.ExternalLinterSensorHelper.REPORT_BASE_PATH;
Expand All @@ -37,14 +37,14 @@ class GolangCILintReportSensorTest {

private final List<String> analysisWarnings = new ArrayList<>();

@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);

@BeforeEach
void setup() {
analysisWarnings.clear();
}

@RegisterExtension
public ThreadLocalLogTester logTester = new ThreadLocalLogTester();

@Test
void test_descriptor() {
DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
*/
package org.sonar.go.plugin;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ScanResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.sonarsource.slang.testing.PackageScanner;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

class GoCheckListTest {

Expand All @@ -36,7 +40,7 @@ void go_checks_size() {
@Test
void go_specific_checks_are_added_to_check_list() {
List<String> checkListNames = GoCheckList.checks().stream().map(Class::getName).collect(Collectors.toList());
List<String> languageImplementation = PackageScanner.findSlangChecksInPackage(GO_CHECKS_PACKAGE);
List<String> languageImplementation = findSlangChecksInPackage(GO_CHECKS_PACKAGE);
for (String languageCheck : languageImplementation) {
assertThat(checkListNames).contains(languageCheck);
assertThat(languageCheck).endsWith("GoCheck");
Expand All @@ -50,4 +54,24 @@ void go_excluded_not_present() {
assertThat(checks).doesNotContain(excluded);
}
}

/**
* Returns the fully qualified names (FQNs) of the classes inside @packageName implementing SlangCheck.
* @param packageName Used to filter classes - the FQN of a class contains the package name.
* @return A list of slang checks (FQNs).
*/
private static List<String> findSlangChecksInPackage(String packageName) {
try (ScanResult scanResult = new ClassGraph().enableAllInfo().acceptPackages(packageName).scan()) {
Map<String, ClassInfo> allClasses = scanResult.getAllClassesAsMap();
List<String> testClassesInPackage = new ArrayList<>();
for (Map.Entry<String, ClassInfo> classInfoEntry : allClasses.entrySet()) {
String name = classInfoEntry.getKey();
ClassInfo classInfo = classInfoEntry.getValue();
if (name.startsWith(packageName) && classInfo.getInterfaces().stream().anyMatch(i -> i.getSimpleName().equals("SlangCheck"))) {
testClassesInPackage.add(classInfo.getName());
}
}
return testClassesInPackage;
}
}
}
Loading
Loading