简体   繁体   中英

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

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


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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM