-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathconfig_file_windows_test.go
More file actions
80 lines (73 loc) · 1.48 KB
/
config_file_windows_test.go
File metadata and controls
80 lines (73 loc) · 1.48 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
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
//go:build windows
// +build windows
package pathing
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestConfigPath(t *testing.T) {
// temporary directories for env vars
packerConfigTempDir, err := ioutil.TempDir(os.TempDir(), "*")
if err != nil {
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
}
defer os.RemoveAll(packerConfigTempDir)
// reset env
packerConfigDir := os.Getenv("PACKER_CONFIG_DIR")
os.Setenv("PACKER_CONFIG_DIR", "")
defer func() {
os.Setenv("PACKER_CONFIG_DIR", packerConfigDir)
}()
homedir, err := homeDir()
if err != nil {
t.Fatalf("err: %s", err)
}
tests := []struct {
name string
env map[string]string
want string
wantErr bool
}{
{
"base",
nil,
filepath.Join(homedir, defaultConfigDir),
false,
},
{
"env PACKER_CONFIG_DIR",
map[string]string{"PACKER_CONFIG_DIR": packerConfigTempDir},
filepath.Join(packerConfigTempDir, defaultConfigDir),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for k, v := range tt.env {
os.Setenv(k, v)
}
got, err := ConfigDir()
if (err != nil) != tt.wantErr {
t.Errorf(
"Name: %v, ConfigPath() error = %v, wantErr %v",
tt.name,
err,
tt.wantErr)
return
}
if got != tt.want {
t.Errorf(
"Name: %v, ConfigPath() = %v, want %v",
tt.name,
got,
tt.want)
}
resetTestEnv()
})
}
}
func resetTestEnv() {
os.Setenv("PACKER_CONFIG_DIR", "")
}