Groovy check array contains string does not work identically with literal string and concat string

Viewed 1155

When you check a literal string within a list or array, it works well. But when you check a concat string within a list or array, it returns incorrect result.

And when compare the literal string and concat string directly, they are considered identical.

I failed at Jenkins pipeline with these code and I was tried on Groovy 1.8.9 directly, the result was the same.

In this code, issue_file = "issue-${signal_dc_key}.json", then contains returns false

def issued_list = ["names":["issue-something-DC-VV.json","issue-else-DC-VV.json"]]

            def signal_dc_key = "something-DC-VV"
            def issue_file = "issue-${signal_dc_key}.json"
            println issue_file                                         // issue-something-DC-VV.json
            println issue_file == "issue-something-DC-VV.json"                  // true
            println issue_file == "issue-${signal_dc_key}.json"                 // true
            println issued_list["names"].contains("${issue_file}")              // false
            println issued_list["names"].contains(issue_file)                   // false
            println issued_list["names"].contains("issue-something-DC-VV.json")   // true

In this code, issue_file = "issue-something-DC-VV.json", contains returns true.

def issued_list = ["names":["issue-something-DC-VV.json","issue-else-DC-VV.json"]]
            def signal_dc_key = "something-DC-VV"
            def issue_file = "issue-something-DC-VV.json"
            println issue_file                                         // issue-something-DC-VV.json
            println issue_file == "issue-something-DC-VV.json"                 // true
            println issue_file == "issue-${signal_dc_key}.json"                  // true
            println issued_list["names"].contains("${issue_file}")              // false
            println issued_list["names"].contains(issue_file)                   // true
            println issued_list["names"].contains("issue-something-DC-VV.json")   // true


1 Answers

It seems like got fixed by changing this line

def issue_file = "issue-${signal_dc_key}.json"

to

def issue_file = new String("issue-${signal_dc_key}.json" )

But new String is NOT permitted in Jenkins.

Thanks to Michael Rutherfurd's comment, toString() works and is permitted in Jenkins.

def issue_file = "issue-${signal_dc_key}.json".toString()
Related