build: test for Flags
Ran into a panic in a Sourcegraph test and I thought this was the
problem. I ended up narrowing the problem down to something Sourcegraph
specific. But in the process wrote this useful test.
Change-Id: I7f2d5d48b840076106995b714d25f8eaa587a652
diff --git a/build/builder_test.go b/build/builder_test.go
new file mode 100644
index 0000000..60c8be4
--- /dev/null
+++ b/build/builder_test.go
@@ -0,0 +1,49 @@
+package build
+
+import (
+ "flag"
+ "testing"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func TestFlags(t *testing.T) {
+ cases := []struct {
+ args []string
+ want Options
+ }{{
+ // Defaults
+ args: []string{},
+ want: Options{},
+ }, {
+ args: []string{"-index", "/tmp"},
+ want: Options{
+ IndexDir: "/tmp",
+ },
+ }, {
+ // single large file pattern
+ args: []string{"-large_file", "*.md"},
+ want: Options{
+ LargeFiles: []string{"*.md"},
+ },
+ }, {
+ // multiple large file pattern
+ args: []string{"-large_file", "*.md", "-large_file", "*.yaml"},
+ want: Options{
+ LargeFiles: []string{"*.md", "*.yaml"},
+ },
+ }}
+
+ for _, c := range cases {
+ c.want.SetDefaults()
+
+ got := Options{}
+ fs := flag.NewFlagSet("", flag.ContinueOnError)
+ got.Flags(fs)
+ if err := fs.Parse(c.args); err != nil {
+ t.Errorf("failed to parse args %v: %v", c.args, err)
+ } else if !cmp.Equal(got, c.want) {
+ t.Errorf("mismatch for %v (-want +got):\n%s", c.args, cmp.Diff(c.want, got))
+ }
+ }
+}