Skip to content

Commit 160b72c

Browse files
mvo5lzap
andcommitted
main: unif/refactor std{out,err} test handling
For historic reasons we have two way to test the std{out,err} output. One is to just monkey patch `var osStdout = os.Stdout` and then replace it in the tests, the other to have a helper that works like a `with` in python that replaces the stdout/stderr dynamically, e.g.: ``` stdout, stderr := testutil.CaptureStdio(t, func() { err = main.Run() require.NoError(t, err) }) ``` The trouble is that they are not quite compatible. In places where we use the monkey patches `osStdout` the testutil.CaptureStdio() will not work as it (temporarely) replaces the "real" `os.Stdout` (and vice-versa). So this commit unifies all tests to use testutil.CaptureStdio() which feels slightly nicer than the other approach in the sense that one does not need to remember to use `osStdout` in the code. Its mostly mechanical. Co-authored-by: Lukas Zapletal <lzap+git@redhat.com>
1 parent 9991cf2 commit 160b72c

9 files changed

Lines changed: 129 additions & 218 deletions

File tree

cmd/image-builder/bib_cloud.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func bibUpload(uploader cloud.Uploader, path string, flags *pflag.FlagSet) error
4242
size = st.Size()
4343
pbar.SetTotal(size)
4444
pbar.Set(pb.Bytes, true)
45-
pbar.SetWriter(osStdout)
45+
pbar.SetWriter(os.Stdout)
4646
r = pbar.NewProxyReader(file)
4747
pbar.Start()
4848
defer pbar.Finish()
4949
}
5050

51-
return uploader.UploadAndRegister(r, uint64(size), osStderr)
51+
return uploader.UploadAndRegister(r, uint64(size), os.Stderr)
5252
}

cmd/image-builder/distro.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/osbuild/images/pkg/distro"
78
)
@@ -25,6 +26,6 @@ func findDistro(argDistroName, bpDistroName string) (string, error) {
2526
if err != nil {
2627
return "", fmt.Errorf("error deriving host distro %w", err)
2728
}
28-
fmt.Fprintf(osStderr, "No distro name specified, selecting %q based on host, use --distro to override\n", distroStr)
29+
fmt.Fprintf(os.Stderr, "No distro name specified, selecting %q based on host, use --distro to override\n", distroStr)
2930
return distroStr, nil
3031
}

cmd/image-builder/distro_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package main_test
22

33
import (
4-
"bytes"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
87

98
main "github.com/osbuild/image-builder-cli/cmd/image-builder"
9+
"github.com/osbuild/image-builder-cli/internal/testutil"
1010
)
1111

1212
func TestFindDistro(t *testing.T) {
@@ -32,17 +32,17 @@ func TestFindDistro(t *testing.T) {
3232
}
3333

3434
func TestFindDistroAutoDetect(t *testing.T) {
35-
var buf bytes.Buffer
36-
restore := main.MockOsStderr(&buf)
37-
defer restore()
38-
39-
restore = main.MockDistroGetHostDistroName(func() (string, error) {
35+
restore := main.MockDistroGetHostDistroName(func() (string, error) {
4036
return "mocked-host-distro", nil
4137
})
4238
defer restore()
4339

44-
distro, err := main.FindDistro("", "")
40+
var err error
41+
var distro string
42+
_, stderr := testutil.CaptureStdio(t, func() {
43+
distro, err = main.FindDistro("", "")
44+
})
4545
assert.NoError(t, err)
4646
assert.Equal(t, "mocked-host-distro", distro)
47-
assert.Equal(t, "No distro name specified, selecting \"mocked-host-distro\" based on host, use --distro to override\n", buf.String())
47+
assert.Equal(t, "No distro name specified, selecting \"mocked-host-distro\" based on host, use --distro to override\n", stderr)
4848
}

cmd/image-builder/export_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"io"
65
"os"
76

87
"github.com/osbuild/images/pkg/cloud"
@@ -31,22 +30,6 @@ func MockOsArgs(new []string) (restore func()) {
3130
}
3231
}
3332

34-
func MockOsStdout(new io.Writer) (restore func()) {
35-
saved := osStdout
36-
osStdout = new
37-
return func() {
38-
osStdout = saved
39-
}
40-
}
41-
42-
func MockOsStderr(new io.Writer) (restore func()) {
43-
saved := osStderr
44-
osStderr = new
45-
return func() {
46-
osStderr = saved
47-
}
48-
}
49-
5033
func MockNewRepoRegistry(f func() (*reporegistry.RepoRegistry, error)) (restore func()) {
5134
saved := newRepoRegistry
5235
newRepoRegistry = func(repoDir string, extraRepos []string) (*reporegistry.RepoRegistry, error) {

cmd/image-builder/list.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"os"
5+
46
"github.com/osbuild/images/pkg/imagefilter"
57
)
68

@@ -19,7 +21,7 @@ func listImages(repoDir string, extraRepos []string, output string, filterExprs
1921
if err != nil {
2022
return err
2123
}
22-
if err := fmter.Output(osStdout, filteredResult); err != nil {
24+
if err := fmter.Output(os.Stdout, filteredResult); err != nil {
2325
return err
2426
}
2527

cmd/image-builder/main.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ import (
3030
"github.com/osbuild/image-builder-cli/pkg/setup"
3131
)
3232

33-
var (
34-
osStdout io.Writer = os.Stdout
35-
osStderr io.Writer = os.Stderr
36-
)
37-
3833
// basenameFor returns the basename for directory and filenames
3934
// for the given imageType. This can be user overriden via userBasename.
4035
func basenameFor(img *imagefilter.Result, userBasename string) string {
@@ -340,7 +335,7 @@ func cmdManifest(cmd *cobra.Command, args []string) error {
340335
if err != nil {
341336
return err
342337
}
343-
_, err = cmdManifestWrapper(pbar, cmd, args, osStdout, io.Discard, nil)
338+
_, err = cmdManifestWrapper(pbar, cmd, args, os.Stdout, io.Discard, nil)
344339
return err
345340
}
346341

@@ -452,7 +447,7 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
452447
}
453448
pbar.Stop()
454449

455-
fmt.Fprintf(osStdout, "Image build successful: %s\n", imagePath)
450+
fmt.Fprintf(os.Stdout, "Image build successful: %s\n", imagePath)
456451

457452
if uploader != nil {
458453
// XXX: integrate better into the progress, see bib
@@ -493,7 +488,7 @@ func cmdDescribeImg(cmd *cobra.Command, args []string) error {
493488
return err
494489
}
495490

496-
return describeImage(res, osStdout)
491+
return describeImage(res, os.Stdout)
497492
}
498493

499494
func normalizeRootArgs(_ *pflag.FlagSet, name string) pflag.NormalizedName {
@@ -549,9 +544,6 @@ operating systems like Fedora, CentOS and RHEL with easy customizations support.
549544
rootCmd.PersistentFlags().String("output-dir", "", `Put output into the specified directory`)
550545
rootCmd.PersistentFlags().BoolP("verbose", "v", false, `Switch to verbose mode (more logging on stderr and verbose progress)`)
551546

552-
rootCmd.SetOut(osStdout)
553-
rootCmd.SetErr(osStderr)
554-
555547
listCmd := &cobra.Command{
556548
Use: "list",
557549
Short: "List buildable images, use --filter to limit further",

0 commit comments

Comments
 (0)