-
Notifications
You must be signed in to change notification settings - Fork 5
Show plan name and config path in header #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
924878d
f303fcf
7644006
c3777c9
73a961f
4985656
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestPlanDisplayName(t *testing.T) { | ||
| tests := []struct { | ||
| licenseType string | ||
| want string | ||
| }{ | ||
| {"hobby", "Hobby"}, | ||
| {"pro", "Pro"}, | ||
| {"team", "Teams"}, | ||
| {"enterprise", "Enterprise"}, | ||
| {"trial", "Trial"}, | ||
| {"freemium", "Community"}, | ||
| {"base", "Starter"}, | ||
| {"ultimate", "Ultimate"}, | ||
| {"student", "Student"}, | ||
| {"unknown_type", "unknown_type"}, | ||
| {"", ""}, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.licenseType, func(t *testing.T) { | ||
| resp := &LicenseResponse{LicenseType: tc.licenseType} | ||
| assert.Equal(t, tc.want, resp.PlanDisplayName()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestPlanDisplayNameNilResponse(t *testing.T) { | ||
| var resp *LicenseResponse | ||
| assert.Equal(t, "", resp.PlanDisplayName()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/viper" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestFriendlyConfigPathRelativeForProjectLocal(t *testing.T) { | ||
| // Cannot run in parallel: mutates process-wide cwd and viper state. | ||
|
|
||
| tmpDir := t.TempDir() | ||
| dir, err := filepath.EvalSymlinks(tmpDir) | ||
| require.NoError(t, err) | ||
| configDir := filepath.Join(dir, ".lstk") | ||
| require.NoError(t, os.MkdirAll(configDir, 0755)) | ||
|
|
||
| configFile := filepath.Join(configDir, "config.toml") | ||
| require.NoError(t, os.WriteFile(configFile, []byte("[aws]\n"), 0644)) | ||
|
|
||
| origDir, err := os.Getwd() | ||
| require.NoError(t, err) | ||
|
|
||
| require.NoError(t, os.Chdir(dir)) | ||
| t.Cleanup(func() { _ = os.Chdir(origDir) }) | ||
|
|
||
| viper.Reset() | ||
| t.Cleanup(viper.Reset) | ||
| viper.SetConfigFile(configFile) | ||
| require.NoError(t, viper.ReadInConfig()) | ||
|
|
||
| friendly, err := FriendlyConfigPath() | ||
| require.NoError(t, err) | ||
| require.Equal(t, filepath.Join(".lstk", "config.toml"), friendly) | ||
| } | ||
|
|
||
| func TestFriendlyConfigPathTildeForHomeDir(t *testing.T) { | ||
| // Cannot run in parallel: mutates process-wide viper state and HOME env. | ||
|
|
||
| fakeHome := t.TempDir() | ||
| resolvedHome, err := filepath.EvalSymlinks(fakeHome) | ||
| require.NoError(t, err) | ||
|
|
||
| configDir := filepath.Join(resolvedHome, ".config", "lstk") | ||
| require.NoError(t, os.MkdirAll(configDir, 0755)) | ||
|
|
||
| configFile := filepath.Join(configDir, "config.toml") | ||
| require.NoError(t, os.WriteFile(configFile, []byte("[aws]\n"), 0644)) | ||
|
|
||
| t.Setenv("HOME", resolvedHome) | ||
|
|
||
| viper.Reset() | ||
| t.Cleanup(viper.Reset) | ||
| viper.SetConfigFile(configFile) | ||
| require.NoError(t, viper.ReadInConfig()) | ||
|
|
||
| friendly, err := FriendlyConfigPath() | ||
| require.NoError(t, err) | ||
| require.Equal(t, filepath.Join("~", ".config", "lstk", "config.toml"), friendly) | ||
|
Comment on lines
+40
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Global Both tests call Additionally, this test relies on Consider creating a temporary config file similar to the first test to ensure consistent test coverage. 🐛 Proposed fix: remove t.Parallel() and create temp config func TestFriendlyConfigPathTildeForHomeDir(t *testing.T) {
- t.Parallel()
-
home, err := os.UserHomeDir()
require.NoError(t, err)
- configDir := filepath.Join(home, ".config", "lstk")
- if _, err := os.Stat(configDir); os.IsNotExist(err) {
- t.Skip("~/.config/lstk does not exist")
- }
-
- configFile := filepath.Join(configDir, "config.toml")
- if _, err := os.Stat(configFile); os.IsNotExist(err) {
- t.Skip("~/.config/lstk/config.toml does not exist")
- }
+ // Create temp config in a subdirectory of home to test tilde prefix
+ configDir := filepath.Join(home, ".lstk-test-"+t.Name())
+ require.NoError(t, os.MkdirAll(configDir, 0755))
+ t.Cleanup(func() { _ = os.RemoveAll(configDir) })
+
+ configFile := filepath.Join(configDir, "config.toml")
+ require.NoError(t, os.WriteFile(configFile, []byte("[aws]\n"), 0644))
viper.Reset()🤖 Prompt for AI Agents |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| const planCacheFile = "plan_label" | ||
|
|
||
| func CachedPlanLabel() string { | ||
| dir, err := ConfigDir() | ||
| if err != nil { | ||
| return "" | ||
| } | ||
| data, err := os.ReadFile(filepath.Join(dir, planCacheFile)) | ||
| if err != nil { | ||
| return "" | ||
| } | ||
| return strings.TrimSpace(string(data)) | ||
| } | ||
|
|
||
| func CachePlanLabel(label string) { | ||
| dir, err := ConfigDir() | ||
| if err != nil { | ||
| return | ||
| } | ||
| _ = os.WriteFile(filepath.Join(dir, planCacheFile), []byte(label+"\n"), 0600) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Race condition:
t.Parallel()withos.Chdir()is unsafe.os.Chdirmodifies the process-global working directory. When this test runs in parallel with other tests (includingTestFriendlyConfigPathTildeForHomeDirwhich also callst.Parallel()), the working directory changes will interfere with each other, causing flaky test failures.Remove
t.Parallel()from this test, or refactorFriendlyConfigPath()to accept an explicit working directory parameter for testing.🐛 Proposed fix: remove t.Parallel()
func TestFriendlyConfigPathRelativeForProjectLocal(t *testing.T) { - t.Parallel() - tmpDir := t.TempDir()📝 Committable suggestion
🤖 Prompt for AI Agents