Skip to content

Commit

Permalink
Fix cucumber#1228: Added explicit checks for Null on JsonObject
Browse files Browse the repository at this point in the history
This fix works around the limitation of minimal-json, that null as default is not handled (see ralfstx/minimal-json#103 (comment))
  • Loading branch information
Kathrin Geilmann committed Oct 26, 2020
1 parent 3e73553 commit c63f3b2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import io.cucumber.messages.Messages;
import io.cucumber.messages.ProtocolVersion;

Expand Down Expand Up @@ -81,13 +82,13 @@ static Messages.Meta.CI detectCI(Map<String, String> env) {
}

private static Messages.Meta.CI createCi(String name, JsonObject ciSystem, Map<String, String> env) {
String url = evaluate(ciSystem.getString("url", null), env);
String url = evaluate(getString(ciSystem, "url"), env);
if (url == null) return null;
JsonObject git = ciSystem.get("git").asObject();
String remote = removeUserInfoFromUrl(evaluate(git.getString("remote", null), env));
String revision = evaluate(git.getString("revision", null), env);
String branch = evaluate(git.getString("branch", null), env);
String tag = evaluate(git.getString("tag", null), env);
String remote = removeUserInfoFromUrl(evaluate(getString(git, "remote"), env));
String revision = evaluate(getString(git, "revision"), env);
String branch = evaluate(getString(git, "branch"), env);
String tag = evaluate(getString(git, "tag"), env);

Messages.Meta.CI.Builder ciBuilder = Messages.Meta.CI.newBuilder()
.setName(name)
Expand Down Expand Up @@ -144,4 +145,9 @@ private static String group1(String value, Pattern pattern) {
}
return matcher.find() ? matcher.group(1) : null;
}

private static String getString(JsonObject json, String name) {
JsonValue val = json.get(name);
return val.isNull() ? null : val.asString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,21 @@ void it_detects_github_actions() {
meta.getCi());
}

@Test
void can_handle_CIs_with_null_in_ciDict() {
// choose gocd as example, which has null everywhere except for url and revision
HashMap<String, String> env = new HashMap<String, String>() {{
put("GO_SERVER_URL", "https://<cihost>/buildurl");
put("GO_REVISION", "the-revision");
}};
Messages.Meta meta = CreateMeta.createMeta("cucumber-jvm", "3.2.1", env);
assertEquals(Messages.Meta.CI.newBuilder()
.setName("GoCD")
.setUrl("https://<cihost>/buildurl/???")
.setGit(Messages.Meta.CI.Git.newBuilder()
.setRevision("the-revision")
)
.build(),
meta.getCi());
}
}
2 changes: 2 additions & 0 deletions create-meta/testdata/GoCD.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GO_SERVER_URL=https://cihost.com/path/to/the/build
GO_REVISION=decafbad
7 changes: 7 additions & 0 deletions create-meta/testdata/GoCD.txt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"git": {
"revision": "decafbad"
},
"name": "GoCD",
"url": "https://cihost.com/path/to/the/build/???"
}

0 comments on commit c63f3b2

Please sign in to comment.