forked from tajtiattila/metadata
/
testutil.go
104 lines (88 loc) · 1.86 KB
/
testutil.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
package testutil
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
func MediaRoot(t *testing.T) string {
if v := os.Getenv("MEDIA_TEST"); v != "" {
return v
}
const testMedia = "github.com/tajtiattila/test-media"
for _, x := range filepath.SplitList(os.Getenv("GOPATH")) {
p := filepath.Join(x, "src", testMedia)
if s, err := os.Stat(p); err == nil && s.Mode().IsDir() {
return p
}
}
t.Skip("test-media not found")
panic("unreachable")
}
// MediaFileInfos returns test file paths having mimetype.
func MediaFileInfos(t *testing.T) []FileInfo {
root := MediaRoot(t)
var fi []FileInfo
pth := filepath.Join(root, "exiftool.json")
f, err := os.Open(pth)
if err != nil {
t.Skip("%s not found", pth)
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&fi); err != nil {
t.Skip("%s decode error %v", pth, err)
}
for _, e := range fi {
fn, ok := e.String("SourceFile")
if !ok {
continue
}
e["SourceFile"] = filepath.Join(root, fn)
}
return fi
}
// MediaFileNames returns test file paths having mimetype.
func MediaFileNames(t *testing.T, mimetype string) []string {
var files []string
for _, e := range MediaFileInfos(t) {
fn, ok := e.String("SourceFile")
if !ok {
continue
}
if mimetype != "" {
if mt, ok := e.String("MIMEType"); !ok || mt != mimetype {
continue
}
}
files = append(files, fn)
}
if len(files) == 0 {
t.Skip("no test files not found")
}
return files
}
type FileInfo map[string]interface{}
func (e FileInfo) Int(n string) (v int, ok bool) {
x, ok := e[n]
if !ok {
return
}
w, ok := x.(float64)
return int(w), ok
}
func (e FileInfo) Float64(n string) (v float64, ok bool) {
x, ok := e[n]
if !ok {
return
}
v, ok = x.(float64)
return v, ok
}
func (e FileInfo) String(n string) (v string, ok bool) {
x, ok := e[n]
if !ok {
return
}
v, ok = x.(string)
return v, ok
}