Skip to content

Commit 5fea3e3

Browse files
authored
Accept '1' as a truthy value for boolean parameters (#186)
Fixes #185
1 parent 384163a commit 5fea3e3

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Build Parameters Gradle plugin - Changelog
22

33
## Version 1.4.4
4+
* [New] [#185](https://github.com/gradlex-org/build-parameters/issues/185) String '1' is accepted as a truthy value for boolean parameters.
45
* [Fixed] [#88](https://github.com/gradlex-org/build-parameters/issues/88) Example for boolean parameters with default false should be setting it to true. Thanks to @timyates.
56

67
## Version 1.4.3

src/docs/asciidoc/index.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ include::{samples-path}/defining-parameters/kotlin/gradle/plugins/build-paramete
164164
include::{samples-path}/defining-parameters/groovy/gradle/plugins/build-parameters/build.gradle[tags=boolean-parameter]
165165
----
166166

167-
Boolean parameters can only be set to 'true' or 'false'.
168-
The empty string is also mapped to 'true' so that `-Pmybool` is the same as `-Pmybool=true`.
167+
String values 'true', and '1' are mapped to `true`.
168+
The empty string is also mapped to `true` so that `-Pmybool` is the same as `-Pmybool=true`.
169+
Only the string value 'false' is mapped to `false`.
169170
All other values will lead to an error during build configuration.
170171

171172
==== Enum parameters

src/main/java/org/gradlex/buildparameters/PluginCodeGeneration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ private void generateEnumParseMethod(String typeName, List<String> values, List<
239239

240240
private void generateBooleanParseMethod(BooleanBuildParameter p, List<String> lines) {
241241
lines.add(" private static boolean parse" + p.id.toSimpleTypeName() + "(String p) {");
242-
lines.add(" if (p.isEmpty() || p.equalsIgnoreCase(\"true\")) return true;");
242+
lines.add(" if (p.isEmpty() || p.equalsIgnoreCase(\"true\") || p.equals(\"1\")) return true;");
243243
lines.add(" if (p.equalsIgnoreCase(\"false\")) return false;");
244-
lines.add(" throw new RuntimeException(\"Value '\" + p + \"' for parameter '" + p.id.toPropertyPath() + "' is not a valid boolean value - use 'true' (or '') / 'false'\");");
244+
lines.add(" throw new RuntimeException(\"Value '\" + p + \"' for parameter '" + p.id.toPropertyPath() + "' is not a valid boolean value. Allowed values are strings 'true', '1', and empty string for true, and string 'false' for false\");");
245245
lines.add(" }");
246246
}
247247

src/test/groovy/org/gradlex/buildparameters/GeneratedPluginFuncTest.groovy

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class GeneratedPluginFuncTest extends Specification {
157157
"""
158158

159159
expect:
160-
build("help", "-Pg1.myParameter", "-Pg2.myParameter=false")
160+
build("help", "-Pg1.myParameter=true", "-Pg2.myParameter=false")
161161
}
162162

163163
def "supports boolean build parameters without default value"() {
@@ -182,7 +182,28 @@ class GeneratedPluginFuncTest extends Specification {
182182
"""
183183

184184
expect:
185-
build("help", "-Pg1.myParameter", "-Pg2.myParameter=false")
185+
build("help", "-Pg1.myParameter=true", "-Pg2.myParameter=false")
186+
}
187+
188+
def "supports alternative values for boolean build parameters"() {
189+
given:
190+
buildLogicBuildFile << """
191+
buildParameters {
192+
group("g1") {
193+
bool("myParameter")
194+
}
195+
group("g2") {
196+
bool("myParameter")
197+
}
198+
}
199+
"""
200+
buildFile << """
201+
assert buildParameters.g1.myParameter.get() == true
202+
assert buildParameters.g2.myParameter.get() == true
203+
"""
204+
205+
expect: "empty string and '1' are also mapped to true"
206+
build("help", "-Pg1.myParameter", "-Pg2.myParameter=1")
186207
}
187208

188209
def "using an unknown value for a boolean parameter leads to an error"() {
@@ -201,7 +222,7 @@ class GeneratedPluginFuncTest extends Specification {
201222
def result = buildAndFail("help", "-Pg1.myParameter=treu")
202223

203224
then:
204-
result.output.contains("Value 'treu' for parameter 'g1.myParameter' is not a valid boolean value - use 'true' (or '') / 'false'")
225+
result.output.contains("Value 'treu' for parameter 'g1.myParameter' is not a valid boolean value. Allowed values are strings 'true', '1', and empty string for true, and string 'false' for false")
205226
}
206227

207228
def "supports enum build parameters with default value"() {

0 commit comments

Comments
 (0)