Skip to content

Commit 4eb5f43

Browse files
author
Vladimir Varankin
committed
fix tests for exercism.paths on systems with $XDG_CONFIG_HOME
In case `$XDG_CONFIG_HOME` is set, tests of paths package fail as config home was not mocked properly. The patch simplifies paths' internals and introduces a more flexible way to mock globals of `paths` package.
1 parent ca8a22e commit 4eb5f43

3 files changed

Lines changed: 26 additions & 22 deletions

File tree

config/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestLoad(t *testing.T) {
2626
t.Fatal(err)
2727
}
2828
paths.Home = tmpDir
29+
paths.ConfigHome = tmpDir
2930

3031
testCases := []struct {
3132
desc string

paths/paths.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ const (
2020
var (
2121
// Home by default will contact the location of your home directory.
2222
Home string
23+
// ConfigHome will contain $XDG_CONFIG_HOME if it exists or Home otherwise.
24+
ConfigHome string
2325

24-
// XDGConfigHome will contain $XDG_CONFIG_HOME if it exists.
25-
XDGConfigHome string
2626
errHomeNotFound = errors.New("unable to locate home directory")
2727
)
2828

2929
func init() {
30+
home, err := findHome()
31+
if err != nil {
32+
panic(err)
33+
}
34+
configHome := os.Getenv("XDG_CONFIG_HOME")
3035
// on startup set default values
31-
Recalculate()
36+
Recalculate(home, configHome)
3237
}
3338

3439
// Config will return the correct input path given any input.
@@ -38,11 +43,7 @@ func init() {
3843
// will be appended.
3944
func Config(path string) string {
4045
if path == "" {
41-
if XDGConfigHome == "" {
42-
return filepath.Join(Home, File)
43-
}
44-
45-
return filepath.Join(XDGConfigHome, File)
46+
return filepath.Join(ConfigHome, File)
4647
}
4748

4849
expandedPath := expandPath(path)
@@ -62,16 +63,17 @@ func Exercises(path string) string {
6263
return expandPath(path)
6364
}
6465

65-
// Recalculate sets exercism paths based on Home.
66-
func Recalculate() {
67-
if Home == "" {
68-
home, err := findHome()
69-
if err != nil {
70-
panic(err)
71-
}
66+
// Recalculate sets exercism paths
67+
func Recalculate(home, configHome string) {
68+
if home != "" {
7269
Home = home
7370
}
74-
XDGConfigHome = os.Getenv("XDG_CONFIG_HOME")
71+
if configHome != "" {
72+
ConfigHome = configHome
73+
}
74+
if ConfigHome == "" {
75+
ConfigHome = Home
76+
}
7577
}
7678

7779
// IsDir determines whether the given path is a valid directory path.

paths/paths_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestExercises(t *testing.T) {
1616
dir, err := os.Getwd()
1717
assert.NoError(t, err)
1818
Home = "/test/home"
19-
Recalculate()
19+
Recalculate(Home, "")
2020

2121
testCases := []struct {
2222
givenPath string
@@ -39,8 +39,7 @@ func TestConfig(t *testing.T) {
3939
dir, err := os.Getwd()
4040
assert.NoError(t, err)
4141

42-
Home = dir
43-
Recalculate()
42+
Recalculate(dir, dir)
4443

4544
testCases := []struct {
4645
desc string
@@ -76,8 +75,10 @@ func TestConfig(t *testing.T) {
7675
}
7776

7877
func TestXDGConfig(t *testing.T) {
79-
XDGConfigHome = "/home/user/.xdg_config"
80-
81-
assert.Equal(t, filepath.Join(XDGConfigHome, File), Config(""))
78+
dir, err := os.Getwd()
79+
assert.NoError(t, err)
80+
xdgConfigHome := "/home/user/.xdg_config"
81+
Recalculate(dir, xdgConfigHome)
8282

83+
assert.Equal(t, filepath.Join(xdgConfigHome, File), Config(""))
8384
}

0 commit comments

Comments
 (0)