github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/cfg/cli_provider_test.go (about) 1 package cfg 2 3 import ( 4 "fmt" 5 "reflect" 6 "sort" 7 "strings" 8 "testing" 9 10 "github.com/awesome-flow/flow/pkg/types" 11 ) 12 13 func TestCliProviderSetUp(t *testing.T) { 14 tests := []struct { 15 name string 16 registry map[string]types.Value 17 wantRegs []string 18 }{ 19 { 20 "An empty map", 21 map[string]types.Value{}, 22 []string{}, 23 }, 24 { 25 "A sample map", 26 map[string]types.Value{ 27 "foo.bar": 42, 28 "foo.baz": true, 29 "moo": []string{"a", "b", "c"}, 30 }, 31 []string{"foo.bar", "foo.baz", "moo"}, 32 }, 33 } 34 35 t.Parallel() 36 37 for _, testCase := range tests { 38 t.Run(testCase.name, func(t *testing.T) { 39 40 // Redefined function 41 oldRegFlags := regFlags 42 regFlags = func(cp *CliProvider) { 43 for k, v := range testCase.registry { 44 cp.registry[k] = v 45 } 46 } 47 48 repo := NewRepository() 49 prov, err := NewCliProvider(repo, 0) 50 if err != nil { 51 t.Fatalf("Failed to initialize a new cli provider: %s", err) 52 } 53 if err := prov.SetUp(repo); err != nil { 54 t.Fatalf("Failed to set up cli provider: %s", err) 55 } 56 57 gotRegs := flattenRepo(repo) 58 for _, k := range testCase.wantRegs { 59 provs, ok := gotRegs[k] 60 if !ok { 61 t.Fatalf("Failed to find a registration for key %q", k) 62 } 63 if !reflect.DeepEqual(provs, []Provider{prov}) { 64 t.Fatalf("Unexpected provider list for key %q: %#v, want: %#v", k, provs, []Provider{prov}) 65 } 66 delete(gotRegs, k) 67 } 68 if len(gotRegs) > 0 { 69 extraKeys := make([]string, 0, len(gotRegs)) 70 for k := range gotRegs { 71 extraKeys = append(extraKeys, k) 72 } 73 sort.Strings(extraKeys) 74 t.Fatalf("Unexpected registration keys: %s", strings.Join(extraKeys, ", ")) 75 } 76 77 regFlags = oldRegFlags 78 }) 79 } 80 } 81 82 func TestCliProviderSet(t *testing.T) { 83 tests := []struct { 84 name string 85 setVal string 86 wantRegistry map[string]types.Value 87 wantErr error 88 }{ 89 { 90 "A bool flag", 91 "foo", 92 map[string]types.Value{"foo": true}, 93 nil, 94 }, 95 { 96 "A simple value", 97 "foo=bar", 98 map[string]types.Value{"foo": "bar"}, 99 nil, 100 }, 101 { 102 "Way too many = signs", 103 "foo=bar=baz", 104 map[string]types.Value{}, 105 fmt.Errorf("Possibly malformed flag (way too many `=`): %q", "foo=bar=baz"), 106 }, 107 } 108 109 t.Parallel() 110 111 for _, testCase := range tests { 112 t.Run(testCase.name, func(t *testing.T) { 113 repo := NewRepository() 114 prov, err := NewCliProvider(repo, 0) 115 if err != nil { 116 t.Fatalf("Failed to instantiate a new cli provider: %s", err) 117 } 118 err = prov.Set(testCase.setVal) 119 if !reflect.DeepEqual(err, testCase.wantErr) { 120 t.Fatalf("Unexpected error while calling CliProvider.Set(%q): %s, want: %s", testCase.setVal, err, testCase.wantErr) 121 } 122 if !reflect.DeepEqual(prov.registry, testCase.wantRegistry) { 123 t.Fatalf("Unexpected state for CliProvider.registry: want: %#v, got: %#v", testCase.wantRegistry, prov.registry) 124 } 125 }) 126 } 127 }