Skip to content

Commit

Permalink
Remove Log Assignment to Stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
Cailyn Edwards committed Feb 17, 2023
1 parent 0fd385d commit b8464a7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 4 additions & 0 deletions api/internal/localizer/locloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package localizer_test
import (
"bytes"
"log"
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions kustomize/commands/localize/localize.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package localize

import (
"fmt"
"io"
"log"

Expand All @@ -27,7 +26,6 @@ type flags struct {

// NewCmdLocalize returns a new localize command.
func NewCmdLocalize(fs filesys.FileSystem, writer io.Writer) *cobra.Command {
log.SetOutput(writer)
var f flags
cmd := &cobra.Command{
Use: "localize [target [destination]]",
Expand Down Expand Up @@ -65,9 +63,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
Expand Down
17 changes: 11 additions & 6 deletions kustomize/commands/localize/localize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -85,6 +86,10 @@ func TestOptionalArgs(t *testing.T) {
loctest.SetWorkingDir(t, target)

buffy := new(bytes.Buffer)
log.SetOutput(buffy)
defer func() {
log.SetOutput(os.Stderr)
}()
cmd := localize.NewCmdLocalize(actual, buffy)
err := cmd.RunE(cmd, args)
require.NoError(t, err)
Expand All @@ -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)
})
}
}
Expand All @@ -109,6 +114,10 @@ func TestOutput(t *testing.T) {
expected, actual, target := loctest.PrepareFs(t, nil, kustomization)

buffy := new(bytes.Buffer)
log.SetOutput(buffy)
defer func() {
log.SetOutput(os.Stderr)
}()
cmd := localize.NewCmdLocalize(actual, buffy)
err := cmd.RunE(cmd, []string{
target.String(),
Expand All @@ -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) {
Expand Down

0 comments on commit b8464a7

Please sign in to comment.