Skip to content

Commit

Permalink
little refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Oct 19, 2023
1 parent 6326fa9 commit b599b51
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
12 changes: 6 additions & 6 deletions client_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ const (
)

func getClientConfig(filePathFromConnectionString string) (*ClientConfig, error) {
configPredefinedFilePaths, err := clientConfigPredefinedDirs()
var filePath string
filePath, err = findClientConfigFilePath(filePathFromConnectionString, configPredefinedFilePaths)
configPredefinedFilePaths := clientConfigPredefinedDirs()
filePath, err := findClientConfigFilePath(filePathFromConnectionString, configPredefinedFilePaths)
if err != nil {
return nil, err
}

Check warning on line 34 in client_configuration.go

View check run for this annotation

Codecov / codecov/patch

client_configuration.go#L33-L34

Added lines #L33 - L34 were not covered by tests
Expand Down Expand Up @@ -75,12 +74,13 @@ func existsFile(filePath string) (bool, error) {
return false, err

Check warning on line 74 in client_configuration.go

View check run for this annotation

Codecov / codecov/patch

client_configuration.go#L74

Added line #L74 was not covered by tests
}

func clientConfigPredefinedDirs() ([]string, error) {
func clientConfigPredefinedDirs() []string {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
logger.Warnf("Home dir could not be determined: %w", err)
return []string{".", os.TempDir()}
}

Check warning on line 82 in client_configuration.go

View check run for this annotation

Codecov / codecov/patch

client_configuration.go#L80-L82

Added lines #L80 - L82 were not covered by tests
return []string{".", homeDir, os.TempDir()}, nil
return []string{".", homeDir, os.TempDir()}
}

// ClientConfig config root
Expand Down
20 changes: 3 additions & 17 deletions client_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestFindConfigFileFromEnvVariable(t *testing.T) {
assertEqualE(t, clientConfigFilePath, envConfigPath, "config file path")
}

func TestFindConfigFileFromDriverDirectory(t *testing.T) {
func TestFindConfigFileFromFirstPredefinedDir(t *testing.T) {
dirs := createTestDirectories(t)
driverConfigPath := createFile(t, defaultConfigName, "random content", dirs.driverDir)
createFile(t, defaultConfigName, "random content", dirs.homeDir)
Expand All @@ -50,7 +50,7 @@ func TestFindConfigFileFromDriverDirectory(t *testing.T) {
assertEqualE(t, clientConfigFilePath, driverConfigPath, "config file path")
}

func TestFindConfigFileFromHomeDirectory(t *testing.T) {
func TestFindConfigFileFromSubsequentDirectoryIfNotFoundInPreviousOne(t *testing.T) {
dirs := createTestDirectories(t)
createFile(t, "wrong_file_name.json", "random content", dirs.driverDir)
homeConfigPath := createFile(t, defaultConfigName, "random content", dirs.homeDir)
Expand All @@ -62,18 +62,6 @@ func TestFindConfigFileFromHomeDirectory(t *testing.T) {
assertEqualE(t, clientConfigFilePath, homeConfigPath, "config file path")
}

func TestFindConfigFileFromTempDirectory(t *testing.T) {
dirs := createTestDirectories(t)
createFile(t, "wrong_file_name.json", "random content", dirs.driverDir)
createFile(t, "wrong_file_name.json", "random content", dirs.homeDir)
tempConfigPath := createFile(t, defaultConfigName, "random content", dirs.tempDir)

clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs))

assertNilF(t, err, "get client config error")
assertEqualE(t, clientConfigFilePath, tempConfigPath, "config file path")
}

func TestNotFindConfigFileWhenNotDefined(t *testing.T) {
dirs := createTestDirectories(t)
createFile(t, "wrong_file_name.json", "random content", dirs.driverDir)
Expand All @@ -89,11 +77,9 @@ func TestNotFindConfigFileWhenNotDefined(t *testing.T) {
func TestCreatePredefinedDirs(t *testing.T) {
homeDir, err := os.UserHomeDir()
assertNilF(t, err, "get home dir error")
var locations []string

locations, err = clientConfigPredefinedDirs()
locations := clientConfigPredefinedDirs()

assertNilF(t, err, "error")
assertEqualF(t, len(locations), 3, "size")
assertEqualE(t, locations[0], ".", "driver directory")
assertEqualE(t, locations[1], homeDir, "home directory")
Expand Down

0 comments on commit b599b51

Please sign in to comment.