-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers_test.go
More file actions
47 lines (40 loc) · 1.01 KB
/
helpers_test.go
File metadata and controls
47 lines (40 loc) · 1.01 KB
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
package procapi
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func helperLoadJSON(t *testing.T, name string, data interface{}) {
path := filepath.Join("testdata", name+".json") // relative path
bytes, err := ioutil.ReadFile(path)
require.NoError(t, err)
err = json.Unmarshal(bytes, &data)
require.NoError(t, err)
}
const TestUpdateEnv = "TEST_UPDATE"
const TestUpdateDir = "testdata-new"
func helperCheckTestUpdate(file string, data interface{}) {
if os.Getenv(TestUpdateEnv) == "" {
return
}
if _, err := os.Stat(TestUpdateDir); os.IsNotExist(err) {
err = os.Mkdir(TestUpdateDir, os.ModePerm)
check(err)
}
p, err := ioutil.TempFile(TestUpdateDir, file+".*.json")
check(err)
fmt.Printf("*** Writing %s\n", p.Name())
out, err := json.MarshalIndent(data, "", " ")
check(err)
_, err = p.WriteString(string(out) + "\n") //ioutil.WriteFile(p, out, os.FileMode(mode))
check(err)
}
func check(e error) {
if e != nil {
panic(e)
}
}