Skip to content

Commit 502db75

Browse files
authored
Merge pull request #146 from oerlikon/fix-full-width-test
Fix full width test
2 parents 23b4ee3 + b2ea7ac commit 502db75

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

progressbar.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func OptionEnableColorCodes(colorCodes bool) Option {
188188
}
189189
}
190190

191-
// OptionSetElapsedTime will enable elapsed time. always enabled if OptionSetPredictTime is true.
191+
// OptionSetElapsedTime will enable elapsed time. Always enabled if OptionSetPredictTime is true.
192192
func OptionSetElapsedTime(elapsedTime bool) Option {
193193
return func(p *ProgressBar) {
194194
p.config.elapsedTime = elapsedTime
@@ -484,12 +484,12 @@ func (p *ProgressBar) Add(num int) error {
484484
return p.Add64(int64(num))
485485
}
486486

487-
// Set wil set the bar to a current number
487+
// Set will set the bar to a current number
488488
func (p *ProgressBar) Set(num int) error {
489489
return p.Set64(int64(num))
490490
}
491491

492-
// Set64 wil set the bar to a current number
492+
// Set64 will set the bar to a current number
493493
func (p *ProgressBar) Set64(num int64) error {
494494
p.lock.Lock()
495495
toAdd := num - int64(p.state.currentBytes)
@@ -795,12 +795,9 @@ func renderProgressBar(c config, s *state) (int, error) {
795795
}
796796

797797
if c.fullWidth && !c.ignoreLength {
798-
width, _, err := term.GetSize(int(os.Stdout.Fd()))
798+
width, err := termWidth()
799799
if err != nil {
800-
width, _, err = term.GetSize(int(os.Stderr.Fd()))
801-
if err != nil {
802-
width = 80
803-
}
800+
width = 80
804801
}
805802

806803
amend := 1 // an extra space at eol
@@ -1062,3 +1059,17 @@ func humanizeBytes(s float64) (string, string) {
10621059
func logn(n, b float64) float64 {
10631060
return math.Log(n) / math.Log(b)
10641061
}
1062+
1063+
// termWidth function returns the visible width of the current terminal
1064+
// and can be redefined for testing
1065+
var termWidth = func() (width int, err error) {
1066+
width, _, err = term.GetSize(int(os.Stdout.Fd()))
1067+
if err == nil {
1068+
return width, nil
1069+
}
1070+
width, _, err = term.GetSize(int(os.Stderr.Fd()))
1071+
if err == nil {
1072+
return width, nil
1073+
}
1074+
return 0, err
1075+
}

progressbar_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ import (
1616
"github.com/stretchr/testify/assert"
1717
)
1818

19+
func TestMain(m *testing.M) {
20+
termWidth = func() (int, error) {
21+
return 0, os.ErrPermission
22+
}
23+
os.Exit(m.Run())
24+
}
25+
1926
func BenchmarkRender(b *testing.B) {
2027
bar := NewOptions64(100000000,
2128
OptionSetWriter(os.Stderr),
@@ -395,22 +402,23 @@ func TestOptionSetPredictTime(t *testing.T) {
395402
}
396403
}
397404

398-
func TestOptionSetElapsedTime(t *testing.T) {
399-
/*
400-
Spinner test with iteration count and iteration rate
401-
*/
405+
func TestOptionSetElapsedTime_spinner(t *testing.T) {
406+
buf := strings.Builder{}
402407
bar := NewOptions(-1,
403408
OptionSetWidth(10),
409+
OptionSetWriter(&buf),
404410
OptionShowIts(),
405411
OptionShowCount(),
406412
OptionSetElapsedTime(false),
407413
)
408414
bar.Reset()
409415
time.Sleep(1 * time.Second)
410416
bar.Add(5)
411-
412-
// Output:
413-
// - (5/-, 5 it/s)
417+
result := strings.TrimSpace(buf.String())
418+
expect := "- (5/-, 5 it/s)"
419+
if result != expect {
420+
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
421+
}
414422
}
415423

416424
func TestShowElapsedTimeOnFinish(t *testing.T) {
@@ -420,14 +428,11 @@ func TestShowElapsedTimeOnFinish(t *testing.T) {
420428
OptionSetWidth(10),
421429
OptionSetWriter(&buf),
422430
)
423-
424431
bar.Reset()
425432
time.Sleep(3 * time.Second)
426433
bar.Add(10)
427-
428434
result := strings.TrimSpace(buf.String())
429435
expect := "100% |██████████| [3s]"
430-
431436
if result != expect {
432437
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
433438
}

0 commit comments

Comments
 (0)