go-test-runner
go-test-runner copied to clipboard
Replace `print` commands without final newline
The commands print, fmt.Print, fmt.Printf and maybe others currently destroy the output of go test --json as they have no final newline.
We should report that to be fixed in go test but for the time being we will replace these commands in students solutions with println / fmt.Println.
My current suggestion would be to add some sed commands to the test runner before executing the tests. This could also be done using the AST library to manipulate the AST if someone wants to dig into that.
Here the replace table I'd recommend to avoid having to remove/add imports:
print->printlnfmt.Print->fmt.Printlnfmt.Printf(...)->fmt.Println(fmt.Sprintf(...))
Upstream issue: golang/go#26325
@mhutter Thanks a lot for finding the correct issues in the Go repo!
@junedev - is this something we still want to do?
Imo not a very high priority but in general yes. There is no solution of the uptream issue in sight so the problem described here still exists.
I am pondering some edge cases of the replacement logic mentioned above.
printwould also be included infmt.Sprintfwhich we don't want to change, evenprint(is also part offmt.Sprintso to do this correctly we would also need something like "no character before theprint(as condition as well- to rewrite something like
fmt.Printf("%s)", x)correctly, we would need to know which bracket is the closing bracket of the function call which is hard to tell, maybe we could try to find the string after the opening bracket and add a line break there instead of replacing the whole expression
This seems to be fixed with go 1.20 for --short --json. Still a problem with -v --json.