Skip to content

Commit

Permalink
test: make state config mockable
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Jan 10, 2024
1 parent a5e871d commit fe0833b
Show file tree
Hide file tree
Showing 14 changed files with 393 additions and 165 deletions.
5 changes: 3 additions & 2 deletions cmd/hcloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hetznercloud/cli/internal/cli"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/state/config"
)

func init() {
Expand All @@ -17,10 +18,10 @@ func init() {
func main() {
configPath := os.Getenv("HCLOUD_CONFIG")
if configPath == "" {
configPath = state.DefaultConfigPath()
configPath = config.DefaultConfigPath()
}

cfg, err := state.ReadConfig(configPath)
cfg, err := config.ReadConfig(configPath)
if err != nil {
log.Fatalf("unable to read config file %q: %s\n", configPath, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/context/active.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func runActive(s state.State, cmd *cobra.Command, _ []string) error {
if os.Getenv("HCLOUD_TOKEN") != "" {
_, _ = fmt.Fprintln(os.Stderr, "Warning: HCLOUD_TOKEN is set. The active context will have no effect.")
}
if cfg := s.Config(); cfg.ActiveContext != nil {
cmd.Println(cfg.ActiveContext.Name)
if ctx := s.Config().ActiveContext(); ctx != nil {
cmd.Println(ctx.Name)
}
return nil
}
7 changes: 4 additions & 3 deletions internal/cmd/context/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"golang.org/x/term"

"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/cli/internal/state/config"
)

func newCreateCommand(s state.State) *cobra.Command {
Expand Down Expand Up @@ -40,7 +41,7 @@ func runCreate(s state.State, cmd *cobra.Command, args []string) error {
return errors.New("name already used")
}

context := &state.ConfigContext{Name: name}
context := &config.Context{Name: name}

var token string

Expand Down Expand Up @@ -82,8 +83,8 @@ func runCreate(s state.State, cmd *cobra.Command, args []string) error {

context.Token = token

cfg.Contexts = append(cfg.Contexts, context)
cfg.ActiveContext = context
cfg.SetContexts(append(cfg.Contexts(), context))
cfg.SetActiveContext(context)

if err := cfg.Write(); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/context/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func runDelete(s state.State, _ *cobra.Command, args []string) error {
if context == nil {
return fmt.Errorf("context not found: %v", name)
}
if cfg.ActiveContext == context {
if cfg.ActiveContext() == context {
_, _ = fmt.Fprintln(os.Stderr, "Warning: You are deleting the currently active context. Please select a new active context.")
cfg.ActiveContext = nil
cfg.SetActiveContext(nil)
}
cfg.RemoveContext(context)
return cfg.Write()
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/context/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func runList(s state.State, cmd *cobra.Command, _ []string) error {
tw.WriteHeader(cols)
}
cfg := s.Config()
for _, context := range cfg.Contexts {
for _, context := range cfg.Contexts() {
presentation := ContextPresentation{
Name: context.Name,
Token: context.Token,
Active: " ",
}
if ctx := cfg.ActiveContext; ctx != nil && ctx.Name == context.Name {
if ctx := cfg.ActiveContext(); ctx != nil && ctx.Name == context.Name {
presentation.Active = "*"
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/context/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func runUse(s state.State, _ *cobra.Command, args []string) error {
if context == nil {
return fmt.Errorf("context not found: %v", name)
}
cfg.ActiveContext = context
cfg.SetActiveContext(context)
return cfg.Write()
}
1 change: 1 addition & 0 deletions internal/hcapi2/mock/mock_gen.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hcapi2_mock

//go:generate mockgen -package hcapi2_mock -destination zz_config_mock.go github.com/hetznercloud/cli/internal/state/config Config
//go:generate mockgen -package hcapi2_mock -destination zz_action_client_mock.go github.com/hetznercloud/cli/internal/hcapi2 ActionClient
//go:generate mockgen -package hcapi2_mock -destination zz_certificate_client_mock.go github.com/hetznercloud/cli/internal/hcapi2 CertificateClient
//go:generate mockgen -package hcapi2_mock -destination zz_datacenter_client_mock.go github.com/hetznercloud/cli/internal/hcapi2 DatacenterClient
Expand Down
182 changes: 182 additions & 0 deletions internal/hcapi2/mock/zz_config_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fe0833b

Please sign in to comment.