Skip to content

Commit 006be29

Browse files
committed
Add tests for changing config and cache configuration. Addressing PR
comments. Adding comment for how the cache configuration works.
1 parent 31918db commit 006be29

8 files changed

Lines changed: 411 additions & 23 deletions

packer/cache.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ import (
1010
// When the directory is not absolute, CachePath will try to make a
1111
// a cache depending on the operating system.
1212
//
13+
// TODO: Update this with better examples
1314
// NOTE: cache directory will change depending on operating system dependent
14-
// ex:
15+
// For Windows:
1516
// PACKER_CACHE_DIR="" CacheDir() => "./packer_cache/
1617
// PACKER_CACHE_DIR="" CacheDir("foo") => "./packer_cache/foo
1718
// PACKER_CACHE_DIR="bar" CacheDir("foo") => "./bar/foo
1819
// PACKER_CACHE_DIR="/home/there" CacheDir("foo", "bar") => "/home/there/foo/bar
20+
// For Unix:
21+
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="", Default_config CacheDir() => "$HOME/cache/packer"
22+
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="", Default_config CacheDir("foo") => "$HOME/cache/packer/foo"
23+
// PACKER_CACHE_DIR="bar", XDG_CONFIG_HOME="", Default_config CacheDir("foo") => "./bar/foo
24+
// PACKER_CACHE_DIR="/home/there", XDG_CONFIG_HOME="", Default_config CacheDir("foo", "bar") => "/home/there/foo/bar
1925
func CachePath(paths ...string) (path string, err error) {
2026
defer func() {
2127
// create the dir based on return path if it doesn't exist
@@ -27,9 +33,5 @@ func CachePath(paths ...string) (path string, err error) {
2733
}
2834

2935
paths = append([]string{cacheDir}, paths...)
30-
result, err := filepath.Abs(filepath.Join(paths...))
31-
if err != nil {
32-
return "", err
33-
}
34-
return result, err
36+
return filepath.Abs(filepath.Join(paths...))
3537
}

packer/cache_config_unix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ func getDefaultCacheDir() string {
1313
if xdgCacheHome := os.Getenv("XDG_CACHE_HOME"); xdgCacheHome != "" {
1414
return filepath.Join(xdgCacheHome, "packer")
1515
}
16-
homeDir := os.Getenv("HOME")
17-
defaultConfigFileDir = filepath.Join(homeDir, ".cache", "packer")
18-
}
16+
17+
homeDir := os.Getenv("HOME")
18+
defaultConfigFileDir = filepath.Join(homeDir, ".cache", "packer")
1919

2020
return defaultConfigFileDir
2121
}

packer/cache_config_unix_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// +build darwin freebsd linux netbsd openbsd solaris
2+
3+
package packer
4+
5+
import (
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
)
11+
12+
func TestCachePath(t *testing.T) {
13+
// temporary directories for env vars
14+
xdgCacheHomeTempDir, err := ioutil.TempDir(os.TempDir(), "*")
15+
if err != nil {
16+
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
17+
}
18+
defer os.RemoveAll(xdgCacheHomeTempDir)
19+
packerCacheTempDir, err := ioutil.TempDir(os.TempDir(), "*")
20+
if err != nil {
21+
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
22+
}
23+
defer os.RemoveAll(packerCacheTempDir)
24+
25+
// reset env
26+
packerCacheDir := os.Getenv("PACKER_CACHE_DIR")
27+
os.Setenv("PACKER_CACHE_DIR", "")
28+
defer func() {
29+
os.Setenv("PACKER_CACHE_DIR", packerCacheDir)
30+
}()
31+
32+
xdgCacheHomeDir := os.Getenv("XDG_CACHE_HOME")
33+
os.Setenv("XDG_CACHE_HOME", "")
34+
defer func() {
35+
os.Setenv("XDG_CACHE_HOME", xdgCacheHomeDir)
36+
}()
37+
38+
type args struct {
39+
paths []string
40+
}
41+
tests := []struct {
42+
name string
43+
args args
44+
env map[string]string
45+
want string
46+
wantErr bool
47+
}{
48+
{
49+
"base",
50+
args{},
51+
nil,
52+
filepath.Join(os.Getenv("HOME"), ".cache", "packer"),
53+
false,
54+
},
55+
{
56+
"base and path",
57+
args{[]string{"a", "b"}},
58+
nil,
59+
filepath.Join(os.Getenv("HOME"), ".cache", "packer", "a", "b"),
60+
false,
61+
},
62+
{
63+
"env PACKER_CACHE_DIR and path",
64+
args{[]string{"a", "b"}},
65+
map[string]string{"PACKER_CACHE_DIR": packerCacheTempDir},
66+
filepath.Join(packerCacheTempDir, "a", "b"),
67+
false,
68+
},
69+
{
70+
"env XDG_CACHE_HOME and path",
71+
args{[]string{"a", "b"}},
72+
map[string]string{"XDG_CACHE_HOME": xdgCacheHomeTempDir},
73+
filepath.Join(xdgCacheHomeTempDir, "packer", "a", "b"),
74+
false,
75+
},
76+
{
77+
"env PACKER_CACHE_DIR, XDG_CACHE_HOME, and path",
78+
args{[]string{"a", "b"}},
79+
map[string]string{
80+
"XDG_CACHE_HOME": xdgCacheHomeTempDir,
81+
"PACKER_CACHE_DIR": packerCacheTempDir,
82+
},
83+
filepath.Join(packerCacheTempDir, "a", "b"),
84+
false,
85+
},
86+
}
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
for k, v := range tt.env {
90+
os.Setenv(k, v)
91+
}
92+
got, err := CachePath(tt.args.paths...)
93+
if (err != nil) != tt.wantErr {
94+
t.Errorf("CachePath() error = %v, wantErr %v", err, tt.wantErr)
95+
return
96+
}
97+
if got != tt.want {
98+
t.Errorf("CachePath() = %v, want %v", got, tt.want)
99+
}
100+
resetTestEnv()
101+
})
102+
}
103+
}
104+
105+
func resetTestEnv() {
106+
os.Setenv("PACKER_CACHE_DIR", "")
107+
os.Setenv("XDG_CACHE_HOME", "")
108+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// +build windows
2+
3+
package packer
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
)
10+
11+
func TestCachePath(t *testing.T) {
12+
wd, err := os.Getwd()
13+
if err != nil {
14+
t.Fatalf("Getwd: %v", err)
15+
}
16+
tmp := os.TempDir()
17+
18+
// reset env
19+
cd := os.Getenv("PACKER_CACHE_DIR")
20+
os.Setenv("PACKER_CACHE_DIR", "")
21+
defer func() {
22+
os.Setenv("PACKER_CACHE_DIR", cd)
23+
}()
24+
25+
type args struct {
26+
paths []string
27+
}
28+
tests := []struct {
29+
name string
30+
args args
31+
env map[string]string
32+
want string
33+
wantErr bool
34+
}{
35+
{"base", args{}, nil, filepath.Join(wd, "packer_cache"), false},
36+
{"base and path", args{[]string{"a", "b"}}, nil, filepath.Join(wd, "packer_cache", "a", "b"), false},
37+
{"env and path", args{[]string{"a", "b"}}, map[string]string{"PACKER_CACHE_DIR": tmp}, filepath.Join(tmp, "a", "b"), false},
38+
}
39+
for _, tt := range tests {
40+
t.Run(tt.name, func(t *testing.T) {
41+
for k, v := range tt.env {
42+
os.Setenv(k, v)
43+
}
44+
got, err := CachePath(tt.args.paths...)
45+
if (err != nil) != tt.wantErr {
46+
t.Errorf("CachePath() error = %v, wantErr %v", err, tt.wantErr)
47+
return
48+
}
49+
if got != tt.want {
50+
t.Errorf("CachePath() = %v, want %v", got, tt.want)
51+
}
52+
})
53+
}
54+
}

