1919package grails.init
2020
2121import spock.lang.Specification
22+ import spock.lang.TempDir
2223import spock.lang.Unroll
2324import uk.org.webcompere.systemstubs.SystemStubs
2425
26+ import java.nio.file.Path
27+
2528class GrailsVersionSpec extends Specification {
2629
30+ @TempDir
31+ Path tempDir
32+
2733 def " allowed release types - specified valid" () {
2834 given :
2935 GrailsWrapper mockVersion = Mock (GrailsWrapper )
3036
3137 when :
3238 Set<GrailsReleaseType > allowedTypes = null
33- SystemStubs . withEnvironmentVariable(' GRAILS_WRAPPER_ALLOWED_TYPES ' , ' SNAPSHOT, RC' ). execute {
39+ SystemStubs . withEnvironmentVariable(GrailsVersion . GRAILS_WRAPPER_ALLOWED_TYPES_ENV , ' SNAPSHOT, RC' ). execute {
3440 allowedTypes = GrailsVersion . getAllowedReleaseTypes(null , mockVersion)
3541 }
3642
@@ -50,13 +56,13 @@ class GrailsVersionSpec extends Specification {
5056
5157 when :
5258 Set<GrailsReleaseType > allowedTypes = null
53- SystemStubs . withEnvironmentVariable(' GRAILS_WRAPPER_ALLOWED_TYPES ' , ' SNAPSHOT, FOO' ). execute {
59+ SystemStubs . withEnvironmentVariable(GrailsVersion . GRAILS_WRAPPER_ALLOWED_TYPES_ENV , ' SNAPSHOT, FOO' ). execute {
5460 allowedTypes = GrailsVersion . getAllowedReleaseTypes(null , mockVersion)
5561 }
5662
5763 then :
5864 def ie = thrown(IllegalStateException )
59- ie. message == ' Invalid Value in GRAILS_WRAPPER_ALLOWED_TYPES : FOO'
65+ ie. message == " Invalid Value in ${ GrailsVersion.GRAILS_WRAPPER_ALLOWED_TYPES_ENV } : FOO"
6066
6167 and :
6268 0 * mockVersion. _
@@ -71,7 +77,7 @@ class GrailsVersionSpec extends Specification {
7177
7278 when :
7379 Set<GrailsReleaseType > allowedTypes = null
74- SystemStubs . withEnvironmentVariable(' GRAILS_WRAPPER_ALLOWED_TYPES ' , ' SNAPSHOT, RC' ). execute {
80+ SystemStubs . withEnvironmentVariable(GrailsVersion . GRAILS_WRAPPER_ALLOWED_TYPES_ENV , ' SNAPSHOT, RC' ). execute {
7581 allowedTypes = GrailsVersion . getAllowedReleaseTypes(preferredVersion, mockVersion)
7682 }
7783
@@ -94,7 +100,7 @@ class GrailsVersionSpec extends Specification {
94100
95101 when :
96102 Set<GrailsReleaseType > allowedTypes = null
97- SystemStubs . withEnvironmentVariable(' GRAILS_WRAPPER_ALLOWED_TYPES ' , null ). execute {
103+ SystemStubs . withEnvironmentVariable(GrailsVersion . GRAILS_WRAPPER_ALLOWED_TYPES_ENV , null ). execute {
98104 allowedTypes = GrailsVersion . getAllowedReleaseTypes(preferredVersion, mockVersion)
99105 }
100106
@@ -116,7 +122,7 @@ class GrailsVersionSpec extends Specification {
116122
117123 when :
118124 Set<GrailsReleaseType > allowedTypes = null
119- SystemStubs . withEnvironmentVariable(' GRAILS_WRAPPER_ALLOWED_TYPES ' , null ). execute {
125+ SystemStubs . withEnvironmentVariable(GrailsVersion . GRAILS_WRAPPER_ALLOWED_TYPES_ENV , null ). execute {
120126 allowedTypes = GrailsVersion . getAllowedReleaseTypes(null , mockVersion)
121127 }
122128
@@ -135,7 +141,7 @@ class GrailsVersionSpec extends Specification {
135141
136142 when :
137143 Set<GrailsReleaseType > allowedTypes = null
138- SystemStubs . withEnvironmentVariable(' GRAILS_WRAPPER_ALLOWED_TYPES ' , null ). execute {
144+ SystemStubs . withEnvironmentVariable(GrailsVersion . GRAILS_WRAPPER_ALLOWED_TYPES_ENV , null ). execute {
139145 allowedTypes = GrailsVersion . getAllowedReleaseTypes(null , mockVersion)
140146 }
141147
@@ -154,7 +160,7 @@ class GrailsVersionSpec extends Specification {
154160
155161 when :
156162 Set<GrailsReleaseType > allowedTypes = null
157- SystemStubs . withEnvironmentVariable(' GRAILS_WRAPPER_ALLOWED_TYPES ' , null ). execute {
163+ SystemStubs . withEnvironmentVariable(GrailsVersion . GRAILS_WRAPPER_ALLOWED_TYPES_ENV , null ). execute {
158164 allowedTypes = GrailsVersion . getAllowedReleaseTypes(null , mockVersion)
159165 }
160166
@@ -165,6 +171,78 @@ class GrailsVersionSpec extends Specification {
165171 allowedTypes == [GrailsReleaseType . RELEASE ] as LinkedHashSet
166172 }
167173
174+ def " preferred version - gradle.properties with grailsVersion wins over env var" () {
175+ given :
176+ File gradleProperties = tempDir. resolve(' gradle.properties' ). toFile()
177+ gradleProperties. text = ' grailsVersion=7.0.11-SNAPSHOT'
178+
179+ when :
180+ GrailsVersion resolved = null
181+ SystemStubs . withEnvironmentVariable(GrailsVersion . PREFERRED_GRAILS_VERSION_ENV , ' 8.0.0-SNAPSHOT' ). execute {
182+ resolved = GrailsVersion . getPreferredGrailsVersion(gradleProperties)
183+ }
184+
185+ then :
186+ resolved == new GrailsVersion (' 7.0.11-SNAPSHOT' )
187+ }
188+
189+ def " preferred version - env var used when gradle.properties is missing" () {
190+ given :
191+ File missing = tempDir. resolve(' does-not-exist.properties' ). toFile()
192+
193+ when :
194+ GrailsVersion resolved = null
195+ SystemStubs . withEnvironmentVariable(GrailsVersion . PREFERRED_GRAILS_VERSION_ENV , ' 7.0.11-SNAPSHOT' ). execute {
196+ resolved = GrailsVersion . getPreferredGrailsVersion(missing)
197+ }
198+
199+ then :
200+ resolved == new GrailsVersion (' 7.0.11-SNAPSHOT' )
201+ }
202+
203+ def " preferred version - env var used when gradle.properties has no grailsVersion key" () {
204+ given :
205+ File gradleProperties = tempDir. resolve(' gradle.properties' ). toFile()
206+ gradleProperties. text = ' projectVersion=7.0.11-SNAPSHOT'
207+
208+ when :
209+ GrailsVersion resolved = null
210+ SystemStubs . withEnvironmentVariable(GrailsVersion . PREFERRED_GRAILS_VERSION_ENV , ' 7.0.11-SNAPSHOT' ). execute {
211+ resolved = GrailsVersion . getPreferredGrailsVersion(gradleProperties)
212+ }
213+
214+ then :
215+ resolved == new GrailsVersion (' 7.0.11-SNAPSHOT' )
216+ }
217+
218+ def " preferred version - env var trimmed and whitespace-only treated as unset" () {
219+ given :
220+ File missing = tempDir. resolve(' does-not-exist.properties' ). toFile()
221+
222+ when :
223+ GrailsVersion resolved = null
224+ SystemStubs . withEnvironmentVariable(GrailsVersion . PREFERRED_GRAILS_VERSION_ENV , ' ' ). execute {
225+ resolved = GrailsVersion . getPreferredGrailsVersion(missing)
226+ }
227+
228+ then :
229+ resolved == null
230+ }
231+
232+ def " preferred version - returns null when neither gradle.properties nor env var is set" () {
233+ given :
234+ File missing = tempDir. resolve(' does-not-exist.properties' ). toFile()
235+
236+ when :
237+ GrailsVersion resolved = null
238+ SystemStubs . withEnvironmentVariable(GrailsVersion . PREFERRED_GRAILS_VERSION_ENV , null ). execute {
239+ resolved = GrailsVersion . getPreferredGrailsVersion(missing)
240+ }
241+
242+ then :
243+ resolved == null
244+ }
245+
168246 @Unroll
169247 def " grails version #version" (String version, String major, String minor, String patch, GrailsReleaseType releaseType, Integer candidate) {
170248 when :
0 commit comments