Skip to content
Closed
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
67 changes: 67 additions & 0 deletions internal/cli/profile_precedence_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cli_test

import (
"encoding/json"
"os"
"path/filepath"
"testing"
)

func TestProfilesRespectProfileFlag(t *testing.T) {
cases := []struct {
name string
args []string
expected string
field string
}{
{"show_flag", []string{"profiles", "show", "--profile", "qa-chosen", "--json"}, "qa-chosen", "profile"},
{"list_flag", []string{"profiles", "list", "--profile", "qa-chosen", "--json"}, "qa-chosen", "active"},
{"show_positional_control", []string{"profiles", "show", "qa-chosen", "--json"}, "qa-chosen", "profile"},
{"show_active_control", []string{"profiles", "show", "--json"}, "qa-base", "profile"},
{"list_active_control", []string{"profiles", "list", "--json"}, "qa-base", "active"},
{"positional_overrides_flag_control", []string{"profiles", "show", "qa-base", "--profile", "qa-chosen", "--json"}, "qa-base", "profile"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
for name, project := range map[string]string{"qa-base": "proj_local_base", "qa-chosen": "proj_local_chosen"} {
raw, _ := json.Marshal(map[string]string{"project_id": project})
if err := os.WriteFile(filepath.Join(dir, name+".json"), raw, 0600); err != nil {
t.Fatal(err)
}
}
if err := os.WriteFile(filepath.Join(dir, ".active"), []byte("qa-base\n"), 0600); err != nil {
t.Fatal(err)
}
out, stderr, err := runCmdInConfigDir(t, dir, tc.args...)
if err != nil {
t.Fatalf("command failed: %v; %s", err, stderr)
}
var response struct {
Data map[string]any `json:"data"`
}
if err := json.Unmarshal([]byte(out), &response); err != nil {
t.Fatal(err)
}
if got := response.Data[tc.field]; got != tc.expected {
t.Errorf("%s = %v, want %s; output: %s", tc.field, got, tc.expected, out)
}
if tc.field == "profile" {
expectedProject := "proj_local_base"
if tc.expected == "qa-chosen" {
expectedProject = "proj_local_chosen"
}
if got := response.Data["project_id"]; got != expectedProject {
t.Errorf("project_id = %v, want %s", got, expectedProject)
}
}
active, err := os.ReadFile(filepath.Join(dir, ".active"))
if err != nil {
t.Fatal(err)
}
if string(active) != "qa-base\n" {
t.Errorf("transient flag changed persisted active profile: %q", active)
}
})
}
}
4 changes: 2 additions & 2 deletions internal/cli/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func newProfilesListCmd() *cobra.Command {
if err != nil {
return err
}
active := config.ProfileName("")
active := config.ProfileName(rt.Globals.Profile)
rows := make([][]string, 0, len(names))
for _, n := range names {
marker := " "
Expand Down Expand Up @@ -107,7 +107,7 @@ func newProfilesShowCmd() *cobra.Command {
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
rt := RuntimeFrom(cmd.Context())
name := ""
name := rt.Globals.Profile
if len(args) == 1 {
name = args[0]
}
Expand Down