github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/instrumentation/prompt/prompt_test.go (about) 1 /* 2 Copyright 2021 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package prompt 18 19 import ( 20 "bytes" 21 "fmt" 22 "io" 23 "testing" 24 25 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" 26 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" 27 "github.com/GoogleContainerTools/skaffold/testutil" 28 ) 29 30 func TestShouldDisplayMetricsPrompt(t *testing.T) { 31 tests := []struct { 32 name string 33 config *config.ContextConfig 34 expected bool 35 err error 36 }{ 37 { 38 name: "empty config", 39 config: &config.ContextConfig{}, 40 expected: true, 41 }, 42 { 43 name: "nil config", 44 expected: true, 45 }, 46 { 47 name: "not nil error", 48 err: fmt.Errorf("test error getting config"), 49 }, 50 { 51 name: "config without collect-metrics", 52 config: &config.ContextConfig{DefaultRepo: "test-repo"}, 53 expected: true, 54 }, 55 { 56 name: "collect-metrics false", 57 config: &config.ContextConfig{CollectMetrics: util.BoolPtr(false)}, 58 }, 59 { 60 name: "collect-metrics true", 61 config: &config.ContextConfig{CollectMetrics: util.BoolPtr(true)}, 62 }, 63 } 64 for _, test := range tests { 65 testutil.Run(t, test.name, func(t *testutil.T) { 66 mock := func(string) (*config.ContextConfig, error) { return test.config, test.err } 67 t.Override(&getConfig, mock) 68 t.Override(&setStatus, func() {}) 69 actual := ShouldDisplayMetricsPrompt(test.name) 70 t.CheckDeepEqual(test.expected, actual) 71 }) 72 } 73 } 74 75 func TestDisplayMetricsPrompt(t *testing.T) { 76 tests := []struct { 77 name string 78 mockStdOut bool 79 expected string 80 }{ 81 { 82 name: "std out", 83 mockStdOut: true, 84 expected: Prompt, 85 }, 86 { 87 name: "not std out", 88 }, 89 } 90 for _, test := range tests { 91 testutil.Run(t, test.name, func(t *testutil.T) { 92 mock := func(io.Writer) bool { return test.mockStdOut } 93 t.Override(&isStdOut, mock) 94 t.Override(&updateConfig, func(_ string, _ bool) error { return nil }) 95 var buf bytes.Buffer 96 err := DisplayMetricsPrompt("", &buf) 97 t.CheckErrorAndDeepEqual(false, err, test.expected, buf.String()) 98 }) 99 } 100 }