Replace `print` commands without final newline
Opened this issue · 6 comments
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
->println
fmt.Print
->fmt.Println
fmt.Printf(...)
->fmt.Println(fmt.Sprintf(...))
Upstream issue: golang/go#26325
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.
print
would also be included infmt.Sprintf
which we don't want to change, evenprint(
is also part offmt.Sprint
so 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
.