Remove Log Assignment to Stdout

This commit is contained in:
Cailyn Edwards
2023-02-16 12:48:37 -05:00
parent 0fd385d719
commit 7c33fe30b7
4 changed files with 27 additions and 19 deletions

View File

@@ -6,6 +6,7 @@ package localizer_test
import (
"bytes"
"log"
"os"
"testing"
"github.com/stretchr/testify/require"
@@ -37,6 +38,9 @@ func TestLocalLoadNewAndCleanup(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
// typical setup
ldr, args, err := NewLoader("a", "/", "/newDir", fSys)
req.NoError(err)
@@ -223,6 +227,9 @@ func TestNewLocLoaderFails(t *testing.T) {
t.Run(name, func(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
_, _, err := NewLoader(params.target, params.scope, params.dest, makeMemoryFs(t))
require.Error(t, err)

View File

@@ -54,7 +54,7 @@ See https://sigs.k8s.io/kustomize
create.NewCmdCreate(fSys, pvd.GetResourceFactory()),
version.NewCmdVersion(stdOut),
openapi.NewCmdOpenAPI(stdOut),
localize.NewCmdLocalize(fSys, stdOut),
localize.NewCmdLocalize(fSys),
)
configcobra.AddCommands(c, konfig.ProgramName)

View File

@@ -4,8 +4,6 @@
package localize
import (
"fmt"
"io"
"log"
"github.com/spf13/cobra"
@@ -26,8 +24,7 @@ type flags struct {
}
// NewCmdLocalize returns a new localize command.
func NewCmdLocalize(fs filesys.FileSystem, writer io.Writer) *cobra.Command {
log.SetOutput(writer)
func NewCmdLocalize(fs filesys.FileSystem) *cobra.Command {
var f flags
cmd := &cobra.Command{
Use: "localize [target [destination]]",
@@ -65,9 +62,8 @@ kustomize localize https://github.com/kubernetes-sigs/kustomize//api/krusty/test
if err != nil {
return errors.Wrap(err)
}
successMsg := fmt.Sprintf("SUCCESS: localized %q to directory %s\n", args.target, dst)
_, err = writer.Write([]byte(successMsg))
return errors.Wrap(err)
log.Printf("SUCCESS: localized %q to directory %s\n", args.target, dst)
return nil
},
}
// no shorthand to avoid conflation with other flags

View File

@@ -7,6 +7,7 @@ import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"testing"
@@ -53,7 +54,7 @@ func TestScopeFlag(t *testing.T) {
"base",
}, kustomizations)
cmd := localize.NewCmdLocalize(actual, new(bytes.Buffer))
cmd := localize.NewCmdLocalize(actual)
require.NoError(t, cmd.Flags().Set("scope", testDir.String()))
err := cmd.RunE(cmd, []string{
testDir.Join("target"),
@@ -85,7 +86,11 @@ func TestOptionalArgs(t *testing.T) {
loctest.SetWorkingDir(t, target)
buffy := new(bytes.Buffer)
cmd := localize.NewCmdLocalize(actual, buffy)
log.SetOutput(buffy)
defer func() {
log.SetOutput(os.Stderr)
}()
cmd := localize.NewCmdLocalize(actual)
err := cmd.RunE(cmd, args)
require.NoError(t, err)
@@ -96,7 +101,7 @@ func TestOptionalArgs(t *testing.T) {
successMsg := fmt.Sprintf(`SUCCESS: localized "." to directory %s
`, dst)
require.Equal(t, successMsg, buffy.String())
require.Contains(t, buffy.String(), successMsg)
})
}
}
@@ -109,7 +114,11 @@ func TestOutput(t *testing.T) {
expected, actual, target := loctest.PrepareFs(t, nil, kustomization)
buffy := new(bytes.Buffer)
cmd := localize.NewCmdLocalize(actual, buffy)
log.SetOutput(buffy)
defer func() {
log.SetOutput(os.Stderr)
}()
cmd := localize.NewCmdLocalize(actual)
err := cmd.RunE(cmd, []string{
target.String(),
target.Join("dst"),
@@ -121,11 +130,7 @@ func TestOutput(t *testing.T) {
successMsg := fmt.Sprintf(`SUCCESS: localized "%s" to directory %s
`, target.String(), target.Join("dst"))
require.Equal(t, successMsg, buffy.String())
const msg = "Check that cmd log output is hooked to buffy."
log.Print(msg)
require.Contains(t, buffy.String(), msg)
require.Contains(t, buffy.String(), successMsg)
}
func TestAlpha(t *testing.T) {
@@ -133,7 +138,7 @@ func TestAlpha(t *testing.T) {
"kustomization.yaml": `namePrefix: test-`,
})
cmd := localize.NewCmdLocalize(actual, new(bytes.Buffer))
cmd := localize.NewCmdLocalize(actual)
require.Contains(t, cmd.Short, "[Alpha]")
require.Contains(t, cmd.Long, "[Alpha]")
}
@@ -143,7 +148,7 @@ func TestTooManyArgs(t *testing.T) {
"kustomization.yaml": `namePrefix: test-`,
})
cmd := localize.NewCmdLocalize(actual, new(bytes.Buffer))
cmd := localize.NewCmdLocalize(actual)
err := cmd.Args(cmd, []string{
target.String(),
target.Join("dst"),