-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper_test.go
45 lines (37 loc) · 910 Bytes
/
helper_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
package pnm
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
const testResourceDir = "testdata"
func OpenGoldenFile(t *testing.T, filename, source string, update bool) string {
t.Helper()
reader := Open(t, filename, os.O_RDWR)
defer reader.(*os.File).Close()
if update {
err := reader.(*os.File).Truncate(0)
CheckErrorf(t, err)
_, err = reader.(*os.File).WriteString(source)
CheckErrorf(t, err)
}
content, err := ioutil.ReadAll(reader)
CheckErrorf(t, err)
return string(content)
}
func Open(t *testing.T, filename string, perm int) io.ReadWriter {
t.Helper()
path := filepath.Join(testResourceDir, filename)
file, err := os.OpenFile(path, perm, 0644)
if err != nil {
t.Errorf("Can't open file %v", path)
}
return file
}
func CheckErrorf(t *testing.T, err error) {
if err != nil {
t.Errorf("Fail wiht %v", err)
}
}