-
Notifications
You must be signed in to change notification settings - Fork 369
feat: add --theme flag to preselect TUI theme #2933
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
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,68 @@ | ||
| package root | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/docker/docker-agent/pkg/paths" | ||
| "github.com/docker/docker-agent/pkg/tui/styles" | ||
| ) | ||
|
|
||
| func TestValidateTheme(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("accepts built-in theme", func(t *testing.T) { | ||
| t.Parallel() | ||
| require.NoError(t, validateTheme("nord")) | ||
| }) | ||
|
|
||
| t.Run("accepts default theme", func(t *testing.T) { | ||
| t.Parallel() | ||
| require.NoError(t, validateTheme(styles.DefaultThemeRef)) | ||
| }) | ||
|
|
||
| t.Run("rejects unknown theme with helpful message", func(t *testing.T) { | ||
| t.Parallel() | ||
| err := validateTheme("does-not-exist") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "does-not-exist") | ||
| assert.Contains(t, err.Error(), "available themes") | ||
| }) | ||
|
|
||
| t.Run("rejects path traversal", func(t *testing.T) { | ||
| t.Parallel() | ||
| require.Error(t, validateTheme("../../etc/passwd")) | ||
| }) | ||
| } | ||
|
|
||
| func TestApplyThemePrecedence(t *testing.T) { | ||
|
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. [MEDIUM]
The
If this ordering were accidentally swapped in a future refactor (user config checked after flag instead of before), the existing tests would not catch the regression because they only test the flag override against an empty user config. Suggested addition to t.Run("flag override wins over user config theme", func(t *testing.T) {
// Write a user config that pins a different theme
userConfigPath := filepath.Join(dir, "config.json")
_ = os.WriteFile(userConfigPath, []byte(`{"theme":"dracula"}`), 0o600)
applyTheme("nord") // --theme flag should override the config's "dracula"
assert.Equal(t, "nord", styles.CurrentTheme().Ref)
}) |
||
| // Not parallel: mutates the process-global applied theme via ApplyTheme. | ||
| // Isolate config/data dirs so a developer's real user config (which may | ||
| // pin a theme) cannot influence the precedence assertions. | ||
| dir := t.TempDir() | ||
| paths.SetConfigDir(dir) | ||
| paths.SetDataDir(dir) | ||
| t.Cleanup(func() { | ||
| paths.SetConfigDir("") | ||
| paths.SetDataDir("") | ||
| }) | ||
|
|
||
| t.Run("override takes precedence and is applied", func(t *testing.T) { | ||
| applyTheme("nord") | ||
| assert.Equal(t, "nord", styles.CurrentTheme().Ref) | ||
| }) | ||
|
|
||
| t.Run("invalid override falls back to default theme", func(t *testing.T) { | ||
| // applyTheme tolerates an invalid ref (validateTheme guards the CLI | ||
| // entry point); it must never panic and should apply the default. | ||
| applyTheme("does-not-exist") | ||
| assert.Equal(t, styles.DefaultThemeRef, styles.CurrentTheme().Ref) | ||
| }) | ||
|
|
||
| t.Run("empty override applies default when no user config theme", func(t *testing.T) { | ||
| applyTheme("") | ||
| assert.Equal(t, styles.DefaultThemeRef, styles.CurrentTheme().Ref) | ||
| }) | ||
| } | ||
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.
[MEDIUM]
--themevalidation fires in--execmode, contradicting "ignored outside TUI" docsThe flag's help text says the
--themeflag is "ignored outside the interactive TUI", butvalidateThemeis called unconditionally fromrunRunCommand— the shared entry point for both interactive and--exec(non-TUI) modes.This means:
…will error out with
unknown theme "does-not-exist"; available themes: …instead of silently ignoring the flag as the documentation promises.Suggested fix: Gate the validation on whether the command is actually launching the interactive TUI (e.g. skip validation when
--execis set):Or update the flag's help text to say the flag is validated even in exec mode (even if the theme is not applied).