Skip to content

Commit c6008ff

Browse files
Updated CsvReport.execute to support a project directory beneath the Git root, preserving the subproject analysis scope.
1 parent 7ac2d5c commit c6008ff

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎refactor-first-gradle-plugin/src/test/java/org/hjug/gradlereport/RefactorFirstPluginFunctionalTest.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,48 @@ void csvReportFileNameUsesProjectVersionSetAfterPluginsBlock() {
207207
"Expected exactly one non-empty RefFirst_P*_PV1.2.3_PD*.csv in " + reportsDir);
208208
}
209209

210+
/**
211+
* Multi-project build: the Git repository root is the root project, but the plugin is
212+
* applied (and the task invoked) on a subproject beneath it. The CSV report must be
213+
* written to the subproject's build directory even though projectDir != Git root.
214+
*/
215+
@Test
216+
void csvReportWritesForSubprojectBeneathGitRoot() throws Exception {
217+
Files.writeString(
218+
projectDir.toPath().resolve("settings.gradle.kts"),
219+
"rootProject.name = \"sample\"\ninclude(\"service\")\n");
220+
221+
Path serviceDir = projectDir.toPath().resolve("service");
222+
Files.createDirectories(serviceDir);
223+
Files.writeString(serviceDir.resolve("build.gradle.kts"), "plugins { id(\"org.hjug.refactorfirst\") }\n");
224+
225+
Path source = serviceDir.resolve(Path.of("src", "main", "java", "com", "example", "Service.java"));
226+
Files.createDirectories(source.getParent());
227+
Files.writeString(
228+
source,
229+
"""
230+
package com.example;
231+
232+
public class Service {
233+
public String serve() {
234+
return "served";
235+
}
236+
}
237+
""");
238+
commitAll(projectDir, "Add service subproject");
239+
240+
BuildResult result = runGradle(projectDir, ":service:refactorFirstCsvReport");
241+
assertNotNull(result.task(":service:refactorFirstCsvReport"));
242+
243+
File reportsDir = new File(serviceDir.toFile(), "build/reports/refactorfirst");
244+
assertTrue(reportsDir.isDirectory(), "Expected reports directory at " + reportsDir);
245+
File[] csvFiles = reportsDir.listFiles((dir, name) -> name.matches("RefFirst_P.*_PV.*_PD.*\\.csv"));
246+
assertNotNull(csvFiles);
247+
assertTrue(
248+
csvFiles.length == 1 && csvFiles[0].length() > 0,
249+
"Expected exactly one non-empty RefFirst CSV in " + reportsDir);
250+
}
251+
210252
/** T12: extension overrides are honored by the HTML report task. */
211253
@Test
212254
void extensionOverridesAreHonored() throws Exception {

‎report/src/main/java/org/hjug/refactorfirst/report/CsvReport.java‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,28 @@ public void execute(
7171
}
7272

7373
String parentOfGitDir = gitDir.getParentFile().getPath();
74+
Path gitRootPath = gitDir.getParentFile().toPath().toAbsolutePath().normalize();
75+
Path projectBaseDirPath = Path.of(projectBaseDir).toAbsolutePath().normalize();
76+
7477
log.info("Project Base Dir: {} ", projectBaseDir);
7578
log.info("Parent of Git Dir: {}", parentOfGitDir);
7679

77-
if (!projectBaseDir.equals(parentOfGitDir)) {
78-
log.warn("Project Base Directory does not match Git Parent Directory");
79-
contentBuilder.append("Project Base Directory does not match Git Parent Directory. "
80-
+ "Please refer to the report at the root of the site directory.");
80+
if (!projectBaseDirPath.startsWith(gitRootPath)) {
81+
log.warn("Project Base Directory {} is not within Git repository root {}", projectBaseDir, parentOfGitDir);
82+
contentBuilder.append("Project Base Directory is not within Git repository root "
83+
+ parentOfGitDir
84+
+ ". Please refer to the report at the root of the site directory.");
85+
writeReportToDisk(outputDirectory, filename, contentBuilder.toString());
8186
return;
8287
}
8388

89+
if (!projectBaseDirPath.equals(gitRootPath)) {
90+
log.info(
91+
"Project Base Directory {} is beneath the Git repository root; "
92+
+ "analyzing sources within the project directory only.",
93+
projectBaseDir);
94+
}
95+
8496
// actual calcualte
8597
List<RankedDisharmony> rankedDisharmonies;
8698
// TODO: revisit

0 commit comments

Comments
 (0)