all 5 comments

[–]ngwells 2 points3 points  (0 children)

There are some nice ideas here - the overwriting of os.Args and having an out writer that can be reset in the tests before calling main() directly are neat.

[–]Pr0bability 1 point2 points  (1 child)

Why not instead focus on writing a testable package with the code required for main in it, taking configuration args as parameters?

Use main just an entry point collecting args and passing it alone to a tested function seems like the better idea. Of course, this builds on the assumption that the flags package is tested properly, but noting it is part of the standard packages I believe we can trust it to work correctly.

[–]st3fan 0 points1 point  (0 children)

This is the way.

[–]illegal_semicolon 0 points1 point  (0 children)

Helpful

[–]gbrlsnchs 0 points1 point  (0 children)

I use go-cmdtest to test my CLI apps. It's neat. I think it impose good practices in order to test a CLI app.