Skip to content

Commit

Permalink
Merge pull request #5048 from cailynse/issue-5039
Browse files Browse the repository at this point in the history
Remove Log Assignment to Stdout
  • Loading branch information
k8s-ci-robot committed Feb 21, 2023
2 parents 22dbd3e + 7c33fe3 commit 7a89df8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
7 changes: 7 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 Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion kustomize/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 3 additions & 7 deletions kustomize/commands/localize/localize.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package localize

import (
"fmt"
"io"
"log"

"github.com/spf13/cobra"
Expand All @@ -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]]",
Expand Down Expand Up @@ -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
Expand Down
27 changes: 16 additions & 11 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 @@ -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"),
Expand Down Expand Up @@ -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)

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,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"),
Expand All @@ -121,19 +130,15 @@ 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) {
_, actual, _ := loctest.PrepareFs(t, nil, map[string]string{
"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]")
}
Expand All @@ -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"),
Expand Down

0 comments on commit 7a89df8

Please sign in to comment.