github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/drycc_test.go (about)

     1  package main
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestHelpReformatting(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	tests := []string{"--help", "-h", "help"}
    14  	expected := "help"
    15  
    16  	for _, test := range tests {
    17  		actual, argv := parseArgs([]string{test})
    18  
    19  		if actual != expected {
    20  			t.Errorf("Expected %s, Got %s", expected, actual)
    21  		}
    22  
    23  		if len(argv) != 1 {
    24  			t.Errorf("Expected length of 1, Got %d", len(argv))
    25  		}
    26  	}
    27  }
    28  
    29  func TestHelpReformattingWithCommand(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	tests := []string{"--help", "-h", "help"}
    33  	expected := "test"
    34  	expectedArgv := []string{"test", "--help"}
    35  
    36  	for _, test := range tests {
    37  		actual, argv := parseArgs([]string{test, "test"})
    38  
    39  		if actual != expected {
    40  			t.Errorf("Expected %s, Got %s", expected, actual)
    41  		}
    42  
    43  		if !reflect.DeepEqual(expectedArgv, argv) {
    44  			t.Errorf("Expected %v, Got %v", expectedArgv, argv)
    45  		}
    46  	}
    47  }
    48  
    49  func TestCommandSplitting(t *testing.T) {
    50  	t.Parallel()
    51  
    52  	expected := "apps"
    53  	expectedArgv := []string{"apps:create", "test", "foo"}
    54  
    55  	actual, argv := parseArgs(expectedArgv)
    56  
    57  	if actual != expected {
    58  		t.Errorf("Expected %s, Got %s", expected, actual)
    59  	}
    60  
    61  	if !reflect.DeepEqual(expectedArgv, argv) {
    62  		t.Errorf("Expected %v, Got %v", expectedArgv, argv)
    63  	}
    64  }
    65  
    66  func TestTopLevelCommandArgsPreparing(t *testing.T) {
    67  	t.Parallel()
    68  
    69  	command := "ssh"
    70  	argv := []string{"ssh"}
    71  	expected := []string{"drycc-ssh"}
    72  	actual := prepareCmdArgs(command, argv)
    73  
    74  	if !reflect.DeepEqual(expected, actual) {
    75  		t.Errorf("Expected %v, Got %v", expected, actual)
    76  	}
    77  }
    78  
    79  func TestCommandWithParameterArgsPreparing(t *testing.T) {
    80  	t.Parallel()
    81  
    82  	command := "ssh --help"
    83  	argv := []string{"ssh --help"}
    84  	expected := []string{"drycc-ssh --help"}
    85  	actual := prepareCmdArgs(command, argv)
    86  
    87  	if !reflect.DeepEqual(expected, actual) {
    88  		t.Errorf("Expected %v, Got %v", expected, actual)
    89  	}
    90  }
    91  
    92  func TestReplaceShortcutRepalce(t *testing.T) {
    93  	t.Parallel()
    94  
    95  	expected := "apps:create"
    96  	actual := replaceShortcut("create")
    97  
    98  	if expected != actual {
    99  		t.Errorf("Expected %s, Got %s", expected, actual)
   100  	}
   101  }
   102  
   103  func TestReplaceShortcutUnchanged(t *testing.T) {
   104  	t.Parallel()
   105  
   106  	expected := "users:list"
   107  	actual := replaceShortcut(expected)
   108  
   109  	if expected != actual {
   110  		t.Errorf("Expected %s, Got %s", expected, actual)
   111  	}
   112  }
   113  
   114  func TestGetConfigFlag(t *testing.T) {
   115  	t.Parallel()
   116  
   117  	expected := "the-config-flag"
   118  	argv := []string{
   119  		"lorem",
   120  		"ipsum",
   121  		"--config=" + expected,
   122  	}
   123  	actual := getConfigFlag(argv)
   124  	assert.Equal(t, actual, expected, "config-flag")
   125  
   126  	argv = []string{
   127  		"lorem",
   128  		"ipsum",
   129  		"-c",
   130  		expected,
   131  	}
   132  	actual = getConfigFlag(argv)
   133  	assert.Equal(t, actual, expected, "config-flag")
   134  
   135  	expected = ""
   136  	argv = []string{
   137  		"--",
   138  		"ipsum",
   139  		"--config=config",
   140  	}
   141  	actual = getConfigFlag(argv)
   142  	assert.Equal(t, actual, expected, "config-flag")
   143  
   144  	argv = []string{
   145  		"--",
   146  		"ipsum",
   147  		"-c",
   148  		"config",
   149  	}
   150  	actual = getConfigFlag(argv)
   151  	assert.Equal(t, actual, expected, "config-flag")
   152  }
   153  
   154  func TestRemoveConfigFlag(t *testing.T) {
   155  	t.Parallel()
   156  	expected := []string{
   157  		"lorem",
   158  		"ipsum",
   159  	}
   160  
   161  	argv := []string{
   162  		"lorem",
   163  		"ipsum",
   164  		"--config=the-config-flag",
   165  	}
   166  	actual := removeConfigFlag(argv)
   167  	assert.Equal(t, actual, expected, "args")
   168  
   169  	argv = []string{
   170  		"lorem",
   171  		"ipsum",
   172  		"-c",
   173  		"the-config-flag",
   174  	}
   175  	actual = removeConfigFlag(argv)
   176  	assert.Equal(t, actual, expected, "args")
   177  
   178  	expected = []string{
   179  		"--",
   180  		"sh",
   181  		"-c",
   182  		"the-config-flag",
   183  	}
   184  	argv = []string{
   185  		"--",
   186  		"sh",
   187  		"-c",
   188  		"the-config-flag",
   189  	}
   190  	actual = removeConfigFlag(argv)
   191  	assert.Equal(t, actual, expected, "args")
   192  
   193  	expected = []string{
   194  		"--",
   195  		"sh",
   196  		"--config=the-config-flag",
   197  	}
   198  	argv = []string{
   199  		"--",
   200  		"sh",
   201  		"--config=the-config-flag",
   202  	}
   203  	actual = removeConfigFlag(argv)
   204  	assert.Equal(t, actual, expected, "args")
   205  }