forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
49 lines (41 loc) · 947 Bytes
/
util_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
46
47
48
49
package getter
import (
"io"
"io/ioutil"
"os"
"strings"
"testing"
)
// tempEnv sets the env var temporarily and returns a function that should
// be deferred to clean it up.
func tempEnv(t *testing.T, k, v string) func() {
old := os.Getenv(k)
// Set env
if err := os.Setenv(k, v); err != nil {
t.Fatalf("err: %s", err)
}
// Easy cleanup
return func() {
if err := os.Setenv(k, old); err != nil {
t.Fatalf("err: %s", err)
}
}
}
// tempFileContents writes a temporary file and returns the path and a function
// to clean it up.
func tempFileContents(t *testing.T, contents string) (string, func()) {
tf, err := ioutil.TempFile("", "getter")
if err != nil {
t.Fatalf("err: %s", err)
}
if _, err := io.Copy(tf, strings.NewReader(contents)); err != nil {
t.Fatalf("err: %s", err)
}
tf.Close()
path := tf.Name()
return path, func() {
if err := os.Remove(path); err != nil {
t.Fatalf("err: %s", err)
}
}
}