@@ -18,6 +18,8 @@ package progress
18
18
19
19
import (
20
20
"context"
21
+ "os"
22
+ "strings"
21
23
"testing"
22
24
23
25
"gotest.tools/v3/assert"
@@ -29,3 +31,57 @@ func TestNoopWriter(t *testing.T) {
29
31
30
32
assert .Equal (t , writer , & noopWriter {})
31
33
}
34
+
35
+ func TestRunWithStatusWithoutCustomContextWriter (t * testing.T ) {
36
+ r , w , err := os .Pipe ()
37
+ assert .NilError (t , err )
38
+
39
+ os .Stderr = w // mock Stderr for default writer just for testing purpose
40
+
41
+ result := make (chan string )
42
+ go func () {
43
+ buf := make ([]byte , 256 )
44
+ n , _ := r .Read (buf )
45
+ result <- string (buf [:n ])
46
+ }()
47
+
48
+ // run without any custom writer, so it will use the default writer
49
+ _ , err = RunWithStatus (context .TODO (), func (ctx context.Context ) (string , error ) {
50
+ ContextWriter (ctx ).Event (Event {Text : "pass" })
51
+ return "test" , nil
52
+ })
53
+
54
+ assert .NilError (t , err )
55
+
56
+ actual := <- result
57
+ assert .Equal (t , strings .TrimSpace (actual ), "pass" )
58
+ }
59
+
60
+ func TestRunWithStatusrWithCustomContextWriter (t * testing.T ) {
61
+ r , w , err := os .Pipe ()
62
+ assert .NilError (t , err )
63
+
64
+ writer , err := NewWriter (w ) // custom writer
65
+ assert .NilError (t , err )
66
+
67
+ result := make (chan string )
68
+ go func () {
69
+ buf := make ([]byte , 256 )
70
+ n , _ := r .Read (buf )
71
+ result <- string (buf [:n ])
72
+ }()
73
+
74
+ // attach the custom writer to the context
75
+ ctx := WithContextWriter (context .TODO (), writer )
76
+
77
+ // run with the custom writer
78
+ _ , err = RunWithStatus (ctx , func (ctx context.Context ) (string , error ) {
79
+ ContextWriter (ctx ).Event (Event {Text : "pass" })
80
+ return "test" , nil
81
+ })
82
+
83
+ assert .NilError (t , err )
84
+
85
+ actual := <- result
86
+ assert .Equal (t , strings .TrimSpace (actual ), "pass" )
87
+ }
0 commit comments