@@ -2,19 +2,42 @@ package utils
22
33import (
44 "bytes"
5+ "fmt"
56 "io"
67 "io/ioutil"
78 "os"
9+ "reflect"
810 "testing"
911
1012 "github.com/stretchr/testify/require"
1113)
1214
15+ type mockReader struct {
16+ n int
17+ err error
18+ }
19+
20+ func (r * mockReader ) Read (p []byte ) (int , error ) {
21+ return r .n , r .err
22+ }
23+
1324// Helper function for setting os.Stdin for mocking in tests.
1425func setStdin (new * os.File ) (cleanup func ()) {
15- old := _osStdin
16- _osStdin = new
17- return func () { _osStdin = old }
26+ old := stdin
27+ stdin = new
28+ return func () { stdin = old }
29+ }
30+
31+ // Returns a temp file and a cleanup function to delete it.
32+ func newFile (t * testing.T , data []byte ) (file * os.File , cleanup func ()) {
33+ f , err := ioutil .TempFile ("" /* dir */ , "utils-read-test" )
34+ require .NoError (t , err )
35+ // write to temp file and reset read cursor to beginning of file
36+ _ , err = f .Write (data )
37+ require .NoError (t , err )
38+ _ , err = f .Seek (0 , io .SeekStart )
39+ require .NoError (t , err )
40+ return f , func () { os .Remove (f .Name ()) }
1841}
1942
2043func TestFileExists (t * testing.T ) {
@@ -43,6 +66,66 @@ func TestFileExists(t *testing.T) {
4366 }
4467}
4568
69+ func TestReadAll (t * testing.T ) {
70+ content := []byte ("read all this" )
71+
72+ type args struct {
73+ r io.Reader
74+ }
75+ tests := []struct {
76+ name string
77+ args args
78+ want []byte
79+ wantErr bool
80+ }{
81+ {"ok" , args {bytes .NewReader (content )}, content , false },
82+ {"fail" , args {& mockReader {err : fmt .Errorf ("this is an error" )}}, []byte {}, true },
83+ }
84+ for _ , tt := range tests {
85+ t .Run (tt .name , func (t * testing.T ) {
86+ got , err := ReadAll (tt .args .r )
87+ if (err != nil ) != tt .wantErr {
88+ t .Errorf ("ReadAll() error = %v, wantErr %v" , err , tt .wantErr )
89+ return
90+ }
91+ if ! reflect .DeepEqual (got , tt .want ) {
92+ t .Errorf ("ReadAll() = %v, want %v" , got , tt .want )
93+ }
94+ })
95+ }
96+ }
97+
98+ func TestReadString (t * testing.T ) {
99+ c1 := []byte ("read all this" )
100+ c2 := []byte ("read all this\n and all that" )
101+
102+ type args struct {
103+ r io.Reader
104+ }
105+ tests := []struct {
106+ name string
107+ args args
108+ want string
109+ wantErr bool
110+ }{
111+ {"ok" , args {bytes .NewReader (c1 )}, "read all this" , false },
112+ {"ok with new line" , args {bytes .NewReader (c2 )}, "read all this" , false },
113+ {"fail" , args {& mockReader {err : fmt .Errorf ("this is an error" )}}, "" , true },
114+ }
115+ for _ , tt := range tests {
116+ t .Run (tt .name , func (t * testing.T ) {
117+ got , err := ReadString (tt .args .r )
118+ if (err != nil ) != tt .wantErr {
119+ t .Errorf ("ReadString() error = %v, wantErr %v" , err , tt .wantErr )
120+ return
121+ }
122+ if got != tt .want {
123+ t .Errorf ("ReadString() = %v, want %v" , got , tt .want )
124+ }
125+ })
126+ }
127+ }
128+
46129func TestReadFile (t * testing.T ) {
47130 content := []byte ("my file content" )
48131 f , cleanup := newFile (t , content )
@@ -84,14 +167,40 @@ func TestStringReadPasswordFromFile(t *testing.T) {
84167 require .Equal (t , "my-password-on-file" , s , "expected %s to equal %s" , s , content )
85168}
86169
87- // Returns a temp file and a cleanup function to delete it.
88- func newFile (t * testing.T , data []byte ) (file * os.File , cleanup func ()) {
89- f , err := ioutil .TempFile ("" /* dir */ , "utils-read-test" )
90- require .NoError (t , err )
91- // write to temp file and reset read cursor to beginning of file
92- _ , err = f .Write (data )
93- require .NoError (t , err )
94- _ , err = f .Seek (0 , io .SeekStart )
95- require .NoError (t , err )
96- return f , func () { os .Remove (f .Name ()) }
170+ func TestReadInput (t * testing.T ) {
171+
172+ type args struct {
173+ prompt string
174+ }
175+ tests := []struct {
176+ name string
177+ args args
178+ before func () func ()
179+ want []byte
180+ wantErr bool
181+ }{
182+ {"ok" , args {"Write input" }, func () func () {
183+ content := []byte ("my file content" )
184+ mockStdin , cleanup := newFile (t , content )
185+ reset := setStdin (mockStdin )
186+ return func () {
187+ defer cleanup ()
188+ reset ()
189+ }
190+ }, []byte ("my file content" ), false },
191+ }
192+ for _ , tt := range tests {
193+ t .Run (tt .name , func (t * testing.T ) {
194+ cleanup := tt .before ()
195+ defer cleanup ()
196+ got , err := ReadInput (tt .args .prompt )
197+ if (err != nil ) != tt .wantErr {
198+ t .Errorf ("ReadInput() error = %v, wantErr %v" , err , tt .wantErr )
199+ return
200+ }
201+ if ! reflect .DeepEqual (got , tt .want ) {
202+ t .Errorf ("ReadInput() = %v, want %v" , got , tt .want )
203+ }
204+ })
205+ }
97206}
0 commit comments