Browse code

Fix pkg/jsonmessage.TestProgress panic

Fix #23112

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>

Akihiro Suda authored on 2016/05/31 13:12:25
Showing 1 changed files
... ...
@@ -19,6 +19,11 @@ func TestError(t *testing.T) {
19 19
 }
20 20
 
21 21
 func TestProgress(t *testing.T) {
22
+	termsz, err := term.GetWinsize(0)
23
+	if err != nil {
24
+		// we can safely ignore the err here
25
+		termsz = nil
26
+	}
22 27
 	jp := JSONProgress{}
23 28
 	if jp.String() != "" {
24 29
 		t.Fatalf("Expected empty string, got '%s'", jp.String())
... ...
@@ -31,6 +36,9 @@ func TestProgress(t *testing.T) {
31 31
 	}
32 32
 
33 33
 	expectedStart := "[==========>                                        ]     20 B/100 B"
34
+	if termsz != nil && termsz.Width <= 110 {
35
+		expectedStart = "    20 B/100 B"
36
+	}
34 37
 	jp3 := JSONProgress{Current: 20, Total: 100, Start: time.Now().Unix()}
35 38
 	// Just look at the start of the string
36 39
 	// (the remaining time is really hard to test -_-)
... ...
@@ -39,6 +47,9 @@ func TestProgress(t *testing.T) {
39 39
 	}
40 40
 
41 41
 	expected = "[=========================>                         ]     50 B/100 B"
42
+	if termsz != nil && termsz.Width <= 110 {
43
+		expected = "    50 B/100 B"
44
+	}
42 45
 	jp4 := JSONProgress{Current: 50, Total: 100}
43 46
 	if jp4.String() != expected {
44 47
 		t.Fatalf("Expected %q, got %q", expected, jp4.String())
... ...
@@ -46,6 +57,9 @@ func TestProgress(t *testing.T) {
46 46
 
47 47
 	// this number can't be negative gh#7136
48 48
 	expected = "[==================================================>]     50 B"
49
+	if termsz != nil && termsz.Width <= 110 {
50
+		expected = "    50 B"
51
+	}
49 52
 	jp5 := JSONProgress{Current: 50, Total: 40}
50 53
 	if jp5.String() != expected {
51 54
 		t.Fatalf("Expected %q, got %q", expected, jp5.String())