forked from barasher/go-exiftool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexiftool_test.go
191 lines (154 loc) · 4.11 KB
/
exiftool_test.go
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
package exiftool
import (
"bufio"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewExiftoolEmpty(t *testing.T) {
e, err := NewExiftool()
assert.Nil(t, err)
defer e.Close()
}
func TestNewExifToolOptOk(t *testing.T) {
var exec1, exec2 bool
f1 := func(*Exiftool) error {
exec1 = true
return nil
}
f2 := func(*Exiftool) error {
exec2 = true
return nil
}
e, err := NewExiftool(f1, f2)
assert.Nil(t, err)
defer e.Close()
assert.True(t, exec1)
assert.True(t, exec2)
}
func TestNewExifToolOptKo(t *testing.T) {
f := func(*Exiftool) error {
return fmt.Errorf("err")
}
_, err := NewExiftool(f)
assert.NotNil(t, err)
}
func TestSingleExtract(t *testing.T) {
var tcs = []struct {
tcID string
inFiles []string
expOk []bool
}{
{"single", []string{"./testdata/20190404_131804.jpg"}, []bool{true}},
{"multiple", []string{"./testdata/20190404_131804.jpg", "./testdata/20190404_131804.jpg"}, []bool{true, true}},
{"nonExisting", []string{"./testdata/nonExisting"}, []bool{false}},
{"empty", []string{"./testdata/empty.jpg"}, []bool{true}},
}
for _, tc := range tcs {
tc := tc // Pin variable
t.Run(tc.tcID, func(t *testing.T) {
e, err := NewExiftool()
assert.Nilf(t, err, "error not nil: %v", err)
defer e.Close()
fms := e.ExtractMetadata(tc.inFiles...)
assert.Equal(t, len(tc.expOk), len(fms))
for i, fm := range fms {
t.Log(fm)
assert.Equalf(t, tc.expOk[i], fm.Err == nil, "#%v different", i)
}
})
}
}
func TestMultiExtract(t *testing.T) {
e, err := NewExiftool()
assert.Nilf(t, err, "error not nil: %v", err)
defer e.Close()
f := e.ExtractMetadata("./testdata/20190404_131804.jpg", "./testdata/20190404_131804.jpg")
assert.Equal(t, 2, len(f))
assert.Nil(t, f[0].Err)
assert.Nil(t, f[1].Err)
f = e.ExtractMetadata("./testdata/nonExisting.bla")
assert.Equal(t, 1, len(f))
assert.NotNil(t, f[0].Err)
f = e.ExtractMetadata("./testdata/20190404_131804.jpg")
assert.Equal(t, 1, len(f))
assert.Nil(t, f[0].Err)
}
func TestSplitReadyToken(t *testing.T) {
readyToken := []byte("{ready}\n")
rt := string(readyToken)
var tcs = []struct {
tcID string
in string
expOk bool
expVals []string
}{
{"mono", "a" + rt, true, []string{"a"}},
{"multi", "a" + rt + "b" + rt, true, []string{"a", "b"}},
{"empty", "", true, []string{}},
{"monoNoFinalToken", "a", false, []string{}},
{"multiNoFinalToken", "a" + rt + "b", false, []string{}},
{"emptyWithToken", rt, true, []string{""}},
}
for _, tc := range tcs {
tc := tc // Pin variable
t.Run(tc.tcID, func(t *testing.T) {
sc := bufio.NewScanner(strings.NewReader(tc.in))
sc.Split(splitReadyToken)
vals := []string{}
for sc.Scan() {
vals = append(vals, sc.Text())
}
assert.Equal(t, tc.expOk, sc.Err() == nil)
if tc.expOk {
assert.Equal(t, tc.expVals, vals)
}
})
}
}
func TestCloseNominal(t *testing.T) {
var rClosed, wClosed bool
r := readWriteCloserMock{closed: &rClosed}
w := readWriteCloserMock{closed: &wClosed}
e := Exiftool{stdin: r, stdout: w}
assert.Nil(t, e.Close())
assert.True(t, rClosed)
assert.True(t, wClosed)
}
func TestCloseErrorOnStdin(t *testing.T) {
var rClosed, wClosed bool
r := readWriteCloserMock{closed: &rClosed, closeErr: fmt.Errorf("error")}
w := readWriteCloserMock{closed: &wClosed}
e := Exiftool{stdin: r, stdout: w}
assert.NotNil(t, e.Close())
assert.True(t, rClosed)
assert.True(t, wClosed)
}
func TestCloseErrorOnStdout(t *testing.T) {
var rClosed, wClosed bool
r := readWriteCloserMock{closed: &rClosed}
w := readWriteCloserMock{closed: &wClosed, closeErr: fmt.Errorf("error")}
e := Exiftool{stdin: r, stdout: w}
assert.NotNil(t, e.Close())
assert.True(t, rClosed)
assert.True(t, wClosed)
}
type readWriteCloserMock struct {
writeInt int
writeErr error
readInt int
readErr error
closeErr error
closed *bool
}
func (e readWriteCloserMock) Write(p []byte) (n int, err error) {
return e.writeInt, e.writeErr
}
func (e readWriteCloserMock) Read(p []byte) (n int, err error) {
return e.readInt, e.readErr
}
func (e readWriteCloserMock) Close() error {
*(e.closed) = true
return e.closeErr
}