|
| 1 | +package pflag |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "flag" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func cmpLists(a, b []string) bool { |
| 12 | + if len(a) != len(b) { |
| 13 | + return false |
| 14 | + } |
| 15 | + for i := range a { |
| 16 | + if a[i] != b[i] { |
| 17 | + return false |
| 18 | + } |
| 19 | + } |
| 20 | + return true |
| 21 | +} |
| 22 | + |
| 23 | +func TestFunc(t *testing.T) { |
| 24 | + var values []string |
| 25 | + fn := func(s string) error { |
| 26 | + values = append(values, s) |
| 27 | + return nil |
| 28 | + } |
| 29 | + |
| 30 | + fset := NewFlagSet("test", ContinueOnError) |
| 31 | + fset.Func("fnflag", "Callback function", fn) |
| 32 | + |
| 33 | + err := fset.Parse([]string{"--fnflag=aa", "--fnflag", "bb"}) |
| 34 | + if err != nil { |
| 35 | + t.Fatal("expected no error; got", err) |
| 36 | + } |
| 37 | + |
| 38 | + expected := []string{"aa", "bb"} |
| 39 | + if !cmpLists(expected, values) { |
| 40 | + t.Fatalf("expected %v, got %v", expected, values) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestFuncP(t *testing.T) { |
| 45 | + var values []string |
| 46 | + fn := func(s string) error { |
| 47 | + values = append(values, s) |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + fset := NewFlagSet("test", ContinueOnError) |
| 52 | + fset.FuncP("fnflag", "f", "Callback function", fn) |
| 53 | + |
| 54 | + err := fset.Parse([]string{"--fnflag=a", "--fnflag", "b", "-fc", "-f=d", "-f", "e"}) |
| 55 | + if err != nil { |
| 56 | + t.Fatal("expected no error; got", err) |
| 57 | + } |
| 58 | + |
| 59 | + expected := []string{"a", "b", "c", "d", "e"} |
| 60 | + if !cmpLists(expected, values) { |
| 61 | + t.Fatalf("expected %v, got %v", expected, values) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestFuncCompat(t *testing.T) { |
| 66 | + // compare behavior with the stdlib 'flag' package |
| 67 | + type FuncFlagSet interface { |
| 68 | + Func(name string, usage string, fn func(string) error) |
| 69 | + Parse([]string) error |
| 70 | + } |
| 71 | + |
| 72 | + unitTestErr := errors.New("unit test error") |
| 73 | + runCase := func(f FuncFlagSet, name string, args []string) (values []string, err error) { |
| 74 | + fn := func(s string) error { |
| 75 | + values = append(values, s) |
| 76 | + if s == "err" { |
| 77 | + return unitTestErr |
| 78 | + } |
| 79 | + return nil |
| 80 | + } |
| 81 | + f.Func(name, "Callback function", fn) |
| 82 | + |
| 83 | + err = f.Parse(args) |
| 84 | + return values, err |
| 85 | + } |
| 86 | + |
| 87 | + t.Run("regular parsing", func(t *testing.T) { |
| 88 | + flagName := "fnflag" |
| 89 | + args := []string{"--fnflag=xx", "--fnflag", "yy", "--fnflag=zz"} |
| 90 | + |
| 91 | + stdFSet := flag.NewFlagSet("std test", flag.ContinueOnError) |
| 92 | + stdValues, err := runCase(stdFSet, flagName, args) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("std flag: expected no error, got %v", err) |
| 95 | + } |
| 96 | + expected := []string{"xx", "yy", "zz"} |
| 97 | + if !cmpLists(expected, stdValues) { |
| 98 | + t.Fatalf("std flag: expected %v, got %v", expected, stdValues) |
| 99 | + } |
| 100 | + |
| 101 | + fset := NewFlagSet("pflag test", ContinueOnError) |
| 102 | + pflagValues, err := runCase(fset, flagName, args) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("pflag: expected no error, got %v", err) |
| 105 | + } |
| 106 | + if !cmpLists(stdValues, pflagValues) { |
| 107 | + t.Fatalf("pflag: expected %v, got %v", stdValues, pflagValues) |
| 108 | + } |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("error triggered by callback", func(t *testing.T) { |
| 112 | + flagName := "fnflag" |
| 113 | + args := []string{"--fnflag", "before", "--fnflag", "err", "--fnflag", "after"} |
| 114 | + |
| 115 | + // test behavior of standard flag.Fset with an error triggere by the callback: |
| 116 | + // (note: as can be seen in 'runCase()', if the callback sees "err" as a value |
| 117 | + // for the bool flag, it will return an error) |
| 118 | + stdFSet := flag.NewFlagSet("std test", flag.ContinueOnError) |
| 119 | + stdFSet.SetOutput(io.Discard) // suppress output |
| 120 | + |
| 121 | + // run test case with standard flag.Fset |
| 122 | + stdValues, err := runCase(stdFSet, flagName, args) |
| 123 | + |
| 124 | + // double check the standard behavior: |
| 125 | + // - .Parse() should return an error, which contains the error message |
| 126 | + if err == nil { |
| 127 | + t.Fatalf("std flag: expected an error triggered by callback, got no error instead") |
| 128 | + } |
| 129 | + if !strings.HasSuffix(err.Error(), unitTestErr.Error()) { |
| 130 | + t.Fatalf("std flag: expected unittest error, got unexpected error value: %T %v", err, err) |
| 131 | + } |
| 132 | + // - the function should have been called twice, with the first two values, |
| 133 | + // the final "=after" should not be recorded |
| 134 | + expected := []string{"before", "err"} |
| 135 | + if !cmpLists(expected, stdValues) { |
| 136 | + t.Fatalf("std flag: expected %v, got %v", expected, stdValues) |
| 137 | + } |
| 138 | + |
| 139 | + // now run the test case on a pflag FlagSet: |
| 140 | + fset := NewFlagSet("pflag test", ContinueOnError) |
| 141 | + pflagValues, err := runCase(fset, flagName, args) |
| 142 | + |
| 143 | + // check that there is a similar error (note: pflag will _wrap_ the error, while the stdlib |
| 144 | + // currently keeps the original message but creates a flat errors.Error) |
| 145 | + if !errors.Is(err, unitTestErr) { |
| 146 | + t.Fatalf("pflag: got unexpected error value: %T %v", err, err) |
| 147 | + } |
| 148 | + // the callback should be called the same number of times, with the same values: |
| 149 | + if !cmpLists(stdValues, pflagValues) { |
| 150 | + t.Fatalf("pflag: expected %v, got %v", stdValues, pflagValues) |
| 151 | + } |
| 152 | + }) |
| 153 | +} |
0 commit comments