github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/cmd/action_test.go (about)

     1  /*
     2   * Copyright 2018 The NATS Authors
     3   * Licensed under the Apache License, Version 2.0 (the "License");
     4   * you may not use this file except in compliance with the License.
     5   * You may obtain a copy of the License at
     6   *
     7   * http://www.apache.org/licenses/LICENSE-2.0
     8   *
     9   * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15  
    16  package cmd
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/nats-io/nsc/v2/cmd/store"
    22  	"github.com/spf13/cobra"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func Test_NewActRequiresAction(t *testing.T) {
    27  	ts := NewTestStore(t, "test")
    28  	defer ts.Done(t)
    29  
    30  	err := RunAction(nil, []string{"foo", "bar"}, "hello")
    31  	require.Error(t, err)
    32  	require.Equal(t, "action provided is not an Action", err.Error())
    33  }
    34  
    35  func TestActionContextSet(t *testing.T) {
    36  	ts := NewTestStore(t, "test")
    37  	defer ts.Done(t)
    38  
    39  	ar := newDefaultAction()
    40  	ar.setDefaults = func(ctx ActionCtx) error {
    41  		require.NotNil(t, ctx)
    42  		require.NotNil(t, ctx.StoreCtx())
    43  		require.NotNil(t, ctx.CurrentCmd())
    44  		require.Len(t, ctx.Args(), 2)
    45  		require.Equal(t, "hello", ctx.Args()[0])
    46  		require.Equal(t, "world", ctx.Args()[1])
    47  		return nil
    48  	}
    49  
    50  	cmd := &cobra.Command{
    51  		Use:   "add",
    52  		Short: "Add assets such as accounts, imports, users, clusters, servers",
    53  		RunE: func(cmd *cobra.Command, args []string) error {
    54  			if err := RunAction(cmd, args, &ar); err != nil {
    55  				return err
    56  			}
    57  			return nil
    58  		},
    59  	}
    60  
    61  	_, _, err := ExecuteCmd(cmd, "hello", "world")
    62  	require.NoError(t, err)
    63  }
    64  
    65  func TestActionInteractive(t *testing.T) {
    66  	ts := NewTestStore(t, "test")
    67  	defer ts.Done(t)
    68  
    69  	count := 0
    70  
    71  	ar := newDefaultAction()
    72  	ar.preInteractive = func(ctx ActionCtx) error {
    73  		count++
    74  		return nil
    75  	}
    76  	ar.postInteractive = func(ctx ActionCtx) error {
    77  		count++
    78  		return nil
    79  	}
    80  
    81  	cmd := &cobra.Command{
    82  		Use:   "add",
    83  		Short: "Add assets such as accounts, imports, users, clusters, servers",
    84  		RunE: func(cmd *cobra.Command, args []string) error {
    85  			if err := RunAction(cmd, args, &ar); err != nil {
    86  				return err
    87  			}
    88  			return nil
    89  		},
    90  	}
    91  	_, _, err := ExecuteInteractiveCmd(cmd, []interface{}{})
    92  	require.NoError(t, err)
    93  	require.Equal(t, 2, count)
    94  }
    95  
    96  func TestActionNothingToDoEmpty(t *testing.T) {
    97  	ts := NewTestStore(t, "test")
    98  	defer ts.Done(t)
    99  
   100  	var v string
   101  
   102  	nothingToDo := false
   103  
   104  	ar := newDefaultAction()
   105  	ar.setDefaults = func(ctx ActionCtx) error {
   106  		nothingToDo = ctx.NothingToDo("name")
   107  		return nil
   108  	}
   109  	cmd := &cobra.Command{
   110  		Use:   "add",
   111  		Short: "Add assets such as accounts, imports, users, clusters, servers",
   112  		RunE: func(cmd *cobra.Command, args []string) error {
   113  			if err := RunAction(cmd, args, &ar); err != nil {
   114  				return err
   115  			}
   116  			return nil
   117  		},
   118  	}
   119  
   120  	cmd.Flags().StringVarP(&v, "name", "n", "", "account name")
   121  	_, _, err := ExecuteCmd(cmd)
   122  	require.NoError(t, err)
   123  	require.True(t, nothingToDo)
   124  }
   125  
   126  func TestActionNothingToDoSet(t *testing.T) {
   127  	ts := NewTestStore(t, "test")
   128  	defer ts.Done(t)
   129  
   130  	var v string
   131  
   132  	nothingToDo := false
   133  
   134  	ar := newDefaultAction()
   135  	ar.setDefaults = func(ctx ActionCtx) error {
   136  		nothingToDo = ctx.NothingToDo("name")
   137  		return nil
   138  	}
   139  	cmd := &cobra.Command{
   140  		Use:   "add",
   141  		Short: "Add assets such as accounts, imports, users, clusters, servers",
   142  		RunE: func(cmd *cobra.Command, args []string) error {
   143  			if err := RunAction(cmd, args, &ar); err != nil {
   144  				return err
   145  			}
   146  			return nil
   147  		},
   148  	}
   149  
   150  	cmd.Flags().StringVarP(&v, "name", "n", "", "account name")
   151  	_, _, err := ExecuteCmd(cmd, "--name", "a")
   152  	require.NoError(t, err)
   153  	require.False(t, nothingToDo)
   154  }
   155  
   156  type actionResponse struct {
   157  	setDefaults     ActionFn
   158  	preInteractive  ActionFn
   159  	load            ActionFn
   160  	postInteractive ActionFn
   161  	validate        ActionFn
   162  	run             ActionRunFn
   163  }
   164  
   165  var nilResponse = func(ctx ActionCtx) error {
   166  	return nil
   167  }
   168  
   169  var nilRunResponse = func(ctx ActionCtx) (store.Status, error) {
   170  	return nil, nil
   171  }
   172  
   173  func (a *actionResponse) SetDefaults(ctx ActionCtx) error {
   174  	return a.setDefaults(ctx)
   175  }
   176  
   177  func (a *actionResponse) PreInteractive(ctx ActionCtx) error {
   178  	return a.preInteractive(ctx)
   179  }
   180  
   181  func (a *actionResponse) Load(ctx ActionCtx) error {
   182  	return a.load(ctx)
   183  }
   184  
   185  func (a *actionResponse) PostInteractive(ctx ActionCtx) error {
   186  	return a.postInteractive(ctx)
   187  }
   188  
   189  func (a *actionResponse) Validate(ctx ActionCtx) error {
   190  	return a.validate(ctx)
   191  }
   192  
   193  func (a *actionResponse) Run(ctx ActionCtx) (store.Status, error) {
   194  	return a.run(ctx)
   195  }
   196  
   197  func newDefaultAction() actionResponse {
   198  	var ar actionResponse
   199  	ar.setDefaults = nilResponse
   200  	ar.preInteractive = nilResponse
   201  	ar.load = nilResponse
   202  	ar.postInteractive = nilResponse
   203  	ar.validate = nilResponse
   204  	ar.run = nilRunResponse
   205  
   206  	return ar
   207  }
   208  
   209  func TestActionMessage(t *testing.T) {
   210  	ts := NewTestStore(t, "test")
   211  	defer ts.Done(t)
   212  
   213  	ar := newDefaultAction()
   214  	ar.run = func(ctx ActionCtx) (store.Status, error) {
   215  		return store.OKStatus("this is a test message"), nil
   216  	}
   217  
   218  	cmd := &cobra.Command{
   219  		Use:   "add",
   220  		Short: "Add assets such as accounts, imports, users, clusters, servers",
   221  		RunE: func(cmd *cobra.Command, args []string) error {
   222  			if err := RunAction(cmd, args, &ar); err != nil {
   223  				return err
   224  			}
   225  			return nil
   226  		},
   227  	}
   228  
   229  	out, _, err := ExecuteCmd(cmd)
   230  	require.NoError(t, err)
   231  	require.Contains(t, "This is a test message", out)
   232  }