Skip to content
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

test(account): add add subcommand test #6

Merged
merged 1 commit into from
Feb 16, 2025
Merged
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
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)
}
}