forked from hashicorp/packer-plugin-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_file_unix_test.go
More file actions
141 lines (129 loc) · 3.21 KB
/
config_file_unix_test.go
File metadata and controls
141 lines (129 loc) · 3.21 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// +build darwin freebsd linux netbsd openbsd solaris
package pathing
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestConfigPath(t *testing.T) {
// temporary directories for env vars
xdgConfigHomeTempDir, err := ioutil.TempDir(os.TempDir(), "*")
if err != nil {
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
}
defer os.RemoveAll(xdgConfigHomeTempDir)
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)
homeTempDir, err := ioutil.TempDir(os.TempDir(), "*")
if err != nil {
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
}
defer os.RemoveAll(homeTempDir)
homeDirDefaultConfigTempDir, err := ioutil.TempDir(os.TempDir(), "*")
if err != nil {
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
}
err = os.Mkdir(filepath.Join(homeDirDefaultConfigTempDir, defaultConfigDir), 0755)
if err != nil {
t.Fatalf("Failed to create temp test file: failing test: %v", err)
}
defer os.RemoveAll(homeDirDefaultConfigTempDir)
// reset env
packerConfigDir := os.Getenv("PACKER_CONFIG_DIR")
os.Setenv("PACKER_CONFIG_DIR", "")
defer func() {
os.Setenv("PACKER_CONFIG_DIR", packerConfigDir)
}()
xdgConfigHomeDir := os.Getenv("XDG_CONFIG_HOME")
os.Setenv("XDG_CONFIG_HOME", "")
defer func() {
os.Setenv("XDG_CONFIG_HOME", xdgConfigHomeDir)
}()
homeDir := os.Getenv("HOME")
os.Setenv("HOME", "")
defer func() {
os.Setenv("HOME", homeDir)
}()
tests := []struct {
name string
env map[string]string
want string
wantErr bool
}{
{
"no HOME env var",
nil,
"",
true,
},
{
"base",
map[string]string{"HOME": homeTempDir},
filepath.Join(homeTempDir, ".config", "packer"),
false,
},
{
"XDG_CONFIG_HOME set without default file",
map[string]string{
"XDG_CONFIG_HOME": xdgConfigHomeTempDir,
"HOME": homeTempDir,
},
filepath.Join(xdgConfigHomeTempDir, "packer"),
false,
},
{
"env PACKER_CONFIG_DIR",
map[string]string{"PACKER_CONFIG_DIR": packerConfigTempDir},
filepath.Join(packerConfigTempDir, defaultConfigDir),
false,
},
{
"env PACKER_CONFIG_DIR, XDG_CONFIG_HOME",
map[string]string{
"XDG_CONFIG_HOME": xdgConfigHomeTempDir,
"PACKER_CONFIG_DIR": packerConfigTempDir,
},
filepath.Join(packerConfigTempDir, defaultConfigDir),
false,
},
{
"Old Default Config Found",
map[string]string{"HOME": homeDirDefaultConfigTempDir},
filepath.Join(homeDirDefaultConfigTempDir, 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", "")
os.Setenv("XDG_CONFIG_HOME", "")
os.Setenv("HOME", "")
}