From 60fee355ef174e184b5c3a477a3c81e95302c754 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 11 Apr 2024 16:40:43 +0200 Subject: [PATCH] SONAR-22054 Make property sonar.projectBaseDir optional --- .../sonar/scanner/scan/ProjectReactorBuilder.java | 12 ++++++++++-- .../scanner/scan/ProjectReactorBuilderTest.java | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java index 858c302e093..3b487e3b208 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorBuilder.java @@ -60,7 +60,6 @@ public class ProjectReactorBuilder { private static final String INVALID_VALUE_OF_X_FOR_Y = "Invalid value of {0} for {1}"; - private static final Logger LOG = Loggers.get(ProjectReactorBuilder.class); @VisibleForTesting @@ -125,7 +124,9 @@ public class ProjectReactorBuilder { Profiler profiler = Profiler.create(LOG).startInfo("Process project properties"); Map> propertiesByModuleIdPath = new HashMap<>(); extractPropertiesByModule(propertiesByModuleIdPath, "", "", new HashMap<>(scannerProps.properties())); - ProjectDefinition rootProject = createModuleDefinition(propertiesByModuleIdPath.get(""), null); + var rootModuleProperties = propertiesByModuleIdPath.get(""); + setBaseDirIfNeeded(rootModuleProperties); + ProjectDefinition rootProject = createModuleDefinition(rootModuleProperties, null); rootProjectWorkDir = rootProject.getWorkDir(); defineChildren(rootProject, propertiesByModuleIdPath, ""); cleanAndCheckProjectDefinitions(rootProject); @@ -133,6 +134,13 @@ public class ProjectReactorBuilder { return new ProjectReactor(rootProject); } + private static void setBaseDirIfNeeded(Map rootModuleProperties) { + var baseDir = rootModuleProperties.get(PROPERTY_PROJECT_BASEDIR); + if (StringUtils.isBlank(baseDir)) { + rootModuleProperties.put(PROPERTY_PROJECT_BASEDIR, Paths.get("").toAbsolutePath().toString()); + } + } + private static void extractPropertiesByModule(Map> propertiesByModuleIdPath, String currentModuleId, String currentModuleIdPath, Map parentProperties) { if (propertiesByModuleIdPath.containsKey(currentModuleIdPath)) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorBuilderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorBuilderTest.java index 7ed5fc9e084..4092d2f7e0f 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorBuilderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorBuilderTest.java @@ -50,6 +50,14 @@ public class ProjectReactorBuilderTest { @Rule public LogTester logTester = new LogTester(); + @Test + public void projectBaseDirDefaultToCurrentDirectory() { + ScannerProperties bootstrapProps = new ScannerProperties(Map.of("sonar.projectKey", "foo")); + ProjectReactor projectReactor = new ProjectReactorBuilder(bootstrapProps, mock(AnalysisWarnings.class)).execute(); + var def = projectReactor.getRoot(); + assertThat(def.getBaseDir()).isEqualTo(new File("").getAbsoluteFile()); + } + @Test public void shouldDefineSimpleProject() { ProjectDefinition projectDefinition = loadProjectDefinition("simple-project"); -- 2.39.5