|
| 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "os/exec" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/google/go-cmp/cmp" |
| 25 | +) |
| 26 | + |
| 27 | +func TestCmd(t *testing.T) { |
| 28 | + RunCmdOut([]string{"hello"}, "abc"). |
| 29 | + AndRunCmdFail([]string{"ls"}, 1). |
| 30 | + Setup(t) |
| 31 | + |
| 32 | + if out, err := newCommand(nil, []string{"hello"}, nil).Output(); err != nil { |
| 33 | + t.Error("command should not have failed") |
| 34 | + } else if string(out) != "abc" { |
| 35 | + t.Error("output should have been abc") |
| 36 | + } |
| 37 | + if newCommand(nil, []string{"ls"}, nil).Run() == nil { |
| 38 | + t.Error("command should have failed") |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type fakeCmd struct { |
| 43 | + mode string |
| 44 | + cmdline []string |
| 45 | + exitCode int |
| 46 | + output string |
| 47 | +} |
| 48 | + |
| 49 | +// ensures fakeCmd satisfies the commander interface |
| 50 | +var _ commander = (*fakeCmd)(nil) |
| 51 | + |
| 52 | +func (f *fakeCmd) Run() error { |
| 53 | + _t.Helper() |
| 54 | + if f.mode != "Run" { |
| 55 | + _t.Errorf("Command%v: expected %s() not Run()", f.cmdline, f.mode) |
| 56 | + } |
| 57 | + if f.exitCode == 0 { |
| 58 | + return nil |
| 59 | + } |
| 60 | + // doesn't seem to be an easy way to set the exitcode |
| 61 | + return &exec.ExitError{} |
| 62 | +} |
| 63 | + |
| 64 | +func (f *fakeCmd) Output() ([]byte, error) { |
| 65 | + _t.Helper() |
| 66 | + if f.mode != "Output" { |
| 67 | + _t.Errorf("Command%v: expected %v() not Output()", f.cmdline, f.mode) |
| 68 | + } |
| 69 | + if f.exitCode == 0 { |
| 70 | + return []byte(f.output), nil |
| 71 | + } |
| 72 | + // doesn't seem to be an easy way to set the exitcode |
| 73 | + return []byte(f.output), &exec.ExitError{} |
| 74 | +} |
| 75 | + |
| 76 | +func (f *fakeCmd) CombinedOutput() ([]byte, error) { |
| 77 | + _t.Helper() |
| 78 | + if f.mode != "Output" { |
| 79 | + _t.Errorf("Command%v: expected %v() not CombinedOutput()", f.cmdline, f.mode) |
| 80 | + } |
| 81 | + if f.exitCode == 0 { |
| 82 | + return []byte(f.output), nil |
| 83 | + } |
| 84 | + // doesn't seem to be an easy way to set the exitcode |
| 85 | + return []byte(f.output), &exec.ExitError{} |
| 86 | +} |
| 87 | + |
| 88 | +type commands []*fakeCmd |
| 89 | + |
| 90 | +var ( |
| 91 | + _cmdStack commands |
| 92 | + _t *testing.T |
| 93 | +) |
| 94 | + |
| 95 | +func fakeCommand(_ context.Context, cmdline []string, env env) commander { |
| 96 | + _t.Helper() |
| 97 | + if len(_cmdStack) == 0 { |
| 98 | + _t.Fatalf("test expected no further commands: %v", cmdline) |
| 99 | + } |
| 100 | + current := _cmdStack[0] |
| 101 | + _cmdStack = _cmdStack[1:] |
| 102 | + if diff := cmp.Diff(current.cmdline, cmdline); diff != "" { |
| 103 | + _t.Errorf("cmdlines differ (-got, +want): %s", diff) |
| 104 | + } |
| 105 | + return current |
| 106 | +} |
| 107 | + |
| 108 | +func (c commands) Setup(t *testing.T) { |
| 109 | + _t = t |
| 110 | + _cmdStack = c |
| 111 | + |
| 112 | + oldCommand := newCommand |
| 113 | + oldConsoleCommand := newConsoleCommand |
| 114 | + newCommand = fakeCommand |
| 115 | + newConsoleCommand = fakeCommand |
| 116 | + _t.Cleanup(func() { |
| 117 | + newCommand = oldCommand |
| 118 | + newConsoleCommand = oldConsoleCommand |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func RunCmd(cmdline []string) commands { |
| 123 | + return commands{}.AndRunCmd(cmdline) |
| 124 | +} |
| 125 | + |
| 126 | +func RunCmdFail(cmdline []string, exitCode int) commands { |
| 127 | + return commands{}.AndRunCmdFail(cmdline, exitCode) |
| 128 | +} |
| 129 | + |
| 130 | +func RunCmdOut(cmdline []string, output string) commands { |
| 131 | + return commands{}.AndRunCmdOut(cmdline, output) |
| 132 | +} |
| 133 | + |
| 134 | +func RunCmdOutFail(cmdline []string, output string, exitCode int) commands { |
| 135 | + return commands{}.AndRunCmdOutFail(cmdline, output, exitCode) |
| 136 | +} |
| 137 | + |
| 138 | +func (c commands) AndRunCmd(cmdline []string) commands { |
| 139 | + c = append(c, &fakeCmd{mode: "Run", cmdline: cmdline}) |
| 140 | + return c |
| 141 | +} |
| 142 | + |
| 143 | +func (c commands) AndRunCmdFail(cmdline []string, exitCode int) commands { |
| 144 | + c = append(c, &fakeCmd{mode: "Run", cmdline: cmdline, exitCode: exitCode}) |
| 145 | + return c |
| 146 | +} |
| 147 | + |
| 148 | +func (c commands) AndRunCmdOut(cmdline []string, output string) commands { |
| 149 | + c = append(c, &fakeCmd{mode: "Output", cmdline: cmdline, output: output}) |
| 150 | + return c |
| 151 | +} |
| 152 | + |
| 153 | +func (c commands) AndRunCmdOutFail(cmdline []string, output string, exitCode int) commands { |
| 154 | + c = append(c, &fakeCmd{mode: "Output", cmdline: cmdline, output: output, exitCode: exitCode}) |
| 155 | + return c |
| 156 | +} |
0 commit comments