Skip to content

Commit 4da0ea4

Browse files
author
Oppodelldog
committed
enable to configure directory providers to search in a deeper path
1 parent d483bd7 commit 4da0ea4

3 files changed

Lines changed: 118 additions & 42 deletions

File tree

discovery_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"path"
88
"testing"
99

10-
"github.com/stretchr/testify/assert"
1110
"fmt"
1211
"io/ioutil"
12+
13+
"github.com/stretchr/testify/assert"
1314
)
1415

1516
func TestNew(t *testing.T) {
@@ -129,7 +130,7 @@ func TestFileDiscovery_Discover_ifFileWasFoundReturnsFilePath(t *testing.T) {
129130
func ExampleFileDiscovery_Discover() {
130131

131132
// for this demonstration we create a test file in /tmp
132-
testFilePath := "/tmp/test-file.yml";
133+
testFilePath := "/tmp/test-file.yml"
133134
ioutil.WriteFile(testFilePath, []byte("test"), 0666)
134135

135136
// Discovery needs at least one FileLocationProvider which provides a file location to search for.

providers.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,43 @@ package filediscovery
33
import (
44
"fmt"
55
"os"
6+
"os/user"
67
"path"
78
"path/filepath"
8-
"os/user"
99
)
1010

1111
var workingDirProviderFunc = os.Getwd
1212

1313
// WorkingDirProvider provides the working directory as a possible file location
14-
func WorkingDirProvider() FileLocationProvider {
14+
func WorkingDirProvider(subFolders ...string) FileLocationProvider {
1515

1616
return func(fileName string) (string, error) {
1717
dir, err := workingDirProviderFunc()
1818
if err != nil {
1919
return "", err
2020
}
2121

22-
return path.Join(dir, fileName), nil
22+
subFoldersPath := createPath(subFolders...)
23+
24+
return path.Join(dir, subFoldersPath, fileName), nil
2325
}
2426
}
2527

2628
var executableDirProviderFunc = os.Executable
2729

2830
// ExecutableDirProvider provides the executables directory as a possible file location
29-
func ExecutableDirProvider() FileLocationProvider {
31+
func ExecutableDirProvider(subFolders ...string) FileLocationProvider {
3032

3133
return func(fileName string) (string, error) {
3234
dir, err := executableDirProviderFunc()
3335
if err != nil {
3436
return "", err
3537
}
3638

37-
return path.Join(filepath.Dir(dir), fileName), nil
39+
executableDir := filepath.Dir(dir)
40+
subFoldersPath := createPath(subFolders...)
41+
42+
return path.Join(executableDir, subFoldersPath, fileName), nil
3843
}
3944
}
4045

@@ -54,7 +59,6 @@ func EnvVarFilePathProvider(envVar string) FileLocationProvider {
5459
}
5560
}
5661

57-
5862
var homeFolderLookupFunc = user.Current
5963

6064
// HomeConfigDirProvider provides the working directory as a possible file location
@@ -66,11 +70,17 @@ func HomeConfigDirProvider(subFolders ...string) FileLocationProvider {
6670
return "", err
6771
}
6872

69-
subfoldersPath := ""
70-
for _, subfolder := range subFolders {
71-
subfoldersPath = path.Join(subfoldersPath, subfolder)
72-
}
73+
subFoldersPath := createPath(subFolders...)
7374

74-
return path.Join(usr.HomeDir, subfoldersPath, fileName), nil
75+
return path.Join(usr.HomeDir, subFoldersPath, fileName), nil
7576
}
76-
}
77+
}
78+
79+
func createPath(subFolders ...string) string {
80+
subFoldersPath := ""
81+
for _, subfolder := range subFolders {
82+
subFoldersPath = path.Join(subFoldersPath, subfolder)
83+
}
84+
85+
return subFoldersPath
86+
}

providers_test.go

Lines changed: 93 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,55 @@ package filediscovery
33
import (
44
"os"
55
"path"
6-
"path/filepath"
76
"testing"
87

98
"errors"
109

11-
"github.com/stretchr/testify/assert"
1210
"os/user"
11+
"path/filepath"
12+
13+
"github.com/stretchr/testify/assert"
1314
)
1415

1516
func TestWorkingDirProvider(t *testing.T) {
16-
testFileName := "testfile"
17-
provider := WorkingDirProvider()
18-
result, err := provider(testFileName)
19-
if err != nil {
20-
t.Fatalf("Did not expect provider to return an error, but got: %v", err)
21-
}
2217

2318
wd, err := os.Getwd()
2419
if err != nil {
2520
t.Fatalf("Did not expect os.Getwd to return an error, but got: %v", err)
2621
}
2722

28-
assert.Equal(t, path.Join(wd, testFileName), result)
23+
testFileName := "testfile"
24+
25+
testDataSet := map[string]struct {
26+
SubFolders []string
27+
ExpectedPath string
28+
}{
29+
"simple call": {
30+
SubFolders: []string{},
31+
ExpectedPath: path.Join(wd, testFileName),
32+
},
33+
"one subdir": {
34+
SubFolders: []string{"subdir1"},
35+
ExpectedPath: path.Join(wd, "subdir1", testFileName),
36+
},
37+
"two subdirs": {
38+
SubFolders: []string{"subdir1", "subdir2"},
39+
ExpectedPath: path.Join(wd, "subdir1", "subdir2", testFileName),
40+
},
41+
}
42+
43+
for testCaseName, testData := range testDataSet {
44+
t.Run(testCaseName, func(t *testing.T) {
45+
46+
provider := WorkingDirProvider(testData.SubFolders...)
47+
result, err := provider(testFileName)
48+
if err != nil {
49+
t.Fatalf("Did not expect provider to return an error, but got: %v", err)
50+
}
51+
52+
assert.Equal(t, testData.ExpectedPath, result)
53+
})
54+
}
2955
}
3056

3157
func TestWorkingDirProvider_error(t *testing.T) {
@@ -45,20 +71,41 @@ func TestWorkingDirProvider_error(t *testing.T) {
4571
func TestExecutableDirProvider(t *testing.T) {
4672
testFileName := "testfile"
4773

48-
provider := ExecutableDirProvider()
49-
result, err := provider(testFileName)
50-
if err != nil {
51-
t.Fatalf("Did not expect provider to return an error, but got: %v", err)
52-
}
53-
5474
executableFilePath, err := os.Executable()
5575
if err != nil {
5676
t.Fatalf("Did not expect os.Executable to return an error, but got: %v", err)
5777
}
78+
executableFilePath = filepath.Dir(executableFilePath)
79+
80+
testDataSet := map[string]struct {
81+
SubFolders []string
82+
ExpectedPath string
83+
}{
84+
"simple call": {
85+
SubFolders: []string{},
86+
ExpectedPath: path.Join(executableFilePath, testFileName),
87+
},
88+
"one subdir": {
89+
SubFolders: []string{"subdir1"},
90+
ExpectedPath: path.Join(executableFilePath, "subdir1", testFileName),
91+
},
92+
"two subdirs": {
93+
SubFolders: []string{"subdir1", "subdir2"},
94+
ExpectedPath: path.Join(executableFilePath, "subdir1", "subdir2", testFileName),
95+
},
96+
}
5897

59-
expectedFilePath := path.Join(filepath.Dir(executableFilePath), testFileName)
98+
for testCaseName, testData := range testDataSet {
99+
t.Run(testCaseName, func(t *testing.T) {
100+
provider := ExecutableDirProvider(testData.SubFolders...)
101+
result, err := provider(testFileName)
102+
if err != nil {
103+
t.Fatalf("Did not expect provider to return an error, but got: %v", err)
104+
}
60105

61-
assert.Equal(t, expectedFilePath, result)
106+
assert.Equal(t, testData.ExpectedPath, result)
107+
})
108+
}
62109
}
63110

64111
func TestExecutableDirProvider_error(t *testing.T) {
@@ -101,27 +148,45 @@ func TestEnvVarFilePathProvider_error(t *testing.T) {
101148
testFileName := "" // not necessary for this test, since filename comes from env var
102149
_, err := provider(testFileName)
103150
assert.Error(t, err)
104-
105151
}
106152

107153
func TestHomeConfigDirProvider(t *testing.T) {
108154
testFileName := "testfile"
109155

110-
subfolder1 := ".config"
111-
subfolder2 := "some-project"
112-
provider := HomeConfigDirProvider(subfolder1, subfolder2)
113-
result, err := provider(testFileName)
114-
if err != nil {
115-
t.Fatalf("Did not expect provider to return an error, but got: %v", err)
116-
}
117-
118156
usr, err := user.Current()
119157
if err != nil {
120158
t.Fatalf("Did not expect user.Current to return an error, but got: %v", err)
121159
}
122160

123-
expectedFilepath := path.Join(usr.HomeDir, subfolder1, subfolder2, testFileName)
124-
assert.Equal(t, expectedFilepath, result)
161+
testDataSet := map[string]struct {
162+
SubFolders []string
163+
ExpectedPath string
164+
}{
165+
"simple call": {
166+
SubFolders: []string{},
167+
ExpectedPath: path.Join(usr.HomeDir, testFileName),
168+
},
169+
"one subdir": {
170+
SubFolders: []string{"subdir1"},
171+
ExpectedPath: path.Join(usr.HomeDir, "subdir1", testFileName),
172+
},
173+
"two subdirs": {
174+
SubFolders: []string{"subdir1", "subdir2"},
175+
ExpectedPath: path.Join(usr.HomeDir, "subdir1", "subdir2", testFileName),
176+
},
177+
}
178+
179+
for testCaseName, testData := range testDataSet {
180+
t.Run(testCaseName, func(t *testing.T) {
181+
provider := HomeConfigDirProvider(testData.SubFolders...)
182+
result, err := provider(testFileName)
183+
if err != nil {
184+
t.Fatalf("Did not expect provider to return an error, but got: %v", err)
185+
}
186+
187+
assert.Equal(t, testData.ExpectedPath, result)
188+
})
189+
}
125190
}
126191

127192
func TestHomeConfigDirProvider_UserLookupReturnsError(t *testing.T) {

0 commit comments

Comments
 (0)