fix defer in for loop, maybe resource leak

Kubernetes-commit: 684c5bf2a77f96b7db608e3c726a1d7da1a68421
This commit is contained in:
czm 2022-02-20 11:39:11 +08:00 committed by Kubernetes Publisher
parent c26d922e2e
commit 46b7c48533
1 changed files with 24 additions and 9 deletions

View File

@ -27,16 +27,31 @@ import (
)
func TestSetupOutputWriterNoOp(t *testing.T) {
tests := []string{"", "-"}
for _, test := range tests {
_, _, buf, _ := genericclioptions.NewTestIOStreams()
f := cmdtesting.NewTestFactory()
defer f.Cleanup()
tests := []struct {
name string
outputWriter string
}{
{
name: "empty",
outputWriter: "",
},
{
name: "stdout",
outputWriter: "-",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
_, _, buf, _ := genericclioptions.NewTestIOStreams()
f := cmdtesting.NewTestFactory()
defer f.Cleanup()
writer := setupOutputWriter(test, buf, "/some/file/that/should/be/ignored", "")
if writer != buf {
t.Errorf("expected: %v, saw: %v", buf, writer)
}
writer := setupOutputWriter(tt.outputWriter, buf, "/some/file/that/should/be/ignored", "")
if writer != buf {
t.Errorf("expected: %v, saw: %v", buf, writer)
}
})
}
}