-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
93 lines (75 loc) · 1.91 KB
/
common_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
// testBoxUtils is a handy handle to all of the testing implementations of
// interfaces needed by EnvBox.
type testBoxUtils struct {
*testSystem
// more to come
}
// cleanup will clean up any testing resources created
func (tau testBoxUtils) cleanup() {
tau.testSystem.cleanup()
}
// newTestBox creates a new EnvBox struct with all of the sub-interfaces
// replaced with doppelgangers that make testing easy.
func newTestBox() (*EnvBox, *testBoxUtils) {
box, _ := NewEnvBox()
tu := &testBoxUtils{
testSystem: newTestSystem(),
}
box.System = tu.testSystem
return box, tu
}
// testSystem is a testing implementation of the System interface.
type testSystem struct {
homePath string
Env map[string]string
}
func newTestSystem() *testSystem {
tempdir, _ := ioutil.TempDir("", "envboxapi")
return &testSystem{
homePath: tempdir,
Env: map[string]string{"HOME": tempdir},
}
}
func (ts testSystem) Getenv(key string) string {
val, ok := ts.Env[key]
if ok {
return val
}
return ""
}
func (ts *testSystem) Setenv(key, value string) {
ts.Env[key] = value
}
func (ts *testSystem) cleanup() {
os.RemoveAll(ts.homePath)
}
func (ts testSystem) FileExists(localPath string) bool {
if _, err := os.Stat(localPath); os.IsNotExist(err) {
return false
}
return true
}
func (ts testSystem) DataPath() (string, error) {
var envboxPath string
if xdgDataHome := ts.Getenv("XDG_DATA_HOME"); len(xdgDataHome) > 0 {
envboxPath = filepath.Join(xdgDataHome, "envbox")
} else {
var home string
if home = ts.Getenv("HOME"); len(home) == 0 {
return "", fmt.Errorf("$HOME environment variable not found")
}
envboxPath = filepath.Join(home, ".local", "share", "envbox")
}
os.MkdirAll(envboxPath, 0755)
return envboxPath, nil
}
func (ts testSystem) ExecCommandWithEnv(command string, args []string, extraEnv []string) error {
return nil
}