GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — func/cmd/config_test.go at main · stackbuilders/func Expand file tree/ Copy pathconfig_test.goMore file actions 169 lines (146 loc) · 3.57 KB / Copy pathconfig_test.goFile metadata and controls169 lines (146 loc) · 3.57 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 package cmd_test import ( "bytes" "encoding/json" "fmt" "io" "sort" "testing" "github.com/ory/viper" "github.com/spf13/cobra" fnCmd "knative.dev/func/cmd" "knative.dev/func/cmd/common" "knative.dev/func/pkg/ci/github" fn "knative.dev/func/pkg/functions" ) func TestListEnvs(t *testing.T) { mock := common.NewMockLoaderSaver() foo := "foo" bar := "bar" envs := []fn.Env{{Name: &foo, Value: &bar}} mock.LoadFn = func(path string) (fn.Function, error) { if path != "<path>" { t.Fatalf("bad path, got %q but expected <path>", path) } return fn.Function{Run: fn.RunSpec{Envs: envs}}, nil } cmd := setupConfigEnvCmd(mock, "-o=json", "--path=<path>") var buff bytes.Buffer cmd.SetOut(&buff) cmd.SetErr(&buff) err := cmd.Execute() if err != nil { t.Fatal(err) } var data []fn.Env err = json.Unmarshal(buff.Bytes(), &data) if err != nil { t.Fatal(err) } if !envsEqual(envs, data) { t.Errorf("env mismatch, expected %v but got %v", envs, data) } } func setupConfigEnvCmd(mock common.FunctionLoaderSaver, args ...string) *cobra.Command { cmd := fnCmd.NewConfigCmd( mock, github.NewBufferWriter(), common.CurrentBranchStub("", nil), common.WorkDirStub("", nil), fnCmd.NewTestCIGeneratorFactory(&github.WorkflowGeneratorMock{}), fnCmd.NewClient, ) cmd.SetArgs(append([]string{"envs"}, args...)) return cmd } func TestListEnvAdd(t *testing.T) { // strings as vars so we can take address of them foo := "foo" bar := "bar" answer := "answer" fortyTwo := "42" configMapExpression := "{{ configMap:myMap }}" mock := common.NewMockLoaderSaver() mock.LoadFn = func(path string) (fn.Function, error) { return fn.Function{Run: fn.RunSpec{Envs: []fn.Env{{Name: &foo, Value: &bar}}}}, nil } var expectedEnvs []fn.Env mock.SaveFn = func(f fn.Function) error { if !envsEqual(expectedEnvs, f.Run.Envs) { return fmt.Errorf("unexpected envs: got %v but %v was expected", f.Run.Envs, expectedEnvs) } return nil } expectedEnvs = []fn.Env{{Name: &foo, Value: &bar}, {Name: &answer, Value: &fortyTwo}} cmd := setupConfigEnvCmd(mock, "add", "--name=answer", "--value=42") cmd.SetOut(io.Discard) cmd.SetErr(io.Discard) err := cmd.Execute() if err != nil { t.Error(err) } viper.Reset() expectedEnvs = []fn.Env{{Name: &foo, Value: &bar}, {Name: nil, Value: &configMapExpression}} cmd = setupConfigEnvCmd(mock, "add", "--value={{ configMap:myMap }}") cmd.SetOut(io.Discard) cmd.SetErr(io.Discard) err = cmd.Execute() if err != nil { t.Error(err) } viper.Reset() cmd = setupConfigEnvCmd(mock, "add", "--name=1", "--value=abc") cmd.SetOut(io.Discard) cmd.SetErr(io.Discard) err = cmd.Execute() if err == nil { t.Error("expected variable name error but got nil") } } func envsEqual(a, b []fn.Env) bool { if len(a) != len(b) { return false } strPtrEq := func(x, y *string) bool { switch { case x == nil && y == nil: return true case x != nil && y != nil: return *x == *y default: return false } } strPtrLess := func(x, y *string) bool { switch { case x == nil && y == nil: return false case x != nil && y != nil: return *x < *y case x == nil: return true default: return false } } lessForSlice := func(s []fn.Env) func(i, j int) bool { return func(i, j int) bool { x := s[i] y := s[j] if strPtrLess(x.Name, y.Name) { return true } return strPtrLess(x.Value, y.Value) } } sort.Slice(a, lessForSlice(a)) sort.Slice(b, lessForSlice(b)) for i := range a { x := a[i] y := b[i] if !strPtrEq(x.Name, y.Name) || !strPtrEq(x.Value, y.Value) { return false } } return true } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |