forked from newrelic/go-agent
/
crossagent.go
57 lines (49 loc) · 1.3 KB
/
crossagent.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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package crossagent
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"runtime"
)
var (
crossAgentDir = func() string {
if s := os.Getenv("NEW_RELIC_CROSS_AGENT_TESTS"); s != "" {
return s
}
_, here, _, _ := runtime.Caller(0)
return filepath.Join(filepath.Dir(here), "cross_agent_tests")
}()
)
// ReadFile reads a file from the crossagent tests directory given as with
// ioutil.ReadFile.
func ReadFile(name string) ([]byte, error) {
return ioutil.ReadFile(filepath.Join(crossAgentDir, name))
}
// ReadJSON takes the name of a file and parses it using JSON.Unmarshal into
// the interface given.
func ReadJSON(name string, v interface{}) error {
data, err := ReadFile(name)
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
// ReadDir reads a directory relative to crossagent tests and returns an array
// of absolute filepaths of the files in that directory.
func ReadDir(name string) ([]string, error) {
dir := filepath.Join(crossAgentDir, name)
entries, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
var files []string
for _, info := range entries {
if !info.IsDir() {
files = append(files, filepath.Join(dir, info.Name()))
}
}
return files, nil
}