Skip to content

Commit

Permalink
Accept '1' as a truthy value for boolean parameters (#186)
Browse files Browse the repository at this point in the history
Fixes #185
  • Loading branch information
britter authored Apr 5, 2024
1 parent 384163a commit 5fea3e3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Build Parameters Gradle plugin - Changelog

## Version 1.4.4
* [New] [#185](https://github.com/gradlex-org/build-parameters/issues/185) String '1' is accepted as a truthy value for boolean parameters.
* [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.

## Version 1.4.3
Expand Down
5 changes: 3 additions & 2 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ include::{samples-path}/defining-parameters/kotlin/gradle/plugins/build-paramete
include::{samples-path}/defining-parameters/groovy/gradle/plugins/build-parameters/build.gradle[tags=boolean-parameter]
----

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

==== Enum parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ private void generateEnumParseMethod(String typeName, List<String> values, List<

private void generateBooleanParseMethod(BooleanBuildParameter p, List<String> lines) {
lines.add(" private static boolean parse" + p.id.toSimpleTypeName() + "(String p) {");
lines.add(" if (p.isEmpty() || p.equalsIgnoreCase(\"true\")) return true;");
lines.add(" if (p.isEmpty() || p.equalsIgnoreCase(\"true\") || p.equals(\"1\")) return true;");
lines.add(" if (p.equalsIgnoreCase(\"false\")) return false;");
lines.add(" throw new RuntimeException(\"Value '\" + p + \"' for parameter '" + p.id.toPropertyPath() + "' is not a valid boolean value - use 'true' (or '') / 'false'\");");
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\");");
lines.add(" }");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class GeneratedPluginFuncTest extends Specification {
"""

expect:
build("help", "-Pg1.myParameter", "-Pg2.myParameter=false")
build("help", "-Pg1.myParameter=true", "-Pg2.myParameter=false")
}

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

expect:
build("help", "-Pg1.myParameter", "-Pg2.myParameter=false")
build("help", "-Pg1.myParameter=true", "-Pg2.myParameter=false")
}

def "supports alternative values for boolean build parameters"() {
given:
buildLogicBuildFile << """
buildParameters {
group("g1") {
bool("myParameter")
}
group("g2") {
bool("myParameter")
}
}
"""
buildFile << """
assert buildParameters.g1.myParameter.get() == true
assert buildParameters.g2.myParameter.get() == true
"""

expect: "empty string and '1' are also mapped to true"
build("help", "-Pg1.myParameter", "-Pg2.myParameter=1")
}

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

then:
result.output.contains("Value 'treu' for parameter 'g1.myParameter' is not a valid boolean value - use 'true' (or '') / 'false'")
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")
}

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

0 comments on commit 5fea3e3

Please sign in to comment.