Skip to content

Commit 1a2fd1b

Browse files
committed
if our path is a directory append the default filename
1 parent f9848a5 commit 1a2fd1b

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

config/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,14 @@ func (c *Config) resolvePath(argPath string) (string, error) {
167167
if err != nil {
168168
return "", err
169169
}
170-
return strings.Replace(path, "~", h, 1), nil
170+
path = strings.Replace(path, "~", h, 1)
171+
172+
fi, _ := os.Stat(path)
173+
if fi != nil && fi.IsDir() {
174+
path = filepath.Join(path, File)
175+
}
176+
177+
return path, nil
171178
}
172179

173180
func (c *Config) setDefaults() error {

config/config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ func TestLoad(t *testing.T) {
9292
}
9393
}
9494

95+
func TestReadDirectory(t *testing.T) {
96+
// if the provided path is a directory, append the default filename
97+
tmpDir, err := ioutil.TempDir("", "")
98+
assert.NoError(t, err)
99+
100+
myConfig, err := New(tmpDir)
101+
assert.NoError(t, err)
102+
103+
expected := filepath.Join(tmpDir, File)
104+
actual := myConfig.File
105+
assert.Equal(t, expected, actual)
106+
107+
// if it can't determine if the provided path is a directory, don't modify
108+
// the path
109+
myConfig, err = New("badpath")
110+
assert.NoError(t, err)
111+
assert.Equal(t, "badpath", myConfig.File)
112+
}
113+
95114
func TestReadingWritingConfig(t *testing.T) {
96115
tmpDir, err := ioutil.TempDir("", "")
97116
filename := fmt.Sprintf("%s/%s", tmpDir, File)

0 commit comments

Comments
 (0)