Skip to content
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
2 changes: 1 addition & 1 deletion community-rust-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
import org.sonar.api.issue.impact.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rules.RuleType;
import org.sonarsource.analyzer.commons.ExternalReportProvider;
import org.sonarsource.analyzer.commons.internal.json.simple.parser.ParseException;

Expand Down Expand Up @@ -76,7 +76,9 @@ private void saveIssue(SensorContext context, ClippyJsonReportReader.ClippyIssue
}

var newExternalIssue = context.newExternalIssue();
newExternalIssue.addImpact(SoftwareQuality.MAINTAINABILITY, toSonarQubeSeverity(clippyIssue.severity))
newExternalIssue
.type(RuleType.CODE_SMELL)
.severity(toSonarQubeSeverity(clippyIssue.severity))
.remediationEffortMinutes(DEFAULT_CONSTANT_DEBT_MINUTES);

NewIssueLocation primaryLocation = newExternalIssue.newLocation()
Expand All @@ -97,11 +99,11 @@ private void saveIssue(SensorContext context, ClippyJsonReportReader.ClippyIssue
newExternalIssue.save();
}

private static Severity toSonarQubeSeverity(String severity) {
private static org.sonar.api.batch.rule.Severity toSonarQubeSeverity(String severity) {
if ("error".equalsIgnoreCase(severity)) {
return Severity.HIGH;
return Severity.MAJOR;
} else
return Severity.MEDIUM;
return Severity.MINOR;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
*/
package org.elegoff.plugins.communityrust.coverage.lcov;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.coverage.NewCoverage;

import javax.annotation.CheckForNull;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -39,6 +32,12 @@
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.coverage.NewCoverage;


import static org.elegoff.plugins.communityrust.coverage.lcov.LCOVFields.BRDA;
Expand Down Expand Up @@ -144,7 +143,7 @@ private void parseLineCoverage(FileContent fileContent, int linum, String line)
}

private void logMismatch(String dataType, int linum, Exception e) {
LOG.debug("Error while parsing LCOV report: can't save {} data for line {} of coverage report file ({}).", dataType, linum, e);
LOG.debug("Error while parsing LCOV report: can't save {} data for line {} of coverage report file ({}).", dataType, linum, e.toString());
pbCount++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
import org.sonar.rust.RustFile;


import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class SonarQubeRustFileTest {

private final InputFile inputFile = mock(InputFile.class);
private final InputFile inputFile = mock(InputFile.class, "file1.rs");

@Rule
ExpectedException thrown = ExpectedException.none();
Expand All @@ -55,8 +56,7 @@ void knowFile() throws Exception {
void unknownFile() throws Exception {
when(inputFile.contents()).thenThrow(new FileNotFoundException());
RustFile rustFile = SonarQubeRustFile.create(inputFile);
thrown.expect(IllegalStateException.class);
rustFile.content();
assertThatThrownBy(rustFile::content).isInstanceOf(IllegalStateException.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.ExternalIssue;
import org.sonar.api.batch.sensor.issue.IssueLocation;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.issue.impact.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rules.RuleType;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.Version;

Expand All @@ -62,7 +62,7 @@ class ClippySensorTest {
private static final ClippySensor clippySensor = new ClippySensor();

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

static void assertNoErrorWarnDebugLogs(LogTesterJUnit5 logTester) {
org.assertj.core.api.Assertions.assertThat(logTester.logs(Level.ERROR)).isEmpty();
Expand Down Expand Up @@ -125,7 +125,8 @@ void issuesDetection() throws IOException {

ExternalIssue first = externalIssues.get(0);
assertThat(first.ruleKey()).hasToString(CLIPPY_UNUSED);
assertThat(first.impacts()).containsEntry(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM);
assertThat(first.type()).isEqualTo(RuleType.CODE_SMELL);
assertThat(first.severity()).isEqualTo(Severity.MINOR);
IssueLocation firstPrimaryLoc = first.primaryLocation();
assertThat(firstPrimaryLoc.inputComponent().key()).isEqualTo(CLIPPY_FILE);
assertThat(firstPrimaryLoc.message())
Expand All @@ -138,7 +139,8 @@ void issuesDetection() throws IOException {

ExternalIssue second = externalIssues.get(1);
assertThat(second.ruleKey()).hasToString("external_clippy:unused_doc_comments");
assertThat(second.impacts()).containsEntry(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM);
assertThat(second.type()).isEqualTo(RuleType.CODE_SMELL);
assertThat(second.severity()).isEqualTo(Severity.MINOR);
IssueLocation secondPrimaryLoc = second.primaryLocation();
assertThat(secondPrimaryLoc.inputComponent().key()).isEqualTo(CLIPPY_FILE);
assertThat(secondPrimaryLoc.message())
Expand Down Expand Up @@ -175,15 +177,16 @@ void issuesWhenClippyFileHasErrors() throws IOException {
ExternalIssue first = externalIssues.get(0);
assertThat(first.primaryLocation().inputComponent().key()).isEqualTo("clippy-project:main.rs");
assertThat(first.ruleKey()).hasToString(CLIPPY_AEC);
assertThat(first.impacts()).containsEntry(SoftwareQuality.MAINTAINABILITY, Severity.HIGH);
assertThat(first.type()).isEqualTo(RuleType.CODE_SMELL);
assertThat(first.severity()).isEqualTo(Severity.MAJOR);
assertThat(first.primaryLocation().message()).isEqualTo("A message");
assertThat(first.primaryLocation().textRange()).isNull();

assertThat(logTester.logs(Level.ERROR)).isEmpty();
assertThat(onlyOneLogElement(logTester.logs(Level.WARN)))
.startsWith("Failed to resolve 1 file path(s) in Clippy report. No issues imported related to file(s)");
assertThat(logTester.logs(Level.WARN)).hasSize(1);
assertThat(logTester.logs(Level.WARN).get(0)).startsWith("Missing information for ruleKey:'clippy::absurd_extreme_comparisons'");
assertThat(logTester.logs(Level.DEBUG)).hasSize(1);
assertThat(logTester.logs(Level.DEBUG).get(0)).startsWith("Missing information for ruleKey:'clippy::absurd_extreme_comparisons'");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.event.Level;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
Expand All @@ -56,9 +57,9 @@ class CoberturaSensorTest {
private static final String TESTFILE3 = "moduleKey:src/process/init.rs";
private final File moduleBaseDir = new File("src/test/resources/org/elegoff/plugins/communityrust/cobertura").getAbsoluteFile();
@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5();
@RegisterExtension
public TemporaryFolder tmpDir = new TemporaryFolder();
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);
@TempDir
public Path tmpDir;
private SensorContextTester context;
private MapSettings settings;
private CoberturaSensor coberturaSensor;
Expand Down Expand Up @@ -184,14 +185,14 @@ void test_multiple_reports() {
void fail_with_invalid_report() {
settings.setProperty(CommunityRustPlugin.COBERTURA_REPORT_PATHS, "invalid.xml");
IllegalStateException e = Assert.assertThrows(IllegalStateException.class, () -> coberturaSensor.execute(context));
Assertions.assertThat(e.getMessage()).isEqualTo("Unable to compile regular expression: a+*(");
Assertions.assertThat(e.getCause().toString()).contains("Unexpected character");
}

@Test
void fail_with_invalid_eof() {
settings.setProperty(CommunityRustPlugin.COBERTURA_REPORT_PATHS, "wrong_eof.xml");
IllegalStateException e = Assert.assertThrows(IllegalStateException.class, () -> coberturaSensor.execute(context));
Assertions.assertThat(e.getMessage()).isEqualTo("Unable to compile regular expression: a+*(");
Assertions.assertThat(e.getCause().toString()).contains("Unexpected EOF");
}

@Test
Expand All @@ -218,7 +219,8 @@ void sensor_descriptor() {
}

private String generateReportWithAbsPaths() throws Exception {
Path tmpPath = tmpDir.newFolder("sonar-rust").toPath().toAbsolutePath();
Path tmpPath = tmpDir.toAbsolutePath().resolve("rust");
Files.createDirectory(tmpPath);
String reportName = "unresolved.xml";

String absoluteSourcePath = new File(moduleBaseDir, TESTFILE1).getAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ class LCOVSensorTest {
private static final String TWO_REPORTS = REPORT1 + ", " + REPORT2;
@TempDir
static Path tmpDir;
static Path tmpFile;

private final LCOVSensor coverageSensor = new LCOVSensor();
private final File moduleBaseDir = new File("src/test/resources/lcov/").getAbsoluteFile();
@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5();
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);
private SensorContextTester context;
private MapSettings settings;

Expand Down Expand Up @@ -126,7 +126,7 @@ void should_ignore_and_log_warning_for_invalid_line() {
assertThat(context.conditions("moduleKey:file1.rs", 2)).isEqualTo(3);
assertThat(context.coveredConditions("moduleKey:file1.rs", 2)).isEqualTo(1);

assertThat(logTester.logs()).contains(
assertThat(logTester.logs(Level.DEBUG)).contains(
"Error while parsing LCOV report: can't save DA data for line 3 of coverage report file (java.lang.IllegalArgumentException: Line number 0 doesn't exist in file file1.rs).");

assertThat(logTester.logs()).contains(
Expand All @@ -139,8 +139,7 @@ void test_unresolved_path() {
coverageSensor.execute(context);
String fileName = File.separator + "reports" + File.separator + "report_with_unresolved_path.lcov";
assertThat(logTester.logs(Level.WARN))
.contains("Could not resolve 2 file paths in [" + moduleBaseDir.getAbsolutePath() + fileName + "]")
.contains("First unresolved path: unresolved/file1.rs (Run in DEBUG mode to get full list of unresolved paths)");
.contains("Could not resolve 2 file paths in [" + moduleBaseDir.getAbsolutePath() + fileName + "]");
}

@Test
Expand Down Expand Up @@ -210,11 +209,11 @@ void should_resolve_relative_path() throws Exception {

@Test
void should_resolve_absolute_path() throws Exception {
File lcovFile = Files.createFile(tmpDir.resolve("test.txt")).toFile();
Path lcovFile = Files.createFile(tmpDir.resolve("lcovfile"));
String absolutePathFile1 = new File("src/test/resources/lcov/file1.rs").getAbsolutePath();
String absolutePathFile2 = new File("src/test/resources/lcov/file2.rs").getAbsolutePath();

Files.write(lcovFile.toPath(),
Files.write(lcovFile,
("SF:" + absolutePathFile1 + "\n" +
"DA:1,2\n" +
"DA:2,2\n" +
Expand All @@ -230,7 +229,7 @@ void should_resolve_absolute_path() throws Exception {
"DA:1,5\n" +
"DA:2,5\n" +
"end_of_record\n").getBytes(StandardCharsets.UTF_8));
settings.setProperty(CommunityRustPlugin.LCOV_REPORT_PATHS, lcovFile.getAbsolutePath());
settings.setProperty(CommunityRustPlugin.LCOV_REPORT_PATHS, lcovFile.toAbsolutePath().toString());
inputFile("file1.rs", InputFile.Type.MAIN);
inputFile("file2.rs", InputFile.Type.MAIN);
coverageSensor.execute(context);
Expand Down
Loading