Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions action/config/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,22 @@ func genBytes(c *Config) ([]byte, error) {
URL: c.GitHub.URL,
},
},
Org: c.Org,
Repo: c.Repo,
Output: c.Output,
Color: &c.Color.Enabled,
ColorFormat: c.Color.Format,
ColorTheme: c.Color.Theme,
NoGit: c.NoGit,
Org: c.Org,
Repo: c.Repo,
Output: c.Output,
Color: &c.Color.Enabled,
NoGit: c.NoGit,
}

// only save if theme was user specified; this prevents saving default "monokai"
// during login, which would bypass auto-detection for light terminal backgrounds.
if c.Color.UserSpecified {
config.ColorTheme = c.Color.Theme
}

// only save if not default
if c.Color.Format != "" && c.Color.Format != "terminal256" {
config.ColorFormat = c.Color.Format
}

out, err := yaml.Marshal(config)
Expand Down
317 changes: 317 additions & 0 deletions action/config/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import (
"testing"

"github.com/spf13/afero"
yaml "go.yaml.in/yaml/v3"

"github.com/go-vela/cli/internal/output"
)

// Test constants for color configuration.
const (
testThemeMonokai = "monokai"
testThemeMonokaiLight = "monokailight"
testThemeDracula = "dracula"
testThemeSolarizedDark = "solarized-dark"
testFormatTerminal256 = "terminal256"
testFormatTerminal16 = "terminal16"
testFormatTerminal16m = "terminal16m"
)

func TestConfig_Config_Generate(t *testing.T) {
Expand Down Expand Up @@ -44,3 +58,306 @@ func TestConfig_Config_Generate(t *testing.T) {
}
}
}

func TestConfig_Config_Generate_ColorTheme(t *testing.T) {
// setup tests
tests := []struct {
name string
config *Config
expectTheme bool
expectThemeValue string
expectFormat bool
expectFormatValue string
}{
{
name: "default theme not user specified - should not save theme",
config: &Config{
Action: "generate",
File: ".vela.yml",
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Format: testFormatTerminal256,
Theme: testThemeMonokai,
ThemeLight: testThemeMonokaiLight,
UserSpecified: false,
},
},
expectTheme: false,
expectFormat: false,
},
{
name: "custom theme user specified - should save theme",
config: &Config{
Action: "generate",
File: ".vela.yml",
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Format: testFormatTerminal256,
Theme: testThemeDracula,
ThemeLight: testThemeMonokaiLight,
UserSpecified: true,
},
},
expectTheme: true,
expectThemeValue: testThemeDracula,
expectFormat: false,
},
{
name: "default theme user specified - should save theme",
config: &Config{
Action: "generate",
File: ".vela.yml",
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Format: testFormatTerminal256,
Theme: testThemeMonokai,
ThemeLight: testThemeMonokaiLight,
UserSpecified: true,
},
},
expectTheme: true,
expectThemeValue: testThemeMonokai,
expectFormat: false,
},
{
name: "custom format non-default - should save format",
config: &Config{
Action: "generate",
File: ".vela.yml",
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Format: testFormatTerminal16,
Theme: testThemeMonokai,
ThemeLight: testThemeMonokaiLight,
UserSpecified: false,
},
},
expectTheme: false,
expectFormat: true,
expectFormatValue: testFormatTerminal16,
},
{
name: "custom format and theme both specified - should save both",
config: &Config{
Action: "generate",
File: ".vela.yml",
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Format: testFormatTerminal16m,
Theme: testThemeSolarizedDark,
ThemeLight: testThemeMonokaiLight,
UserSpecified: true,
},
},
expectTheme: true,
expectThemeValue: testThemeSolarizedDark,
expectFormat: true,
expectFormatValue: testFormatTerminal16m,
},
{
name: "empty format - should not save format",
config: &Config{
Action: "generate",
File: ".vela.yml",
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Format: "",
Theme: testThemeMonokai,
ThemeLight: testThemeMonokaiLight,
UserSpecified: false,
},
},
expectTheme: false,
expectFormat: false,
},
{
name: "default format terminal256 - should not save format",
config: &Config{
Action: "generate",
File: ".vela.yml",
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Format: testFormatTerminal256,
Theme: testThemeMonokai,
ThemeLight: testThemeMonokaiLight,
UserSpecified: false,
},
},
expectTheme: false,
expectFormat: false,
},
}

// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// setup filesystem
appFS = afero.NewMemMapFs()

err := test.config.Generate()
if err != nil {
t.Errorf("Generate returned err: %v", err)
return
}

// read the generated file
a := &afero.Afero{Fs: appFS}

content, err := a.ReadFile(test.config.File)
if err != nil {
t.Errorf("Failed to read generated file: %v", err)
return
}

