-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.gradle; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
class GitMetadataResolver { | ||
private final boolean isGitInstalled; | ||
|
||
GitMetadataResolver(boolean isGitInstalled) { | ||
this.isGitInstalled = isGitInstalled; | ||
} | ||
|
||
Optional<String> resolve(Supplier<Optional<String>> fromGit) { | ||
return resolve(null, fromGit); | ||
} | ||
|
||
Optional<String> resolve(Supplier<Optional<String>> fromEnv, Supplier<Optional<String>> fromGit) { | ||
Optional<String> resolved = Optional.empty(); | ||
if (fromEnv != null) { | ||
resolved = fromEnv.get(); | ||
if (resolved.isPresent() && resolved.get().isEmpty()) { | ||
resolved = Optional.empty(); | ||
} | ||
} | ||
if (isGitInstalled && fromGit != null && !resolved.isPresent()) { | ||
resolved = fromGit.get(); | ||
} | ||
return resolved; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.gradle | ||
|
||
import spock.lang.Specification | ||
|
||
class GitMetadataResolverTest extends Specification { | ||
def "Resolve from #label"() { | ||
given: | ||
def resolver = new GitMetadataResolver(isGitInstalled) | ||
|
||
when: | ||
def resolved = fromEnv ? resolver.resolve(fromEnv, fromGit) : resolver.resolve(fromGit) | ||
|
||
then: | ||
resolved == expected | ||
|
||
where: | ||
label | isGitInstalled | fromGit | fromEnv | expected | ||
'nothing' | true | null | null | Optional.empty() | ||
'nothing too' | false | null | null | Optional.empty() | ||
'git only' | true | (() -> Optional.of('fromGit')) | null | Optional.of('fromGit') | ||
'git only no CLI' | false | (() -> Optional.of('whatever')) | null | Optional.empty() | ||
'env only' | true | null | (() -> Optional.of('fromEnv')) | Optional.of('fromEnv') | ||
'env, git' | true | (() -> Optional.of('fromGit')) | (() -> Optional.of('fromEnv')) | Optional.of('fromEnv') | ||
'env, git, no CLI' | false | (() -> Optional.of('whatever')) | (() -> Optional.of('fromEnv')) | Optional.of('fromEnv') | ||
'env empty, git' | true | (() -> Optional.of('fromGit')) | (() -> Optional.empty()) | Optional.of('fromGit') | ||
'env empty, git, no CLI' | false | (() -> Optional.of('whatever')) | (() -> Optional.empty()) | Optional.empty() | ||
'env blank, git' | true | (() -> Optional.of('fromGit')) | (() -> Optional.of('')) | Optional.of('fromGit') | ||
'env blank, git, no CLI' | false | (() -> Optional.of('whatever')) | (() -> Optional.of('')) | Optional.empty() | ||
} | ||
} |