forked from caraml-dev/turing
/
utils.go
41 lines (35 loc) · 973 Bytes
/
utils.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
package testutils
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
"github.com/gojek/turing/api/turing/models"
)
// ReadFile reads a file and returns the byte contents
func ReadFile(filepath string) ([]byte, error) {
// Open file
fileObj, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer fileObj.Close()
// Read contents
byteValue, err := ioutil.ReadAll(fileObj)
if err != nil {
return nil, err
}
return byteValue, nil
}
// GetRouterVersion reads the given file, attempts to convert the contents into
// a router version model object and returns it. The method records a failure to
// the test s if an error is encountered.
func GetRouterVersion(t *testing.T, filePath string) *models.RouterVersion {
fileBytes, err := ReadFile(filePath)
FailOnError(t, err)
// Convert to RouterVersion type
var routerVersion models.RouterVersion
err = json.Unmarshal(fileBytes, &routerVersion)
FailOnError(t, err)
return &routerVersion
}