github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/internal/codespaces/ssh_test.go (about)

     1  package codespaces
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  type parseTestCase struct {
     9  	Args       []string
    10  	ParsedArgs []string
    11  	Command    []string
    12  	Error      string
    13  }
    14  
    15  func TestParseSSHArgs(t *testing.T) {
    16  	testCases := []parseTestCase{
    17  		{}, // empty test case
    18  		{
    19  			Args:       []string{"-X", "-Y"},
    20  			ParsedArgs: []string{"-X", "-Y"},
    21  			Command:    nil,
    22  		},
    23  		{
    24  			Args:       []string{"-X", "-Y", "-o", "someoption=test"},
    25  			ParsedArgs: []string{"-X", "-Y", "-o", "someoption=test"},
    26  			Command:    nil,
    27  		},
    28  		{
    29  			Args:       []string{"-X", "-Y", "-o", "someoption=test", "somecommand"},
    30  			ParsedArgs: []string{"-X", "-Y", "-o", "someoption=test"},
    31  			Command:    []string{"somecommand"},
    32  		},
    33  		{
    34  			Args:       []string{"-X", "-Y", "-o", "someoption=test", "echo", "test"},
    35  			ParsedArgs: []string{"-X", "-Y", "-o", "someoption=test"},
    36  			Command:    []string{"echo", "test"},
    37  		},
    38  		{
    39  			Args:       []string{"somecommand"},
    40  			ParsedArgs: []string{},
    41  			Command:    []string{"somecommand"},
    42  		},
    43  		{
    44  			Args:       []string{"echo", "test"},
    45  			ParsedArgs: []string{},
    46  			Command:    []string{"echo", "test"},
    47  		},
    48  		{
    49  			Args:       []string{"-v", "echo", "hello", "world"},
    50  			ParsedArgs: []string{"-v"},
    51  			Command:    []string{"echo", "hello", "world"},
    52  		},
    53  		{
    54  			Args:       []string{"-L", "-l"},
    55  			ParsedArgs: []string{"-L", "-l"},
    56  			Command:    nil,
    57  		},
    58  		{
    59  			Args:       []string{"-v", "echo", "-n", "test"},
    60  			ParsedArgs: []string{"-v"},
    61  			Command:    []string{"echo", "-n", "test"},
    62  		},
    63  		{
    64  			Args:       []string{"-v", "echo", "-b", "test"},
    65  			ParsedArgs: []string{"-v"},
    66  			Command:    []string{"echo", "-b", "test"},
    67  		},
    68  		{
    69  			Args:       []string{"-b"},
    70  			ParsedArgs: nil,
    71  			Command:    nil,
    72  			Error:      "flag: -b requires an argument",
    73  		},
    74  	}
    75  
    76  	for _, tcase := range testCases {
    77  		args, command, err := parseSSHArgs(tcase.Args)
    78  
    79  		checkParseResult(t, tcase, args, command, err)
    80  	}
    81  }
    82  
    83  func TestParseSCPArgs(t *testing.T) {
    84  	testCases := []parseTestCase{
    85  		{}, // empty test case
    86  		{
    87  			Args:       []string{"-X", "-Y"},
    88  			ParsedArgs: []string{"-X", "-Y"},
    89  			Command:    nil,
    90  		},
    91  		{
    92  			Args:       []string{"-X", "-Y", "-o", "someoption=test"},
    93  			ParsedArgs: []string{"-X", "-Y", "-o", "someoption=test"},
    94  			Command:    nil,
    95  		},
    96  		{
    97  			Args:       []string{"-X", "-Y", "-o", "someoption=test", "local/file", "remote:file"},
    98  			ParsedArgs: []string{"-X", "-Y", "-o", "someoption=test"},
    99  			Command:    []string{"local/file", "remote:file"},
   100  		},
   101  		{
   102  			Args:       []string{"-X", "-Y", "-o", "someoption=test", "local/file", "remote:file"},
   103  			ParsedArgs: []string{"-X", "-Y", "-o", "someoption=test"},
   104  			Command:    []string{"local/file", "remote:file"},
   105  		},
   106  		{
   107  			Args:       []string{"local/file", "remote:file"},
   108  			ParsedArgs: []string{},
   109  			Command:    []string{"local/file", "remote:file"},
   110  		},
   111  		{
   112  			Args:       []string{"-c"},
   113  			ParsedArgs: nil,
   114  			Command:    nil,
   115  			Error:      "flag: -c requires an argument",
   116  		},
   117  	}
   118  
   119  	for _, tcase := range testCases {
   120  		args, command, err := parseSCPArgs(tcase.Args)
   121  
   122  		checkParseResult(t, tcase, args, command, err)
   123  	}
   124  }
   125  
   126  func checkParseResult(t *testing.T, tcase parseTestCase, gotArgs, gotCmd []string, gotErr error) {
   127  	if tcase.Error != "" {
   128  		if gotErr == nil {
   129  			t.Errorf("expected error and got nil: %#v", tcase)
   130  		}
   131  
   132  		if gotErr.Error() != tcase.Error {
   133  			t.Errorf("error does not match expected error, got: '%s', expected: '%s'", gotErr.Error(), tcase.Error)
   134  		}
   135  
   136  		return
   137  	}
   138  
   139  	if gotErr != nil {
   140  		t.Errorf("unexpected error: %v on test case: %#v", gotErr, tcase)
   141  		return
   142  	}
   143  
   144  	argsStr, parsedArgsStr := fmt.Sprintf("%s", gotArgs), fmt.Sprintf("%s", tcase.ParsedArgs)
   145  	if argsStr != parsedArgsStr {
   146  		t.Errorf("args do not match parsed args. got: '%s', expected: '%s'", argsStr, parsedArgsStr)
   147  	}
   148  
   149  	commandStr, parsedCommandStr := fmt.Sprintf("%s", gotCmd), fmt.Sprintf("%s", tcase.Command)
   150  	if commandStr != parsedCommandStr {
   151  		t.Errorf("command does not match parsed command. got: '%s', expected: '%s'", commandStr, parsedCommandStr)
   152  	}
   153  }