Skip to content

Commit

Permalink
move to new Execute
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart committed Jan 6, 2025
1 parent 46f796f commit 659bc67
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 24 deletions.
5 changes: 2 additions & 3 deletions cmd/addaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import (

"github.com/nats-io/jwt/v2"
"github.com/nats-io/nkeys"
"github.com/stretchr/testify/require"

"github.com/nats-io/nsc/v2/cmd/store"
"github.com/stretchr/testify/require"
)

func Test_AddAccount(t *testing.T) {
Expand Down Expand Up @@ -177,7 +176,7 @@ func Test_AddAccountInteractiveSigningKey(t *testing.T) {

// sign with the custom key
inputs := []interface{}{"A", true, "0", "0", 1, string(s1)}
_, err = ExecuteInteractiveCmdX(HoistRootFlags(CreateAddAccountCmd()), inputs)
_, err = ExecuteInteractiveCmd(HoistRootFlags(CreateAddAccountCmd()), inputs, []string{}...)
require.NoError(t, err)

d, err := ts.Store.Read(store.JwtName("O"))
Expand Down
9 changes: 0 additions & 9 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,6 @@ func ExecuteCmd(root *cobra.Command, args ...string) (CmdOutput, error) {
return CmdOutput{Out: stdoutBuf.String(), Err: stderrBuf.String()}, err
}

func ExecuteInteractiveCmdX(root *cobra.Command, inputs []interface{}, args ...string) (out CmdOutput, err error) {
InteractiveFlag = true
cli.SetPromptLib(cli.NewTestPrompts(inputs))
out, err = ExecuteCmd(root, args...)
cli.ResetPromptLib()
InteractiveFlag = false
return out, err
}

func ExecuteInteractiveCmd(root *cobra.Command, inputs []interface{}, args ...string) (out CmdOutput, err error) {
InteractiveFlag = true
cli.SetPromptLib(cli.NewTestPrompts(inputs))
Expand Down
2 changes: 1 addition & 1 deletion cmd/describeaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestDescribeAccount_Latency(t *testing.T) {
_, err = ts.Store.StoreClaim([]byte(token))
require.NoError(t, err)

out, err := ExecuteInteractiveCmdX(createDescribeAccountCmd(), []interface{}{0})
out, err := ExecuteInteractiveCmd(createDescribeAccountCmd(), []interface{}{0}, []string{}...)
require.NoError(t, err)
require.Contains(t, out.Out, "lat (10%)")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/describejwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestDescribe_Interactive(t *testing.T) {

fp := filepath.Join(ts.GetStoresRoot(), "O", store.Accounts, "A", "A.jwt")

out, err := ExecuteInteractiveCmdX(createDescribeJwtCmd(), []interface{}{fp})
out, err := ExecuteInteractiveCmd(createDescribeJwtCmd(), []interface{}{fp}, []string{}...)
require.NoError(t, err)
require.Contains(t, out.Out, pub)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/expirations.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (p *ExpirationsParams) renderJSON(ctx ActionCtx) error {
if err != nil {
return err
}
_, err = fmt.Fprintf(ctx.CurrentCmd().OutOrStdout(), string(data))
_, err = fmt.Fprintln(ctx.CurrentCmd().OutOrStdout(), string(data))
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/generateactivation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func Test_InteractiveGenerateActivationPush(t *testing.T) {

tf := filepath.Join(ts.Dir, "token.jwt")
inputs := []interface{}{0, "q", apk, "0", "0", true}
_, err = ExecuteInteractiveCmdX(createGenerateActivationCmd(), inputs, "--output-file", tf)
_, err = ExecuteInteractiveCmd(createGenerateActivationCmd(), inputs, []string{"--output-file", tf}...)
require.NoError(t, err)

d, err := Read(tf)
Expand Down
2 changes: 1 addition & 1 deletion cmd/generateserverconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (p *GenerateServerConfigParams) Run(ctx ActionCtx) (store.Status, error) {
return nil, err
}
if IsStdOut(p.outputFile) {
_, err := fmt.Fprintf(ctx.CurrentCmd().OutOrStdout(), string(d))
_, err := fmt.Fprintln(ctx.CurrentCmd().OutOrStdout(), string(d))
return nil, err
}
err = WriteFile(p.outputFile, d)
Expand Down
2 changes: 1 addition & 1 deletion cmd/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func Test_SyncNatsResolverDiff(t *testing.T) {
require.Contains(t, out.Out, "named SYS exists")

re := regexp.MustCompile("[A-Z0-9]* named AC1 exists")
line := re.FindString(out.Err)
line := re.FindString(out.Out)
accId := strings.TrimSuffix(line, " named AC1 exists")

out, err = ExecuteCmd(createPushCmd(), "--account-removal", accId)
Expand Down
6 changes: 3 additions & 3 deletions cmd/upgradejwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ func TestUpgradeNoKeyNonManaged(t *testing.T) {
executeFailingCmd(t, "edit", "operator", "--tag", "foo") // try writing operator
executePassingCmd(t, "env") // only few exceptions

stdErr, err := ExecuteInteractiveCmdX(rootCmd, []interface{}{}, "upgrade-jwt") // only works in interactive mode
stdErr, err := ExecuteInteractiveCmd(rootCmd, []interface{}{}, []string{"upgrade-jwt"}...) // only works in interactive mode
require.NoError(t, err)
require.Contains(t, stdErr.Out, "Identity Key for Operator")
require.Contains(t, stdErr.Out, "you need to restore it for this command to work")
checkJwtVersion(t, ts, "O", 1, token)
storeOperatorKey(t, ts, kp)
_, err = ExecuteInteractiveCmdX(rootCmd, []interface{}{false, true}, "upgrade-jwt")
_, err = ExecuteInteractiveCmd(rootCmd, []interface{}{false, true}, []string{"upgrade-jwt"}...)
require.NoError(t, err)

checkJwtVersion(t, ts, "O", 2, "")
Expand All @@ -173,7 +173,7 @@ func TestUpgradeManaged(t *testing.T) {
executeFailingCmd(t, "list", "keys") // could be any command
executePassingCmd(t, "env") // only few exceptions

out, err := ExecuteInteractiveCmdX(rootCmd, []interface{}{false}, "upgrade-jwt") // only works in interactive mode
out, err := ExecuteInteractiveCmd(rootCmd, []interface{}{false}, []string{"upgrade-jwt"}...) // only works in interactive mode
require.NoError(t, err)
require.Contains(t, out.Out, "Your store is in managed mode")
require.Contains(t, out.Out, "nsc add operator --force --url")
Expand Down
2 changes: 1 addition & 1 deletion cmd/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"github.com/nats-io/jwt/v2"
jwt1 "github.com/nats-io/jwt/v2/v1compat"
"github.com/nats-io/nats-server/v2/server"
nats "github.com/nats-io/nats.go"
"github.com/nats-io/nats.go"
"github.com/nats-io/nkeys"
"github.com/stretchr/testify/require"

Expand Down
4 changes: 2 additions & 2 deletions cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func Test_ValidateInteractive(t *testing.T) {
ts.AddAccount(t, "A")
ts.AddAccount(t, "B")

out, err := ExecuteInteractiveCmdX(HoistRootFlags(createValidateCommand()), []interface{}{1}, "--account", "B")
out, err := ExecuteInteractiveCmd(HoistRootFlags(createValidateCommand()), []interface{}{1}, []string{"--account", "B"}...)
require.NoError(t, err)
require.Contains(t, out.Out, "Account \"B\"")
}
Expand All @@ -222,7 +222,7 @@ func Test_ValidateJsSys(t *testing.T) {
require.NoError(t, err)
require.NoError(t, ts.Store.StoreRaw([]byte(token)))

out, err := ExecuteInteractiveCmdX(HoistRootFlags(createValidateCommand()), []interface{}{1})
out, err := ExecuteInteractiveCmd(HoistRootFlags(createValidateCommand()), []interface{}{1}, []string{}...)
require.Error(t, err)
require.Contains(t, out.Out, "JetStream should not be enabled for system account")
}

0 comments on commit 659bc67

Please sign in to comment.