Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

progress: tweak how os.Stderr is mocked #818

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bib/pkg/progress/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type (

func MockOsStderr(w io.Writer) (restore func()) {
saved := osStderr
osStderr = w
osStderr = func() io.Writer { return w }
return func() {
osStderr = saved
}
Expand Down
15 changes: 10 additions & 5 deletions bib/pkg/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
)

var (
osStderr io.Writer = os.Stderr

// This is only needed because pb.Pool require a real terminal.
// It sets it into "raw-mode" but there is really no need for
// this (see "func render()" below) so once this is fixed
Expand All @@ -30,6 +28,13 @@ var (
CURSOR_SHOW = ESC + "[?25h"
)

// Used for testing, this must be a function (instead of the usual
// "var osStderr = os.Stderr" so that higher level libraries can test
// this code by replacing "os.Stderr", e.g. testutil.CaptureStdio()
var osStderr = func() io.Writer {
return os.Stderr
}

func cursorUp(i int) string {
return fmt.Sprintf("%s[%dA", ESC, i)
}
Expand Down Expand Up @@ -104,7 +109,7 @@ type terminalProgressBar struct {
// most terminals.
func NewTerminalProgressBar() (ProgressBar, error) {
b := &terminalProgressBar{
out: osStderr,
out: osStderr(),
}
b.spinnerPb = pb.New(0)
b.spinnerPb.SetTemplate(`[{{ (cycle . "|" "/" "-" "\\") }}] {{ string . "spinnerMsg" }}`)
Expand Down Expand Up @@ -248,7 +253,7 @@ type verboseProgressBar struct {
// NewVerboseProgressBar starts a new "verbose" progressbar that will just
// prints message but does not show any progress.
func NewVerboseProgressBar() (ProgressBar, error) {
b := &verboseProgressBar{w: osStderr}
b := &verboseProgressBar{w: osStderr()}
return b, nil
}

Expand Down Expand Up @@ -281,7 +286,7 @@ type debugProgressBar struct {
// so "glitches/weird" messages from the lower-layers can be inspected
// easier.
func NewDebugProgressBar() (ProgressBar, error) {
b := &debugProgressBar{w: osStderr}
b := &debugProgressBar{w: osStderr()}
return b, nil
}

Expand Down
Loading