|
| 1 | +/* |
| 2 | +Copyright 2019 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 survey |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/GoogleContainerTools/skaffold/testutil" |
| 26 | +) |
| 27 | + |
| 28 | +func TestDisplaySurveyForm(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + description string |
| 31 | + mockStdOut bool |
| 32 | + expected string |
| 33 | + }{ |
| 34 | + { |
| 35 | + description: "std out", |
| 36 | + mockStdOut: true, |
| 37 | + expected: Prompt + "\n", |
| 38 | + }, |
| 39 | + { |
| 40 | + description: "not std out", |
| 41 | + }, |
| 42 | + } |
| 43 | + for _, test := range tests { |
| 44 | + testutil.Run(t, test.description, func(t *testutil.T) { |
| 45 | + mock := func(io.Writer) bool { return test.mockStdOut } |
| 46 | + t.Override(&isStdOut, mock) |
| 47 | + var buf bytes.Buffer |
| 48 | + DisplaySurveyForm(&buf) |
| 49 | + t.CheckDeepEqual(test.expected, buf.String()) |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestIsStdOut(t *testing.T) { |
| 55 | + tests := []struct { |
| 56 | + description string |
| 57 | + out io.Writer |
| 58 | + expected bool |
| 59 | + }{ |
| 60 | + { |
| 61 | + description: "std out passed", |
| 62 | + out: os.Stdout, |
| 63 | + expected: true, |
| 64 | + }, |
| 65 | + { |
| 66 | + description: "out nil", |
| 67 | + out: nil, |
| 68 | + }, |
| 69 | + { |
| 70 | + description: "out bytes buffer", |
| 71 | + out: new(bytes.Buffer), |
| 72 | + }, |
| 73 | + } |
| 74 | + for _, test := range tests { |
| 75 | + testutil.Run(t, test.description, func(t *testutil.T) { |
| 76 | + t.CheckDeepEqual(test.expected, isStdOut(test.out)) |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
0 commit comments