1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
package utils
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
type mockReader struct {
n int
err error
}
func (r *mockReader) Read(p []byte) (int, error) {
return r.n, r.err
}
// Helper function for setting os.Stdin for mocking in tests.
func setStdin(new *os.File) (cleanup func()) {
old := stdin
stdin = new
return func() { stdin = old }
}
// Returns a temp file and a cleanup function to delete it.
func newFile(t *testing.T, data []byte) (file *os.File, cleanup func()) {
f, err := ioutil.TempFile("" /* dir */, "utils-read-test")
require.NoError(t, err)
// write to temp file and reset read cursor to beginning of file
_, err = f.Write(data)
require.NoError(t, err)
_, err = f.Seek(0, io.SeekStart)
require.NoError(t, err)
return f, func() { os.Remove(f.Name()) }
}
func TestFileExists(t *testing.T) {
content := []byte("my file content")
f, cleanup := newFile(t, content)
defer cleanup()
type args struct {
path string
}
tests := []struct {
name string
args args
want bool
}{
{"ok", args{f.Name()}, true},
{"nok", args{f.Name() + ".foo"}, false},
{"empty", args{""}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FileExists(tt.args.path); got != tt.want {
t.Errorf("FileExists() = %v, want %v", got, tt.want)
}
})
}
}
func TestReadAll(t *testing.T) {
content := []byte("read all this")
type args struct {
r io.Reader
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{"ok", args{bytes.NewReader(content)}, content, false},
{"fail", args{&mockReader{err: fmt.Errorf("this is an error")}}, []byte{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ReadAll(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("ReadAll() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReadAll() = %v, want %v", got, tt.want)
}
})
}
}
func TestReadString(t *testing.T) {
c1 := []byte("read all this")
c2 := []byte("read all this\n and all that")
type args struct {
r io.Reader
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{"ok", args{bytes.NewReader(c1)}, "read all this", false},
{"ok with new line", args{bytes.NewReader(c2)}, "read all this", false},
{"fail", args{&mockReader{err: fmt.Errorf("this is an error")}}, "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ReadString(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("ReadString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ReadString() = %v, want %v", got, tt.want)
}
})
}
}
func TestReadFile(t *testing.T) {
content := []byte("my file content")
f, cleanup := newFile(t, content)
defer cleanup()
b, err := ReadFile(f.Name())
require.NoError(t, err)
require.True(t, bytes.Equal(content, b), "expected %s to equal %s", b, content)
}
func TestReadFileStdin(t *testing.T) {
content := []byte("my file content")
mockStdin, cleanup := newFile(t, content)
defer cleanup()
defer setStdin(mockStdin)()
b, err := ReadFile(stdinFilename)
require.NoError(t, err)
require.True(t, bytes.Equal(content, b), "expected %s to equal %s", b, content)
}
func TestReadPasswordFromFile(t *testing.T) {
content := []byte("my-password-on-file\n")
f, cleanup := newFile(t, content)
defer cleanup()
b, err := ReadPasswordFromFile(f.Name())
require.NoError(t, err)
require.True(t, bytes.Equal([]byte("my-password-on-file"), b), "expected %s to equal %s", b, content)
}
func TestStringReadPasswordFromFile(t *testing.T) {
content := []byte("my-password-on-file\n")
f, cleanup := newFile(t, content)
defer cleanup()
s, err := ReadStringPasswordFromFile(f.Name())
require.NoError(t, err)
require.Equal(t, "my-password-on-file", s, "expected %s to equal %s", s, content)
}
func TestReadInput(t *testing.T) {
type args struct {
prompt string
}
tests := []struct {
name string
args args
before func() func()
want []byte
wantErr bool
}{
{"ok", args{"Write input"}, func() func() {
content := []byte("my file content")
mockStdin, cleanup := newFile(t, content)
reset := setStdin(mockStdin)
return func() {
defer cleanup()
reset()
}
}, []byte("my file content"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cleanup := tt.before()
defer cleanup()
got, err := ReadInput(tt.args.prompt)
if (err != nil) != tt.wantErr {
t.Errorf("ReadInput() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReadInput() = %v, want %v", got, tt.want)
}
})
}
}
|