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
|
package utils
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/pkg/errors"
)
func TestReadFile(t *testing.T) {
type args struct {
filename string
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{"ok", args{"testdata/pass1.txt"}, []byte("brandy-guidon-basin-ishmael-sedge-ducting"), false},
{"missing", args{"testdata/missing.txt"}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ReadFile(tt.args.filename)
if (err != nil) != tt.wantErr {
t.Errorf("ReadFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReadFile() = %v, want %v", got, tt.want)
}
})
}
}
func TestReadPasswordFromFile(t *testing.T) {
type args struct {
filename string
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{"ok", args{"testdata/pass1.txt"}, []byte("brandy-guidon-basin-ishmael-sedge-ducting"), false},
{"trim", args{"testdata/pass2.txt"}, []byte("benumb-eyepiece-stale-revers-marital-mimesis"), false},
{"missing", args{"testdata/missing.txt"}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ReadPasswordFromFile(tt.args.filename)
if (err != nil) != tt.wantErr {
t.Errorf("ReadPasswordFromFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReadPasswordFromFile() = %v, want %v", got, tt.want)
}
})
}
}
func TestWriteFile(t *testing.T) {
tmpDir, err := os.MkdirTemp(os.TempDir(), "go-tests")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
type args struct {
filename string
data []byte
perm os.FileMode
}
tests := []struct {
name string
args args
wantErr bool
}{
{"ok", args{filepath.Join(tmpDir, "test.txt"), []byte("foo"), 0600}, false},
{"fail", args{tmpDir, []byte("foo"), 0600}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := WriteFile(tt.args.filename, tt.args.data, tt.args.perm); (err != nil) != tt.wantErr {
t.Errorf("WriteFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_maybeUnwrap(t *testing.T) {
wantErr := fmt.Errorf("the error")
type args struct {
err error
}
tests := []struct {
name string
args args
wantErr error
}{
{"wrapped", args{errors.WithMessage(wantErr, "wrapped error")}, wantErr},
{"not wrapped", args{wantErr}, wantErr},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := maybeUnwrap(tt.args.err)
if !reflect.DeepEqual(err, tt.wantErr) { //nolint:govet // legacy deep equal error check
t.Errorf("maybeUnwrap() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
|