From c07803b4aaa355032dc868ff1ca22c0ea578f0af Mon Sep 17 00:00:00 2001 From: Eden Zimbelman Date: Thu, 5 Sep 2024 22:21:51 -0700 Subject: [PATCH] fix: gather configured settings using a generic filepath separator (#130) --- CHANGELOG.md | 1 + pkg/config/config.go | 19 +++++++++++-------- pkg/config/config_test.go | 24 ++++++++++++++++++++---- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 114ed94..5a767e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ to [Semantic Versioning][semver]. ### Fixed - Include the manual pages as part of the release artifacts +- Adjust filepath separators to gather configured settings ### Maintenance diff --git a/pkg/config/config.go b/pkg/config/config.go index a18f6fc..2c19953 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "os" + "path/filepath" "time" "github.com/spf13/afero" @@ -45,19 +46,21 @@ func Load( cfg Config, err error, ) { - homeDir, err := os.UserHomeDir() - if err != nil { - return Config{}, err - } - configDir := homeDir + "/.config/etime" - if val, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok { - configDir = val + "/etime" + configDir := "" + if home, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok { + configDir = filepath.Join(home, "etime") + } else { + home, err := os.UserHomeDir() + if err != nil { + return Config{}, err + } + configDir = filepath.Join(home, ".config", "etime") } err = fs.MkdirAll(configDir, 0o755) if err != nil { return Config{}, err } - path := configDir + "/settings.json" + path := filepath.Join(configDir, "settings.json") data, err := afero.ReadFile(fs, path) if err != nil { if !errors.Is(err, os.ErrNotExist) { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 5585382..9cc55d5 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "os" + "path/filepath" "testing" "time" @@ -18,9 +19,12 @@ import ( func TestLoad(t *testing.T) { mockIDToken := "eyJ-example-token" mockRefreshToken := "eyJ-example-refresh" + mockHomeDir, err := os.UserHomeDir() // FIXME: mock os! + require.NoError(t, err) tests := map[string]struct { mockConfigFile string + mockConfigPath string mockFlags Flags mockGenerateTokensResponse cognito.CognitoResponse mockGenerateTokensError error @@ -29,6 +33,7 @@ func TestLoad(t *testing.T) { mockRefreshTokensResponse cognito.CognitoResponse mockRefreshTokensError error expectedConfig Config + expectedConfigPath string expectedError error }{ "loads the saved and valid credentials into configurations": { @@ -40,6 +45,7 @@ func TestLoad(t *testing.T) { "ExpiresAt": "2222-02-22T22:22:22Z" } }`, + mockConfigPath: filepath.Join("tmp", "configs"), mockGetCustomerDevicesResponse: []api.Device{ { DeviceGid: 123456, @@ -53,6 +59,7 @@ func TestLoad(t *testing.T) { ExpiresAt: time.Date(2222, 2, 22, 22, 22, 22, 0, time.UTC), }, }, + expectedConfigPath: filepath.Join("tmp", "configs", "etime", "settings.json"), }, "writes configured authentication from provided credentials": { mockFlags: Flags{ @@ -76,6 +83,7 @@ func TestLoad(t *testing.T) { RefreshToken: mockRefreshToken, }, }, + expectedConfigPath: filepath.Join(mockHomeDir, ".config", "etime", "settings.json"), }, } for name, tt := range tests { @@ -92,10 +100,18 @@ func TestLoad(t *testing.T) { Return(tt.mockGetCustomerDevicesResponse, tt.mockGetCustomerDevicesError) req.On("SetToken", mock.Anything) req.On("SetDevice", mock.Anything) - dir, err := os.UserHomeDir() - require.NoError(t, err) + configFilePath := "" + if tt.mockConfigPath != "" { + os.Setenv("XDG_CONFIG_HOME", tt.mockConfigPath) + configFilePath = filepath.Join(tt.mockConfigPath, "etime", "settings.json") + } else { + os.Unsetenv("XDG_CONFIG_HOME") + dir, err := os.UserHomeDir() + require.NoError(t, err) + configFilePath = filepath.Join(dir, ".config", "etime", "settings.json") + } if tt.mockConfigFile != "" { - settings, err := fs.Create(dir + "/.config/etime/settings.json") + settings, err := fs.Create(configFilePath) require.NoError(t, err) _, err = settings.WriteString(tt.mockConfigFile) require.NoError(t, err) @@ -111,7 +127,7 @@ func TestLoad(t *testing.T) { assert.Equal(t, tt.expectedConfig.Tokens.IdToken, cfg.Tokens.IdToken) assert.Equal(t, tt.expectedConfig.Tokens.RefreshToken, cfg.Tokens.RefreshToken) assert.Greater(t, cfg.Tokens.ExpiresAt, time.Now()) - assert.Equal(t, dir+"/.config/etime/settings.json", cfg.path) + assert.Equal(t, tt.expectedConfigPath, cfg.path) req.AssertCalled(t, "SetDevice", tt.expectedConfig.Device) req.AssertCalled(t, "SetToken", tt.expectedConfig.Tokens.IdToken) actualConfigFile, err := afero.ReadFile(fs, cfg.path)