Skip to content

Commit 7d48322

Browse files
committed
feat(internal/config): add config tests
1 parent bf1c699 commit 7d48322

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

internal/config/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func LoadConfig() error {
3333
func getConfigFilePath() (string, error) {
3434
for _, value := range constants.DajeConfigPathOrder {
3535
currentFilepath := path.Join(constants.DajeBasePath, value, constants.DajeConfigFileName)
36-
if _, err := os.Stat(currentFilepath); err == nil {
36+
_, err := os.Stat(currentFilepath)
37+
if err == nil {
3738
return currentFilepath, nil
3839
}
3940
}

internal/config/config_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
11
package config
2+
3+
import (
4+
"errors"
5+
"os"
6+
"path"
7+
"testing"
8+
9+
"github.com/Schroedinger-Hat/Daje/constants"
10+
)
11+
12+
const configNotExistsFileName = ".confignotexists"
13+
14+
func Test_getConfigFilePathShouldPassWithNoErrorsWhenFindConfigurationFile(t *testing.T) {
15+
workingDirectory, _ := os.Getwd()
16+
constants.DajeBasePath = workingDirectory + "/../../testdata"
17+
18+
_, err := getConfigFilePath()
19+
20+
if err != nil {
21+
t.Fatalf("getConfigFilePath should pass with no errors when find configuration file: expected=\"\" - get=\"%v\"", err)
22+
}
23+
}
24+
25+
func Test_getConfigFilePathShouldReturnTheExactPathWhenItFindTheConfigurationFile(t *testing.T) {
26+
workingDirectory, _ := os.Getwd()
27+
constants.DajeBasePath = workingDirectory + "/../../testdata"
28+
expectedConfigPath := path.Join(constants.DajeBasePath, "../testdata/.config/daje", constants.DajeConfigFileName)
29+
30+
path, _ := getConfigFilePath()
31+
32+
if path != expectedConfigPath {
33+
t.Fatalf("getConfigFilePath should return the exact path when find configuration file: expected=%v - get=%v", expectedConfigPath, path)
34+
}
35+
}
36+
37+
func Test_getConfigFilePathShouldFailWhenDontFindConfigurationFile(t *testing.T) {
38+
constants.DajeConfigFileName = configNotExistsFileName
39+
40+
_, err := getConfigFilePath()
41+
errExpected := errors.New("getConfigFilePath: Configuration not found")
42+
43+
if err.Error() != errExpected.Error() {
44+
t.Fatalf("getConfigFilePath should fail when don't find configuration file: expected=%v - get=%v", errExpected, err)
45+
}
46+
}
47+
48+
func Test_getConfigFilePathShouldHaveEmptyPathWhenDontFindConfigurationFile(t *testing.T) {
49+
constants.DajeConfigFileName = configNotExistsFileName
50+
path, _ := getConfigFilePath()
51+
52+
if path != "" {
53+
t.Fatalf("getConfigFilePath should have empty path when don't find configuration file: expected=\"\" - get=%v", path)
54+
}
55+
}

0 commit comments

Comments
 (0)