Skip to content

Commit

Permalink
Fix cirrus task
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-pilastri-sonarsource committed Jun 24, 2024
1 parent 416ffff commit 2abeb3c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
6 changes: 3 additions & 3 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ quickfixes_task:
<<: *ONLY_SONARSOURCE_QA
eks_container:
<<: *CONTAINER_DEFINITION
image: ${CIRRUS_AWS_ACCOUNT}.dkr.ecr.eu-central-1.amazonaws.com/base:j17-latest
cpu: 14
memory: 6G
image: ${CIRRUS_AWS_ACCOUNT}.dkr.ecr.eu-central-1.amazonaws.com/base:j21-latest
cpu: 4
memory: 2G
maven_cache:
folder: ${CIRRUS_WORKING_DIR}/.m2/repository
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.List;
import java.util.stream.Stream;
import org.sonar.check.Rule;
import org.sonar.plugins.java.CheckList;
import org.sonar.java.GeneratedCheckList;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonar.plugins.java.api.JavaFileScanner;

Expand All @@ -39,7 +39,7 @@ public class ChecksListWithQuickFix {
static {

QUICKFIX_KEYS = new ArrayList<>();
try(Stream<Path> paths = Files.walk(Paths.get(METADATA_FOLDER))) {
try (Stream<Path> paths = Files.walk(Paths.get(METADATA_FOLDER))) {
paths.filter(Files::isRegularFile)
.forEach(path -> {
try {
Expand All @@ -57,7 +57,7 @@ public class ChecksListWithQuickFix {

checks = new ArrayList<>();
List<Class<? extends JavaCheck>> withQuickFixes =
CheckList.getJavaChecks().stream()
GeneratedCheckList.getJavaChecks().stream()
.filter(c -> c.isAnnotationPresent(Rule.class) && hasQuickFixCovered(c.getAnnotation(Rule.class)))
.toList();
for (Class c : withQuickFixes) {
Expand All @@ -69,7 +69,7 @@ public class ChecksListWithQuickFix {
}
}

private static boolean hasQuickFixCovered(Rule rule){
private static boolean hasQuickFixCovered(Rule rule) {
return QUICKFIX_KEYS.contains(rule.key());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.sonar.api.config.Configuration;
import org.sonar.java.SonarComponents;
import org.sonar.java.ast.JavaAstScanner;
import org.sonar.java.checks.verifier.FilesUtils;
import org.sonar.java.checks.verifier.internal.InternalSensorContext;
import org.sonar.java.classpath.ClasspathForMain;
import org.sonar.java.classpath.ClasspathForTest;
Expand All @@ -53,21 +54,20 @@
public class QuickFixesApplier {

private static final Logger LOG = LoggerFactory.getLogger(QuickFixesApplier.class);
private static final String DEFAULT_CLASSPATH_LOCATION = "../../java-checks-test-sources/default/target/test-classpath.txt";
private static final List<File> DEFAULT_CLASSPATH;
private static final Set<Integer> EDITED_LINES = new HashSet<>();

private Map<Path, List<JavaQuickFix>> pathsToQuickfixes;

static {
Path path = Paths.get(DEFAULT_CLASSPATH_LOCATION.replace('/', File.separatorChar));
DEFAULT_CLASSPATH = TestClasspathUtils.loadFromFile(path.toString());
Path path = Paths.get("../" + FilesUtils.DEFAULT_TEST_CLASSPATH_FILE.replace('/', File.separatorChar));
DEFAULT_CLASSPATH = TestClasspathUtils.loadFromFile(path.toAbsolutePath().toString());
}

public void verifyAll(List<InputFile> files) throws IOException {
List<JavaFileScanner> visitors = new ArrayList<>(ChecksListWithQuickFix.checks);
SonarComponents sonarComponents = sonarComponents();
VisitorsBridgeForQuickFixTests visitorsBridge = new VisitorsBridgeForQuickFixTests(visitors, DEFAULT_CLASSPATH, sonarComponents, new JavaVersionImpl(21));
VisitorsBridgeForQuickFixes visitorsBridge = new VisitorsBridgeForQuickFixes(visitors, DEFAULT_CLASSPATH, sonarComponents, new JavaVersionImpl(21));

JavaAstScanner astScanner = new JavaAstScanner(sonarComponents);
astScanner.setVisitorBridge(visitorsBridge);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public class QuickFixesResolutionTest {

private static final Logger LOG = LoggerFactory.getLogger(QuickFixesResolutionTest.class);
private static final JavaVersion version = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION;
private static final JavaVersion VERSION = JParserConfig.MAXIMUM_SUPPORTED_JAVA_VERSION;
private static final Path PROJECT_LOCATION = Paths.get("../../java-checks-test-sources/");

@ClassRule
Expand Down Expand Up @@ -85,7 +85,6 @@ public void testCompilationAfterQuickfixes() throws Exception {
cloneJavaCheckTestSources();
var applier = new QuickFixesApplier();
List<InputFile> files = collectJavaFiles(tmpProjectClone.getRoot().getAbsolutePath());
LOG.info("Starting analysis of {} files", files.size());
applier.verifyAll(files);
LOG.info("Analysis complete with {} quickfixes found", applier.getQuickfixesCount());
assertThat(validateFilesStillParse(files)).isTrue();
Expand Down Expand Up @@ -123,7 +122,7 @@ private static List<InputFile> collectJavaFiles(String directory) {
private static boolean validateFilesStillParse(List<InputFile> inputFiles) {
for (InputFile inputFile : inputFiles) {
try {
JParser.parse(JParserConfig.Mode.FILE_BY_FILE.create(version, List.of()).astParser(), version.toString(), inputFile.filename(), inputFile.contents());
JParser.parse(JParserConfig.Mode.FILE_BY_FILE.create(VERSION, List.of()).astParser(), VERSION.toString(), inputFile.filename(), inputFile.contents());
} catch (IOException ioException) {
LOG.error("Unable to read contents for file {}", inputFile.filename());
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.sonar.java.SonarComponents;
import org.sonar.java.annotations.VisibleForTesting;
import org.sonar.java.model.JavaVersionImpl;
import org.sonar.java.model.Sema;
import org.sonar.java.model.VisitorsBridge;
import org.sonar.java.reporting.JavaQuickFix;
import org.sonar.java.testing.JavaFileScannerContextForTests;
Expand All @@ -41,9 +40,10 @@
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.ModuleScannerContext;
import org.sonar.plugins.java.api.caching.CacheContext;
import org.sonar.plugins.java.api.semantic.Sema;
import org.sonar.plugins.java.api.tree.CompilationUnitTree;

public class VisitorsBridgeForQuickFixTests extends VisitorsBridge {
public class VisitorsBridgeForQuickFixes extends VisitorsBridge {

private final Map<Path, List<JavaQuickFix>> quickFixes = new HashMap<>();

Expand All @@ -53,16 +53,16 @@ public class VisitorsBridgeForQuickFixTests extends VisitorsBridge {


@VisibleForTesting
public VisitorsBridgeForQuickFixTests(JavaFileScanner visitor, SonarComponents sonarComponents) {
public VisitorsBridgeForQuickFixes(JavaFileScanner visitor, SonarComponents sonarComponents) {
this(Collections.singletonList(visitor), Collections.emptyList(), sonarComponents, new JavaVersionImpl());
}

public VisitorsBridgeForQuickFixTests(Iterable<? extends JavaCheck> visitors, @Nullable SonarComponents sonarComponents, JavaVersion javaVersion) {
public VisitorsBridgeForQuickFixes(Iterable<? extends JavaCheck> visitors, @Nullable SonarComponents sonarComponents, JavaVersion javaVersion) {
super(visitors, Collections.emptyList(), sonarComponents, javaVersion);
enableSemantic = false;
}

public VisitorsBridgeForQuickFixTests(Iterable<? extends JavaCheck> visitors, List<File> projectClasspath, @Nullable SonarComponents sonarComponents, JavaVersion javaVersion) {
public VisitorsBridgeForQuickFixes(Iterable<? extends JavaCheck> visitors, List<File> projectClasspath, @Nullable SonarComponents sonarComponents, JavaVersion javaVersion) {
super(visitors, projectClasspath, sonarComponents, javaVersion);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Double getCost() {
return cost > 0 ? (double) cost : null;
}

public static final class TextSpan implements Comparable {
public static final class TextSpan implements Comparable<TextSpan> {
public final int startLine;
public final int startCharacter;
public final int endLine;
Expand Down Expand Up @@ -144,23 +144,20 @@ public boolean equals(Object obj) {
}

@Override
public int compareTo(Object o) {
if(o instanceof TextSpan other){
int result = startLine - other.startLine;
if (result != 0) {
return result;
}
result = startCharacter - other.startCharacter;
if (result != 0) {
return result;
}
result = endLine - other.endLine;
if (result != 0) {
return result;
}
return endCharacter - other.endCharacter;
public int compareTo(TextSpan other) {
int result = startLine - other.startLine;
if (result != 0) {
return result;
}
return 0;
result = startCharacter - other.startCharacter;
if (result != 0) {
return result;
}
result = endLine - other.endLine;
if (result != 0) {
return result;
}
return endCharacter - other.endCharacter;
}
}

Expand Down

0 comments on commit 2abeb3c

Please sign in to comment.