From 659bc67fb020142443a6206581440301d89c27c4 Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Mon, 6 Jan 2025 12:11:11 -0600 Subject: [PATCH] move to new Execute Signed-off-by: Alberto Ricart --- cmd/addaccount_test.go | 5 ++--- cmd/cmd_test.go | 9 --------- cmd/describeaccount_test.go | 2 +- cmd/describejwt_test.go | 2 +- cmd/expirations.go | 2 +- cmd/generateactivation_test.go | 2 +- cmd/generateserverconfig.go | 2 +- cmd/push_test.go | 2 +- cmd/upgradejwt_test.go | 6 +++--- cmd/util_test.go | 2 +- cmd/validate_test.go | 4 ++-- 11 files changed, 14 insertions(+), 24 deletions(-) diff --git a/cmd/addaccount_test.go b/cmd/addaccount_test.go index 90136872..eaf805e7 100644 --- a/cmd/addaccount_test.go +++ b/cmd/addaccount_test.go @@ -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) { @@ -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")) diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go index 5624cfea..93bb3842 100644 --- a/cmd/cmd_test.go +++ b/cmd/cmd_test.go @@ -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)) diff --git a/cmd/describeaccount_test.go b/cmd/describeaccount_test.go index 8a6b6e9e..4b598d3e 100644 --- a/cmd/describeaccount_test.go +++ b/cmd/describeaccount_test.go @@ -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%)") } diff --git a/cmd/describejwt_test.go b/cmd/describejwt_test.go index 05ed8303..176a0205 100644 --- a/cmd/describejwt_test.go +++ b/cmd/describejwt_test.go @@ -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) } diff --git a/cmd/expirations.go b/cmd/expirations.go index 59cd541a..b1284c99 100644 --- a/cmd/expirations.go +++ b/cmd/expirations.go @@ -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 } diff --git a/cmd/generateactivation_test.go b/cmd/generateactivation_test.go index 44cae52b..bab64496 100644 --- a/cmd/generateactivation_test.go +++ b/cmd/generateactivation_test.go @@ -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) diff --git a/cmd/generateserverconfig.go b/cmd/generateserverconfig.go index 5ce9ea34..2716fa5e 100644 --- a/cmd/generateserverconfig.go +++ b/cmd/generateserverconfig.go @@ -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) diff --git a/cmd/push_test.go b/cmd/push_test.go index 82d587e9..9c3f3b0d 100644 --- a/cmd/push_test.go +++ b/cmd/push_test.go @@ -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) diff --git a/cmd/upgradejwt_test.go b/cmd/upgradejwt_test.go index a77fc071..605fec00 100644 --- a/cmd/upgradejwt_test.go +++ b/cmd/upgradejwt_test.go @@ -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, "") @@ -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") diff --git a/cmd/util_test.go b/cmd/util_test.go index 868248a0..defc4fe1 100644 --- a/cmd/util_test.go +++ b/cmd/util_test.go @@ -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" diff --git a/cmd/validate_test.go b/cmd/validate_test.go index 5a21acc1..5d02ddf5 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -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\"") } @@ -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") }