There is a job which will run daily to collect some info on Jenkins.
For debug, I will print one ArrayList to Jenkins console for possible further debug.
So I run inspect() to print it, and find it's not working as expected.
If all the elements of an array and keys/values of a map are all quoted, I can directly copy paste the output to somewhere else for debug.
Short demo:
// Code
def hz = "3.90GHz"
def info = [["CPU": "CPU @ ${hz}"], ['HOST_OS':"Debian"]]
println(info.inspect())
// Output
[['CPU':CPU @ 3.90GHz], ['HOST_OS':'Debian']]
I find the value of "CPU" is not quoted.
My current fix(Add toString() to the GString):
// Code
def hz = "3.90GHz"
def info = [["CPU": "CPU @ ${hz}".toString()], ['HOST_OS':"Debian"]]
println(info.inspect())
// Output
[['CPU':'CPU @ 3.90GHz'], ['HOST_OS':'Debian']]
I get two questions:
inspect()seems not woking well with GString, is this expected, or a bug? Seems from theInvokerHelperclass, runinspect()on "GString", it will runreturn arguments.toString();, so no quotes.- Is there a better solution, other than adding
toString()to GString?
Thanks in advance.
Thanks for the info from @daggett, find out groovy 3.x and 4.x have different
inspect()methods.In 3.0.11, inside InvokerHelper class, it only checks for String, GString is not checked:
And in 4.0.2 inside FormatHelper class, it checks for
CharSequencewhich contains both String and GString:In general, I think we can consider it as a bug in 3.0.11
inspect(), and the bug does not exist in 4.0.2.