Skip to content

Commit

Permalink
Made the implementation null-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
mockitoguy committed Jan 24, 2024
1 parent 5fc45af commit 3cfe7a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/main/java/org/shipkit/changelog/GenerateChangelogTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 3cfe7a9

Please sign in to comment.