pathing/config_file_unix.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package pathing
44

55
import (
6+
"errors"
67
"log"
78
"os"
89
"path/filepath"
@@ -14,15 +15,20 @@ const (
1415
)
1516

1617
func configDir() (path string, err error) {
18+
19+
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
20+
log.Printf("Detected config directory from env var: %s", cd)
21+
return filepath.Join(cd, defaultConfigDir), nil
22+
}
23+
1724
var dir string
1825
homedir := os.Getenv("HOME")
26+
1927
if homedir == "" {
20-
return "", err
28+
return "", errors.New("No $HOME environment variable found, required to set Config Directory")
2129
}
22-
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
23-
log.Printf("Detected config directory from env var: %s", cd)
24-
dir = filepath.Join(cd, defaultConfigDir)
25-
} else if hasDefaultConfigFileLocation(homedir) {
30+
31+
if hasDefaultConfigFileLocation(homedir) {
2632
dir = filepath.Join(homedir, defaultConfigDir)
2733
log.Printf("Old default config directory found: %s", dir)
2834
} else if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {

pathing/config_file_unix_test.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// +build darwin freebsd linux netbsd openbsd solaris
2+
3+
package pathing
4+
5+
import (
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
)
11+
12+
func TestConfigPath(t *testing.T) {
13+
// temporary directories for env vars
14+
xdgConfigHomeTempDir, err := ioutil.TempDir(os.TempDir(), "*")
15+
if err != nil {
16+
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
17+
}
18+
defer os.RemoveAll(xdgConfigHomeTempDir)
19+
20+
packerConfigTempDir, err := ioutil.TempDir(os.TempDir(), "*")
21+
if err != nil {
22+
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
23+
}
24+
defer os.RemoveAll(packerConfigTempDir)
25+
26+
homeTempDir, err := ioutil.TempDir(os.TempDir(), "*")
27+
if err != nil {
28+
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
29+
}
30+
defer os.RemoveAll(homeTempDir)
31+
32+
homeDirDefaultConfigTempDir, err := ioutil.TempDir(os.TempDir(), "*")
33+
if err != nil {
34+
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
35+
}
36+
37+
err = os.Mkdir(filepath.Join(homeDirDefaultConfigTempDir, defaultConfigDir), 0755)
38+
if err != nil {
39+
t.Fatalf("Failed to create temp test file: failing test: %v", err)
40+
}
41+
defer os.RemoveAll(homeDirDefaultConfigTempDir)
42+
43+
// reset env
44+
packerConfigDir := os.Getenv("PACKER_CONFIG_DIR")
45+
os.Setenv("PACKER_CONFIG_DIR", "")
46+
defer func() {
47+
os.Setenv("PACKER_CONFIG_DIR", packerConfigDir)
48+
}()
49+
50+
xdgConfigHomeDir := os.Getenv("XDG_CONFIG_HOME")
51+
os.Setenv("XDG_CONFIG_HOME", "")
52+
defer func() {
53+
os.Setenv("XDG_CONFIG_HOME", xdgConfigHomeDir)
54+
}()
55+
56+
homeDir := os.Getenv("HOME")
57+
os.Setenv("HOME", "")
58+
defer func() {
59+
os.Setenv("HOME", homeDir)
60+
}()
61+
62+
tests := []struct {
63+
name string
64+
env map[string]string
65+
want string
66+
wantErr bool
67+
}{
68+
{
69+
"no HOME env var",
70+
nil,
71+
"",
72+
true,
73+
},
74+
{
75+
"base",
76+
map[string]string{"HOME": homeTempDir},
77+
filepath.Join(homeTempDir, ".config", "packer"),
78+
false,
79+
},
80+
{
81+
"XDG_CONFIG_HOME set without default file",
82+
map[string]string{
83+
"XDG_CONFIG_HOME": xdgConfigHomeTempDir,
84+
"HOME": homeTempDir,
85+
},
86+
filepath.Join(xdgConfigHomeTempDir, "packer"),
87+
false,
88+
},
89+
{
90+
"env PACKER_CONFIG_DIR",
91+
map[string]string{"PACKER_CONFIG_DIR": packerConfigTempDir},
92+
filepath.Join(packerConfigTempDir, defaultConfigDir),
93+
false,
94+
},
95+
{
96+
"env PACKER_CONFIG_DIR, XDG_CONFIG_HOME",
97+
map[string]string{
98+
"XDG_CONFIG_HOME": xdgConfigHomeTempDir,
99+
"PACKER_CONFIG_DIR": packerConfigTempDir,
100+
},
101+
filepath.Join(packerConfigTempDir, defaultConfigDir),
102+
false,
103+
},
104+
{
105+
"Old Default Config Found",
106+
map[string]string{"HOME": homeDirDefaultConfigTempDir},
107+
filepath.Join(homeDirDefaultConfigTempDir, defaultConfigDir),
108+
false,
109+
},
110+
}
111+
for _, tt := range tests {
112+
t.Run(tt.name, func(t *testing.T) {
113+
for k, v := range tt.env {
114+
os.Setenv(k, v)
115+
}
116+
got, err := ConfigDir()
117+
if (err != nil) != tt.wantErr {
118+
t.Errorf(
119+
"Name: %v, ConfigPath() error = %v, wantErr %v",
120+
tt.name,
121+
err,
122+
tt.wantErr)
123+
return
124+
}
125+
if got != tt.want {
126+
t.Errorf(
127+
"Name: %v, ConfigPath() = %v, want %v",
128+
tt.name,
129+
got,
130+
tt.want)
131+
}
132+
resetTestEnv()
133+
})
134+
}
135+
}
136+
137+
func resetTestEnv() {
138+
os.Setenv("PACKER_CONFIG_DIR", "")
139+
os.Setenv("XDG_CONFIG_HOME", "")
140+
os.Setenv("HOME", "")
141+
}

0 commit comments

Comments
 (0)