// parse the YAML
var configFile ConfigFile

err = yaml.Unmarshal(content, &configFile)
if err != nil {
t.Errorf("Failed to unmarshal YAML: %v", err)
return
}

// check theme
if test.expectTheme {
if configFile.ColorTheme == "" {
t.Errorf("Expected color_theme to be saved, but it was not")
}

if configFile.ColorTheme != test.expectThemeValue {
t.Errorf("ColorTheme = %v, want %v", configFile.ColorTheme, test.expectThemeValue)
}
} else {
if configFile.ColorTheme != "" {
t.Errorf("Expected color_theme to not be saved, but got: %v", configFile.ColorTheme)
}
}

// check format
if test.expectFormat {
if configFile.ColorFormat == "" {
t.Errorf("Expected color_format to be saved, but it was not")
}

if configFile.ColorFormat != test.expectFormatValue {
t.Errorf("ColorFormat = %v, want %v", configFile.ColorFormat, test.expectFormatValue)
}
} else {
if configFile.ColorFormat != "" {
t.Errorf("Expected color_format to not be saved, but got: %v", configFile.ColorFormat)
}
}
})
}
}

func TestConfig_genBytes_ColorTheme(t *testing.T) {
// setup tests
tests := []struct {
name string
config *Config
wantTheme string
wantFormat string
wantNoTheme bool
wantNoFormat bool
}{
{
name: "user specified theme",
config: &Config{
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Theme: testThemeDracula,
Format: testFormatTerminal256,
UserSpecified: true,
},
},
wantTheme: testThemeDracula,
wantNoFormat: true, // default format shouldn't be saved
},
{
name: "default theme not user specified",
config: &Config{
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Theme: testThemeMonokai,
Format: testFormatTerminal256,
UserSpecified: false,
},
},
wantNoTheme: true,
wantNoFormat: true,
},
{
name: "custom format",
config: &Config{
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Theme: testThemeMonokai,
Format: testFormatTerminal16,
UserSpecified: false,
},
},
wantFormat: testFormatTerminal16,
wantNoTheme: true,
},
{
name: "both theme and format custom",
config: &Config{
GitHub: &GitHub{},
Color: output.ColorOptions{
Enabled: true,
Theme: testThemeSolarizedDark,
Format: testFormatTerminal16m,
UserSpecified: true,
},
},
wantTheme: testThemeSolarizedDark,
wantFormat: testFormatTerminal16m,
},
}

// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
bytes, err := genBytes(test.config)
if err != nil {
t.Errorf("genBytes returned err: %v", err)
return
}

var configFile ConfigFile

err = yaml.Unmarshal(bytes, &configFile)
if err != nil {
t.Errorf("Failed to unmarshal YAML: %v", err)
return
}

if test.wantNoTheme {
if configFile.ColorTheme != "" {
t.Errorf("Expected no theme, but got: %v", configFile.ColorTheme)
}
} else if configFile.ColorTheme != test.wantTheme {
t.Errorf("ColorTheme = %v, want %v", configFile.ColorTheme, test.wantTheme)
}

if test.wantNoFormat {
if configFile.ColorFormat != "" {
t.Errorf("Expected no format, but got: %v", configFile.ColorFormat)
}
} else if configFile.ColorFormat != test.wantFormat {
t.Errorf("ColorFormat = %v, want %v", configFile.ColorFormat, test.wantFormat)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/vela-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func main() {
&cli.StringFlag{
Sources: cli.EnvVars("VELA_COLOR_THEME"),
Name: internal.FlagColorTheme,
Usage: "configures the output color theme (default: monokai)",
Usage: "configures the output color theme (default: monokai or monokailight) - use 'vela view themes' to see available themes",
},
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/vela-cli/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/go-vela/cli/command/service"
"github.com/go-vela/cli/command/settings"
"github.com/go-vela/cli/command/step"
"github.com/go-vela/cli/command/themes"
"github.com/go-vela/cli/command/user"
"github.com/go-vela/cli/command/worker"
)
Expand Down Expand Up @@ -96,6 +97,11 @@ var viewCmds = &cli.Command{
// https://pkg.go.dev/github.com/go-vela/cli/command/step?tab=doc#CommandView
step.CommandView,

// add the sub command for viewing available themes
//
// https://pkg.go.dev/github.com/go-vela/cli/command/themes?tab=doc#CommandView
themes.CommandView,

// add the sub command for viewing a user
//
// https://pkg.go.dev/github.com/go-vela/cli/command/user?tab=doc#CommandView
Expand Down
8 changes: 8 additions & 0 deletions command/themes/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: Apache-2.0

// Package themes provides the defined CLI theme commands for Vela.
//
// Usage:
//
// import "github.com/go-vela/cli/command/themes"
package themes
Loading