Skip to content

Commit 0ad8206

Browse files
authored
Merge pull request #344 from narqo/fix-test-paths
Be a XDG-friendly tool
2 parents ca8a22e + 19e3d23 commit 0ad8206

5 files changed

Lines changed: 54 additions & 45 deletions

File tree

cmd/debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func Debug(ctx *cli.Context) error {
7070
fmt.Println("API Key: Please set your API Key to access all of the CLI features")
7171
}
7272
} else {
73-
fmt.Println("Config file: <not configured>")
73+
fmt.Printf("Config file: %s (not configured)\n", c.File)
7474
fmt.Println("API Key: Please set your API Key to access all of the CLI features")
7575
}
7676
fmt.Printf("Exercises Directory: %s\n", c.Dir)

config/config.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,20 @@ type Config struct {
3434

3535
// New returns a configuration struct with content from the exercism.json file
3636
func New(path string) (*Config, error) {
37-
c := &Config{}
38-
err := c.load(paths.Config(path))
37+
configPath := paths.Config(path)
38+
_, err := os.Stat(configPath)
39+
if err != nil && os.IsNotExist(err) {
40+
if path == "" {
41+
configPath = paths.DefaultConfig
42+
}
43+
} else if err != nil {
44+
return nil, err
45+
}
46+
47+
c := &Config{
48+
File: configPath,
49+
}
50+
err = c.load()
3951
return c, err
4052
}
4153

@@ -84,9 +96,7 @@ func (c *Config) Write() error {
8496
return nil
8597
}
8698

87-
func (c *Config) load(argPath string) error {
88-
c.File = argPath
89-
99+
func (c *Config) load() error {
90100
if err := c.read(); err != nil {
91101
return err
92102
}

config/config_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ func TestLoad(t *testing.T) {
1717
if err != nil {
1818
t.Fatal(err)
1919
}
20-
configPath := filepath.Join(tmpDir, "config.json")
20+
21+
paths.Home = tmpDir
22+
paths.ConfigHome = tmpDir
23+
paths.DefaultConfig = filepath.Join(tmpDir, "default.json")
24+
25+
configPath := filepath.Join(paths.ConfigHome, "config.json")
2126
if err := os.Link(fixturePath(t, "config.json"), configPath); err != nil {
2227
t.Fatal(err)
2328
}
24-
dirtyPath := filepath.Join(tmpDir, "dirty.json")
29+
dirtyPath := filepath.Join(paths.ConfigHome, "dirty.json")
2530
if err := os.Link(fixturePath(t, "dirty.json"), dirtyPath); err != nil {
2631
t.Fatal(err)
2732
}
28-
paths.Home = tmpDir
2933

3034
testCases := []struct {
3135
desc string
@@ -36,7 +40,7 @@ func TestLoad(t *testing.T) {
3640
{
3741
desc: "defaults",
3842
in: "",
39-
out: paths.Config(""),
43+
out: paths.DefaultConfig,
4044
dir: paths.Exercises(""),
4145
key: "",
4246
api: hostAPI,

paths/paths.go

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
const (
1212
// File is the default name of the JSON file where the config written.
1313
// The user can pass an alternate filename when using the CLI.
14-
File = ".exercism.json"
14+
File = "exercism.json"
1515
// DirExercises is the default name of the directory for active users.
1616
// Make this non-exported when handlers.Login is deleted.
1717
DirExercises = "exercism"
@@ -20,31 +20,37 @@ 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 is set or default config home directory.
24+
ConfigHome string
25+
// DefaultConfig will contain default path to config, according to Home
26+
DefaultConfig string
2327

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

2931
func init() {
30-
// on startup set default values
31-
Recalculate()
32+
var err error
33+
Home, err = findHome()
34+
if err != nil {
35+
panic(err)
36+
}
37+
ConfigHome = os.Getenv("XDG_CONFIG_HOME")
38+
if ConfigHome == "" {
39+
ConfigHome = filepath.Join(Home, ".config")
40+
}
41+
DefaultConfig = filepath.Join(Home, "." + File)
3242
}
3343

3444
// Config will return the correct input path given any input.
35-
// Blank input will return the default configuration location.
45+
// Blank input will return the default configuration location based
46+
// on ConfigHome.
3647
// Non-blank input will expand home to be an absolute path.
3748
// If the target is known to be a directory, the config filename
3849
// will be appended.
3950
func Config(path string) string {
4051
if path == "" {
41-
if XDGConfigHome == "" {
42-
return filepath.Join(Home, File)
43-
}
44-
45-
return filepath.Join(XDGConfigHome, File)
52+
return filepath.Join(ConfigHome, File)
4653
}
47-
4854
expandedPath := expandPath(path)
4955
if IsDir(path) {
5056
expandedPath = filepath.Join(expandedPath, File)
@@ -62,18 +68,6 @@ func Exercises(path string) string {
6268
return expandPath(path)
6369
}
6470

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-
}
72-
Home = home
73-
}
74-
XDGConfigHome = os.Getenv("XDG_CONFIG_HOME")
75-
}
76-
7771
// IsDir determines whether the given path is a valid directory path.
7872
func IsDir(path string) bool {
7973
fi, _ := os.Stat(path)
@@ -114,7 +108,7 @@ func makeAbsolute(path string) string {
114108
}
115109

116110
func expandHome(path string) string {
117-
if path[:2] == "~"+string(os.PathSeparator) {
111+
if strings.HasPrefix(path, "~"+string(os.PathSeparator)) {
118112
return strings.Replace(path, "~", Home, 1)
119113
}
120114
return path

paths/paths_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ func TestHome(t *testing.T) {
1212
assert.Equal(t, os.Getenv("HOME"), Home)
1313
}
1414

15+
func TestConfigHome(t *testing.T) {
16+
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
17+
if xdgConfigHome == "" {
18+
assert.Equal(t, filepath.Join(Home, ".config"), ConfigHome)
19+
} else {
20+
assert.Equal(t, xdgConfigHome, ConfigHome)
21+
}
22+
}
23+
1524
func TestExercises(t *testing.T) {
1625
dir, err := os.Getwd()
1726
assert.NoError(t, err)
1827
Home = "/test/home"
19-
Recalculate()
2028

2129
testCases := []struct {
2230
givenPath string
@@ -40,7 +48,7 @@ func TestConfig(t *testing.T) {
4048
assert.NoError(t, err)
4149

4250
Home = dir
43-
Recalculate()
51+
ConfigHome = dir
4452

4553
testCases := []struct {
4654
desc string
@@ -50,7 +58,7 @@ func TestConfig(t *testing.T) {
5058
{
5159
"blank path",
5260
"",
53-
filepath.Join(Home, ".exercism.json"),
61+
filepath.Join(ConfigHome, File),
5462
},
5563
{
5664
"unknown path is expanded, but not modified",
@@ -74,10 +82,3 @@ func TestConfig(t *testing.T) {
7482
assert.Equal(t, tc.expectedPath, actual, tc.desc)
7583
}
7684
}
77-
78-
func TestXDGConfig(t *testing.T) {
79-
XDGConfigHome = "/home/user/.xdg_config"
80-
81-
assert.Equal(t, filepath.Join(XDGConfigHome, File), Config(""))
82-
83-
}

0 commit comments

Comments
 (0)