Repository: snowflakedb/gosnowflake
Pull request #1055 (2024-02-19 15:15:19)

SNOW-999076: Fix application directory
### Description
Fix application directory which is used to get the config file.

### Checklist
- [x] Code compiles correctly
- [x] Run ``make fmt`` to fix inconsistent formats
- [x] Run ``make lint`` to get lint errors and fix all of them
- [x] Created tests which fail without the change (if possible)
- [x] All tests passing
- [x] Extended the README / documentation, if necessary



Patch:
diff --git a/client_configuration.go b/client_configuration.go
index bb0c0e8..0f6526d 100644
--- a/client_configuration.go
+++ b/client_configuration.go
@@ -8,6 +8,7 @@ import (
 	"fmt"
 	"os"
 	"path"
+	"path/filepath"
 	"runtime"
 	"strings"
 )
@@ -82,12 +83,23 @@ func existsFile(filePath string) (bool, error) {
 }
 
 func clientConfigPredefinedDirs() []string {
+	var predefinedDirs []string
+	exeFile, err := os.Executable()
+	if err != nil {
+		logger.Warnf("Unable to access the application directory for client configuration search, err: %v", err)
+	} else {
+		predefinedDirs = append(predefinedDirs, filepath.Dir(exeFile))
+	}
 	homeDir, err := os.UserHomeDir()
 	if err != nil {
 		logger.Warnf("Unable to access Home directory for client configuration search, err: %v", err)
-		return []string{"."}
+	} else {
+		predefinedDirs = append(predefinedDirs, homeDir)
+	}
+	if predefinedDirs == nil {
+		return []string{}
 	}
-	return []string{".", homeDir}
+	return predefinedDirs
 }
 
 // ClientConfig config root


Test patch:
diff --git a/client_configuration_test.go b/client_configuration_test.go
index 5a9e7f7..0565f95 100644
--- a/client_configuration_test.go
+++ b/client_configuration_test.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"os"
 	"path"
+	"path/filepath"
 	"strings"
 	"testing"
 )
@@ -71,13 +72,15 @@ func TestNotFindConfigFileWhenNotDefined(t *testing.T) {
 }
 
 func TestCreatePredefinedDirs(t *testing.T) {
+	exeDir, _ := os.Executable()
+	appDir := filepath.Dir(exeDir)
 	homeDir, err := os.UserHomeDir()
 	assertNilF(t, err, "get home dir error")
 
 	locations := clientConfigPredefinedDirs()
 
 	assertEqualF(t, len(locations), 2, "size")
-	assertEqualE(t, locations[0], ".", "driver directory")
+	assertEqualE(t, locations[0], appDir, "driver directory")
 	assertEqualE(t, locations[1], homeDir, "home directory")
 }
 
diff --git a/easy_logging_test.go b/easy_logging_test.go
index a2f9be9..f9ba57b 100644
--- a/easy_logging_test.go
+++ b/easy_logging_test.go
@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"os"
 	"path"
+	"path/filepath"
 	"strings"
 	"testing"
 )
@@ -35,23 +36,45 @@ func TestInitializeEasyLoggingOnlyOnceWhenConfigGivenAsAParameter(t *testing.T)
 }
 
 func TestConfigureEasyLoggingOnlyOnceWhenInitializedWithoutConfigFilePath(t *testing.T) {
-	defer cleanUp()
-	configDir, err := os.UserHomeDir()
-	logDir := t.TempDir()
-	assertNilF(t, err, "user home directory error")
-	logLevel := levelError
-	contents := createClientConfigContent(logLevel, logDir)
-	configFilePath := createFile(t, defaultConfigName, contents, configDir)
-	defer os.Remove(configFilePath)
-	easyLoggingInitTrials.reset()
-
-	err = openWithClientConfigFile(t, "")
-	assertNilF(t, err, "open config error")
-	err = openWithClientConfigFile(t, "")
-	assertNilF(t, err, "open config error")
+	appExe, err := os.Executable()
+	assertNilF(t, err, "application exe not accessible")
+	userHome, err := os.UserHomeDir()
+	assertNilF(t, err, "user home directory not accessible")
+
+	testcases := []struct {
+		name string
+		dir  string
+	}{
+		{
+			name: "user home directory",
+			dir:  userHome,
+		},
+		{
+			name: "application directory",
+			dir:  filepath.Dir(appExe),
+		},
+	}
 
-	assertEqualE(t, toClientConfigLevel(logger.GetLogLevel()), logLevel, "error log level check")
-	assertEqualE(t, easyLoggingInitTrials.configureCounter, 1)
+	for _, test := range testcases {
+		t.Run(test.name, func(t *testing.T) {
+			defer cleanUp()
+			logDir := t.TempDir()
+			assertNilF(t, err, "user home directory error")
+			logLevel := levelError
+			contents := createClientConfigContent(logLevel, logDir)
+			configFilePath := createFile(t, defaultConfigName, contents, test.dir)
+			defer os.Remove(configFilePath)
+			easyLoggingInitTrials.reset()
+
+			err = openWithClientConfigFile(t, "")
+			assertNilF(t, err, "open config error")
+			err = openWithClientConfigFile(t, "")
+			assertNilF(t, err, "open config error")
+
+			assertEqualE(t, toClientConfigLevel(logger.GetLogLevel()), logLevel, "error log level check")
+			assertEqualE(t, easyLoggingInitTrials.configureCounter, 1)
+		})
+	}
 }
 
 func TestReconfigureEasyLoggingIfConfigPathWasNotGivenForTheFirstTime(t *testing.T) {