@@ -193,6 +193,34 @@ class GitVersionPluginTests extends Specification {
193
193
buildResult. output. contains(" :printVersion\n 1.0.0\n " )
194
194
}
195
195
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
+
196
224
def ' git describe when lightweight tag is present' () {
197
225
given :
198
226
buildFile << '''
@@ -701,10 +729,10 @@ class GitVersionPluginTests extends Specification {
701
729
}
702
730
703
731
private GradleRunner with (String ... tasks ) {
704
- return with(Optional . empty(), tasks)
732
+ return with(Optional . empty(), Optional . empty(), tasks)
705
733
}
706
734
707
- private GradleRunner with (Optional<String > gradleVersion , String ... tasks ) {
735
+ private GradleRunner with (Optional<String > gradleVersion , Optional< Map< String , String > > envVars , String ... tasks ) {
708
736
List<String > arguments = new ArrayList<> ([' --stacktrace' ])
709
737
arguments. addAll(tasks)
710
738
@@ -714,6 +742,11 @@ class GitVersionPluginTests extends Specification {
714
742
.withArguments(arguments)
715
743
716
744
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
+ }
717
750
718
751
return gradleRunner
719
752
}
0 commit comments