@@ -193,6 +193,34 @@ class GitVersionPluginTests extends Specification {
193193 buildResult. output. contains(" :printVersion\n 1.0.0\n " )
194194 }
195195
196+ def ' gitVersion() uses GIT_VERSION environment variable if it is set' () {
197+ given :
198+ buildFile << '''
199+ plugins {
200+ id 'com.palantir.git-version'
201+ }
202+ version gitVersion()
203+ ''' . stripIndent()
204+ gitIgnoreFile << ' build'
205+ Git git = new Git (projectDir, true )
206+ git. runGitCommand(" init" , projectDir. toString())
207+ git. runGitCommand(" add" , " ." )
208+ git. runGitCommand(" commit" , " -m" , " 'initial commit'" )
209+ git. runGitCommand(" tag" , " -a" , " 1.0.0" , " -m" , " 1.0.0" )
210+
211+ when :
212+ BuildResult normalResult = with(' printVersion' ). build()
213+
214+ then :
215+ normalResult. output. contains(" :printVersion\n 1.0.0\n " )
216+
217+ when :
218+ BuildResult overriddenResult = with(Optional . empty(), Optional . of(Map . of(" GIT_VERSION" , " 999" )),' printVersion' ). build()
219+
220+ then :
221+ overriddenResult. output. contains(" :printVersion\n 999\n " )
222+ }
223+
196224 def ' git describe when lightweight tag is present' () {
197225 given :
198226 buildFile << '''
@@ -701,10 +729,10 @@ class GitVersionPluginTests extends Specification {
701729 }
702730
703731 private GradleRunner with (String ... tasks ) {
704- return with(Optional . empty(), tasks)
732+ return with(Optional . empty(), Optional . empty(), tasks)
705733 }
706734
707- private GradleRunner with (Optional<String > gradleVersion , String ... tasks ) {
735+ private GradleRunner with (Optional<String > gradleVersion , Optional< Map< String , String > > envVars , String ... tasks ) {
708736 List<String > arguments = new ArrayList<> ([' --stacktrace' ])
709737 arguments. addAll(tasks)
710738
@@ -714,6 +742,11 @@ class GitVersionPluginTests extends Specification {
714742 .withArguments(arguments)
715743
716744 gradleVersion. ifPresent({ version -> gradleRunner. withGradleVersion(version) })
745+ envVars. ifPresent {env ->
746+ Map<String , String > systemEnv = new HashMap<> (System . getenv())
747+ systemEnv. putAll(env)
748+ gradleRunner. withEnvironment(systemEnv)
749+ }
717750
718751 return gradleRunner
719752 }
0 commit comments