Skip to content

Commit

Permalink
SNOW-999076: Fix application directory (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pbulawa authored Feb 19, 2024
1 parent bd9aa25 commit c205278
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
16 changes: 14 additions & 2 deletions client_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion client_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"testing"
)
Expand Down Expand Up @@ -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")
}

Expand Down
55 changes: 39 additions & 16 deletions easy_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"testing"
)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c205278

Please sign in to comment.