aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test
diff options
context:
space:
mode:
authorJulien Camus <julien.camus@sonarsource.com>2024-12-27 15:22:10 +0100
committersonartech <sonartech@sonarsource.com>2024-12-27 20:03:07 +0000
commit643821403bceec551ad5305d80fd43bc63895e4f (patch)
treeb5523e048ea76741412391be28a6625369afdf76 /sonar-scanner-engine/src/test
parent670f4a2337ceeb3cce78cbff249a5b3ba6f3e319 (diff)
downloadsonarqube-643821403bceec551ad5305d80fd43bc63895e4f.tar.gz
sonarqube-643821403bceec551ad5305d80fd43bc63895e4f.zip
SONAR-24021 Invoke method(s) only conditionally
Diffstat (limited to 'sonar-scanner-engine/src/test')
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageSensorTest.java45
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java31
2 files changed, 75 insertions, 1 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageSensorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageSensorTest.java
index be46d38ab9e..1c7423ba340 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageSensorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageSensorTest.java
@@ -19,19 +19,28 @@
*/
package org.sonar.scanner.genericcoverage;
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.junit.Rule;
import org.junit.Test;
+import org.slf4j.event.Level;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.DefaultFileSystem;
+import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.internal.Encryption;
-import org.sonar.api.utils.System2;
import org.sonar.api.testfixtures.log.LogTester;
+import org.sonar.api.utils.System2;
import org.sonar.scanner.config.DefaultConfiguration;
import org.sonar.scanner.scan.ProjectConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class GenericCoverageSensorTest {
@@ -49,4 +58,38 @@ public class GenericCoverageSensorTest {
assertThat(reportPaths).containsOnly("report.xml", "report2.xml");
}
+
+ @Test
+ public void should_log_info_message_when_unknown_files_exist() throws Exception {
+ logTester.setLevel(Level.INFO);
+ DefaultConfiguration config = mock(DefaultConfiguration.class);
+
+ // Create a temporary file to simulate the report file
+ Path tempReportFile = Files.createTempFile("report", ".xml");
+ // Write valid content with version information to the temporary file
+ Files.write(tempReportFile, "<coverage version=\"1\"><file path=\"unknownFile\"/></coverage>".getBytes());
+
+ // Use the temporary file path in the configuration mock
+ when(config.getStringArray(GenericCoverageSensor.REPORT_PATHS_PROPERTY_KEY)).thenReturn(new String[] {tempReportFile.toString()});
+
+ GenericCoverageSensor sensor = new GenericCoverageSensor(config);
+ SensorContextTester context = SensorContextTester.create(new File("."));
+ DefaultFileSystem fileSystem = context.fileSystem();
+
+ // Mock the input file instead of using DefaultInputFile
+ InputFile inputFile = mock(InputFile.class);
+ when(inputFile.language()).thenReturn("java");
+ when(inputFile.type()).thenReturn(InputFile.Type.MAIN);
+ when(inputFile.filename()).thenReturn("unknownFile");
+
+ fileSystem.add(inputFile);
+
+ sensor.execute(context);
+
+ assertThat(logTester.logs(Level.INFO)).contains("Coverage data ignored for 1 unknown files, including:\nunknownFile");
+
+ // Clean up the temporary file
+ Files.delete(tempReportFile);
+ }
+
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java
index 884253a7e4f..7c49df72d8f 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scm/git/GitScmProviderTest.java
@@ -52,6 +52,7 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.slf4j.event.Level;
import org.sonar.api.config.Configuration;
import org.sonar.api.notifications.AnalysisWarnings;
import org.sonar.api.scan.filesystem.PathResolver;
@@ -246,6 +247,36 @@ public class GitScmProviderTest {
}
@Test
+ public void branchChangedLinesWithFileMovementDetection_should_return_null_when_mergeBaseCommit_is_empty() throws IOException, GitAPIException {
+ logs.setLevel(Level.WARN);
+
+ git.checkout().setOrphan(true).setName("orphan-branch").call();
+ createAndCommitFile("file3.txt");
+
+ Map<Path, ChangedFile> changedFiles = Map.of(
+ worktree.resolve("file1.txt"), ChangedFile.of(worktree.resolve("file1.txt"), null),
+ worktree.resolve("file2-renamed.txt"), ChangedFile.of(worktree.resolve("file2-renamed.txt"), "file2.txt"));
+
+ Map<Path, Set<Integer>> result = newScmProvider().branchChangedLinesWithFileMovementDetection("master", worktree, changedFiles);
+
+ assertThat(logs.logs(Level.WARN)).contains("No merge base found between HEAD and refs/heads/master");
+ assertThat(result).isNull();
+ }
+
+ @Test
+ public void branchChangedFilesWithFileMovementDetection_should_return_null_when_mergeBaseCommit_is_empty() throws IOException, GitAPIException {
+ logs.setLevel(Level.WARN);
+
+ git.checkout().setOrphan(true).setName("orphan-branch").call();
+ createAndCommitFile("file3.txt");
+
+ Set<ChangedFile> result = newScmProvider().branchChangedFilesWithFileMovementDetection("master", worktree);
+
+ assertThat(logs.logs(Level.WARN)).contains("No merge base found between HEAD and refs/heads/master");
+ assertThat(result).isNull();
+ }
+
+ @Test
public void branchChangedFiles_should_not_fail_with_patience_diff_algo() throws IOException {
Path gitConfig = worktree.resolve(".git").resolve("config");
Files.write(gitConfig, "[diff]\nalgorithm = patience\n".getBytes(StandardCharsets.UTF_8));