@@ -3,29 +3,55 @@ package filediscovery
33import (
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
1516func 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
3157func TestWorkingDirProvider_error (t * testing.T ) {
@@ -45,20 +71,41 @@ func TestWorkingDirProvider_error(t *testing.T) {
4571func 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
64111func 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
107153func 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
127192func TestHomeConfigDirProvider_UserLookupReturnsError (t * testing.T ) {
0 commit comments