Skip to content

Commit

Permalink
fix: gather configured settings using a generic filepath separator (#130
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zimeg authored Sep 6, 2024
1 parent e4a8599 commit c07803b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 11 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"os"
"path/filepath"
"time"

"github.com/spf13/afero"
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 20 additions & 4 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"

Expand All @@ -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
Expand All @@ -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": {
Expand All @@ -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,
Expand All @@ -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{
Expand All @@ -76,6 +83,7 @@ func TestLoad(t *testing.T) {
RefreshToken: mockRefreshToken,
},
},
expectedConfigPath: filepath.Join(mockHomeDir, ".config", "etime", "settings.json"),
},
}
for name, tt := range tests {
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit c07803b

Please sign in to comment.