github.com/tomwright/dasel@v1.27.3/internal/command/root_test.go (about)

     1  package command_test
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/tomwright/dasel/internal/command"
     6  	"os"
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestChangeDefaultCommand(t *testing.T) {
    13  	cachedArgs := os.Args
    14  	defer func() {
    15  		os.Args = cachedArgs
    16  	}()
    17  
    18  	testArgs := func(in []string, exp []string, blacklistedArgs ...string) func(t *testing.T) {
    19  		return func(t *testing.T) {
    20  			os.Args = in
    21  
    22  			cmd := command.NewRootCMD()
    23  			command.ChangeDefaultCommand(cmd, "select", blacklistedArgs...)
    24  
    25  			got := os.Args
    26  			if !reflect.DeepEqual(exp, got) {
    27  				t.Errorf("expected args %v, got %v", exp, got)
    28  			}
    29  		}
    30  	}
    31  
    32  	t.Run("ChangeToSelect", testArgs(
    33  		[]string{"dasel", "-p", "json", ".name"},
    34  		[]string{"dasel", "select", "-p", "json", ".name"},
    35  	))
    36  
    37  	t.Run("AlreadySelect", testArgs(
    38  		[]string{"dasel", "select", "-p", "json", ".name"},
    39  		[]string{"dasel", "select", "-p", "json", ".name"},
    40  	))
    41  
    42  	t.Run("AlreadyPut", testArgs(
    43  		[]string{"dasel", "put", "-p", "json", "-t", "string", "name=Tom"},
    44  		[]string{"dasel", "put", "-p", "json", "-t", "string", "name=Tom"},
    45  	))
    46  
    47  	t.Run("IgnoreBlacklisted", testArgs(
    48  		[]string{"dasel", "-v"},
    49  		[]string{"dasel", "-v"},
    50  		"-v",
    51  	))
    52  
    53  	t.Run("IgnoreBlacklisted", testArgs(
    54  		[]string{"dasel", "select", "-v"},
    55  		[]string{"dasel", "select", "-v"},
    56  		"-v",
    57  	))
    58  }
    59  
    60  func expectErr(args []string, expErr string) func(t *testing.T) {
    61  	return func(t *testing.T) {
    62  		cmd := command.NewRootCMD()
    63  		outputBuffer := bytes.NewBuffer([]byte{})
    64  
    65  		cmd.SetOut(outputBuffer)
    66  		cmd.SetArgs(args)
    67  
    68  		err := cmd.Execute()
    69  
    70  		if err == nil || !strings.Contains(err.Error(), expErr) {
    71  			t.Errorf("unexpected error: %v", err)
    72  			return
    73  		}
    74  	}
    75  }
    76  
    77  func expectErrFromInput(in string, args []string, expErr string) func(t *testing.T) {
    78  	return func(t *testing.T) {
    79  		cmd := command.NewRootCMD()
    80  		outputBuffer := bytes.NewBuffer([]byte{})
    81  
    82  		cmd.SetIn(bytes.NewReader([]byte(in)))
    83  		cmd.SetOut(outputBuffer)
    84  		cmd.SetArgs(args)
    85  
    86  		err := cmd.Execute()
    87  
    88  		if err == nil || !strings.Contains(err.Error(), expErr) {
    89  			t.Errorf("unexpected error: %v: %s", err, outputBuffer.String())
    90  			return
    91  		}
    92  	}
    93  }
    94  
    95  func expectOutput(in string, args []string, exp string) func(t *testing.T) {
    96  	return func(t *testing.T) {
    97  		cmd := command.NewRootCMD()
    98  		outputBuffer := bytes.NewBuffer([]byte{})
    99  
   100  		cmd.SetIn(bytes.NewReader([]byte(in)))
   101  		cmd.SetOut(outputBuffer)
   102  		cmd.SetArgs(args)
   103  
   104  		err := cmd.Execute()
   105  
   106  		if err != nil {
   107  			t.Errorf("unexpected error: %v", err)
   108  			return
   109  		}
   110  
   111  		got := outputBuffer.String()
   112  		if exp != got {
   113  			t.Errorf("expected %s, got %s", exp, got)
   114  		}
   115  	}
   116  }
   117  
   118  func expectOutputAndErr(args []string, expErr string, expOutput string) func(t *testing.T) {
   119  	return func(t *testing.T) {
   120  		cmd := command.NewRootCMD()
   121  		outputBuffer := bytes.NewBuffer([]byte{})
   122  
   123  		cmd.SetOut(outputBuffer)
   124  		cmd.SetArgs(args)
   125  
   126  		err := cmd.Execute()
   127  
   128  		gotErr := ""
   129  		if err != nil {
   130  			gotErr = err.Error()
   131  		}
   132  
   133  		if expErr != gotErr {
   134  			t.Errorf("expected err %s, got %s", expErr, gotErr)
   135  		}
   136  
   137  		gotOutput := outputBuffer.String()
   138  		if expOutput != gotOutput {
   139  			t.Errorf("expected:\n%s\ngot:\n%s", expOutput, gotOutput)
   140  		}
   141  	}
   142  }