Skip to content

Commit

Permalink
test(account): add add subcommand test (#6)
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Semagin <[email protected]>
  • Loading branch information
pixel365 authored Feb 16, 2025
1 parent 581ca67 commit 66e81ba
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
10 changes: 10 additions & 0 deletions cmd/account/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func addCmd() *cobra.Command {
Run(); err != nil {
return err
}
} else {
if err := internal.ValidateAccountLogin(login, conf); err != nil {
return err
}
}

now := time.Now().UTC()
Expand All @@ -54,6 +58,11 @@ func addCmd() *cobra.Command {

color.Green("Account created")

skipAuth, _ := c.Flags().GetBool("skip-auth")
if skipAuth {
return nil
}

confirm := false
if err := internal.Confirmation(&confirm,
"Do you want to log into this account right away?"); err != nil {
Expand All @@ -70,6 +79,7 @@ func addCmd() *cobra.Command {
}

cmd.Flags().StringP("login", "l", "", "Login")
cmd.Flags().BoolP("skip-auth", "s", false, "Skip auth")

return cmd
}
43 changes: 43 additions & 0 deletions cmd/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"context"
"errors"
"testing"

"github.com/pixel365/bx/internal"
"github.com/pixel365/bx/internal/config"
)

func TestAccountAdd(t *testing.T) {
ctx := context.Background()
cfg, _ := config.NewMockConfig()
var manager internal.ConfigManager = cfg

t.Run("", func(t *testing.T) {
rootCmd := NewRootCmd(ctx, manager)
rootCmd.SetArgs([]string{"account", "add", "--login", "test", "--skip-auth", "true"})
err := rootCmd.ExecuteContext(ctx)
if err != nil {
t.Error(err)
}

if len(cfg.GetAccounts()) != 1 {
t.Error("no accounts added")
}

if cfg.GetAccounts()[0].Login != "test" {
t.Error("invalid login")
}

rootCmd.SetArgs([]string{"account", "add", "--login", "test", "--skip-auth", "true"})
err = rootCmd.ExecuteContext(ctx)
if err == nil {
t.Error("invalid result")
}

if !errors.Is(err, internal.AccountAlreadyExists) {
t.Errorf("invalid result: %v", err)
}
})
}
7 changes: 1 addition & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ import (
"github.com/spf13/cobra"
)

func Execute(ctx context.Context, conf internal.ConfigManager) error {
cmd := rootCmd(ctx, conf)
return cmd.ExecuteContext(ctx)
}

func rootCmd(ctx context.Context, conf internal.ConfigManager) *cobra.Command {
func NewRootCmd(ctx context.Context, conf internal.ConfigManager) *cobra.Command {
cmd := &cobra.Command{
Use: "bx",
Short: "Command-line tool for developers of 1C-Bitrix platform modules.",
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func main() {
}
var configManager internal.ConfigManager = conf

if err := cmd.Execute(ctx, configManager); err != nil {
root := cmd.NewRootCmd(ctx, configManager)
if err := root.ExecuteContext(ctx); err != nil {
log.Fatal(err)
}
}

0 comments on commit 66e81ba

Please sign in to comment.