go-test-runner icon indicating copy to clipboard operation
go-test-runner copied to clipboard

Replace `print` commands without final newline

Open tehsphinx opened this issue 5 years ago • 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(...))

tehsphinx avatar Jan 03 '20 13:01 tehsphinx

Upstream issue: golang/go#26325

mhutter avatar Oct 27 '21 08:10 mhutter

@mhutter Thanks a lot for finding the correct issues in the Go repo!

junedev avatar Oct 27 '21 10:10 junedev

@junedev - is this something we still want to do?

jmrunkle avatar Dec 06 '21 03:12 jmrunkle

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.

junedev avatar Dec 06 '21 04:12 junedev

I am pondering some edge cases of the replacement logic mentioned above.

  • print would also be included in fmt.Sprintf which we don't want to change, even print( is also part of fmt.Sprint so to do this correctly we would also need something like "no character before the print( 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

junedev avatar Feb 09 '22 21:02 junedev

This seems to be fixed with go 1.20 for --short --json. Still a problem with -v --json.

TheFranconianCoder avatar Mar 17 '23 13:03 TheFranconianCoder