From d01c1c908f215493472a37762ab969728c59be82 Mon Sep 17 00:00:00 2001 From: Luna Nova Date: Wed, 29 Mar 2023 00:15:44 -0500 Subject: [PATCH 1/3] Make `workingDir` property internal and only expose the `.git` directory Closes #103 --- .../shipkit/changelog/GenerateChangelogTask.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/shipkit/changelog/GenerateChangelogTask.java b/src/main/java/org/shipkit/changelog/GenerateChangelogTask.java index 164002a..9376d3d 100644 --- a/src/main/java/org/shipkit/changelog/GenerateChangelogTask.java +++ b/src/main/java/org/shipkit/changelog/GenerateChangelogTask.java @@ -139,11 +139,22 @@ public void setOutputFile(File outputFile) { this.outputFile = outputFile; } - @InputDirectory + @Internal public File getWorkingDir() { return workingDir; } + @InputDirectory + @PathSensitive(PathSensitivity.RELATIVE) + public File getGitDir() { + if (workingDir == null) { + return null; + } + else { + return new File(workingDir, ".git"); + } + } + public void setWorkingDir(File workingDir) { this.workingDir = workingDir; } From 5fc45afccf265a62612502d9543a5386ae464704 Mon Sep 17 00:00:00 2001 From: Szczepan Faber Date: Tue, 23 Jan 2024 20:35:50 -0600 Subject: [PATCH 2/3] Added test --- .../org/shipkit/changelog/ChangelogPluginTest.groovy | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/test/groovy/org/shipkit/changelog/ChangelogPluginTest.groovy b/src/test/groovy/org/shipkit/changelog/ChangelogPluginTest.groovy index 98c0a2e..88d8e66 100644 --- a/src/test/groovy/org/shipkit/changelog/ChangelogPluginTest.groovy +++ b/src/test/groovy/org/shipkit/changelog/ChangelogPluginTest.groovy @@ -12,6 +12,13 @@ class ChangelogPluginTest extends Specification { project.plugins.apply(ChangelogPlugin) then: - project.tasks.generateChangelog + GenerateChangelogTask t = project.tasks.generateChangelog + t.gitDir + + when: + t.workingDir = null + + then: + !t.gitDir } } From 3cfe7a9e29d6ad24780c717bead791e3d467d291 Mon Sep 17 00:00:00 2001 From: Szczepan Faber Date: Tue, 23 Jan 2024 21:05:33 -0600 Subject: [PATCH 3/3] Made the implementation null-safe --- .../changelog/GenerateChangelogTask.java | 11 +++++++--- .../changelog/ChangelogPluginTest.groovy | 9 +------- .../GenerateChangelogTaskTest.groovy | 22 +++++++++++++++++++ 3 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 src/test/groovy/org/shipkit/changelog/GenerateChangelogTaskTest.groovy diff --git a/src/main/java/org/shipkit/changelog/GenerateChangelogTask.java b/src/main/java/org/shipkit/changelog/GenerateChangelogTask.java index 9376d3d..5e50d9d 100644 --- a/src/main/java/org/shipkit/changelog/GenerateChangelogTask.java +++ b/src/main/java/org/shipkit/changelog/GenerateChangelogTask.java @@ -144,15 +144,20 @@ public File getWorkingDir() { return workingDir; } + @Optional @InputDirectory @PathSensitive(PathSensitivity.RELATIVE) public File getGitDir() { + return provideGitDir(workingDir); + } + + static File provideGitDir(File workingDir) { if (workingDir == null) { return null; } - else { - return new File(workingDir, ".git"); - } + + File gitDir = new File(workingDir, ".git"); + return gitDir.isDirectory() ? gitDir : null; } public void setWorkingDir(File workingDir) { diff --git a/src/test/groovy/org/shipkit/changelog/ChangelogPluginTest.groovy b/src/test/groovy/org/shipkit/changelog/ChangelogPluginTest.groovy index 88d8e66..98c0a2e 100644 --- a/src/test/groovy/org/shipkit/changelog/ChangelogPluginTest.groovy +++ b/src/test/groovy/org/shipkit/changelog/ChangelogPluginTest.groovy @@ -12,13 +12,6 @@ class ChangelogPluginTest extends Specification { project.plugins.apply(ChangelogPlugin) then: - GenerateChangelogTask t = project.tasks.generateChangelog - t.gitDir - - when: - t.workingDir = null - - then: - !t.gitDir + project.tasks.generateChangelog } } diff --git a/src/test/groovy/org/shipkit/changelog/GenerateChangelogTaskTest.groovy b/src/test/groovy/org/shipkit/changelog/GenerateChangelogTaskTest.groovy new file mode 100644 index 0000000..49388a5 --- /dev/null +++ b/src/test/groovy/org/shipkit/changelog/GenerateChangelogTaskTest.groovy @@ -0,0 +1,22 @@ +package org.shipkit.changelog + +import org.junit.Rule +import org.junit.rules.TemporaryFolder +import spock.lang.Specification + +import static org.shipkit.changelog.GenerateChangelogTask.provideGitDir + +class GenerateChangelogTaskTest extends Specification { + + @Rule TemporaryFolder tmp = new TemporaryFolder() + + def "getGitDir is safe"() { + expect: + provideGitDir(null) == null + provideGitDir(new File("missing")) == null + + and: + def gitDir = tmp.newFolder(".git") + provideGitDir(gitDir.parentFile) == gitDir + } +}