package main import ( "testing" "github.com/stretchr/testify/assert" "os" ) func WithArgs(args []string, test func ()) { old_args := os.Args defer (func() { os.Args = old_args })() os.Args = args test() } func TestParseOpts(t *testing.T) { args := []string{"cat.exe", "-n", "main.go"} WithArgs(args, func () { opts := ParseOpts() assert.Equal(t, opts.Number, true, "-n didn't work") }) /* // This fails because flag can't be run more than once args = []string{"cat.exe", "-s", "main.go"} WithArgs(args, func () { opts := ParseOpts() assert.Equal(t, opts.Squeeze, true, "-s didn't work") }) */ }