github.com/ericwq/aprilsh@v0.0.0-20240517091432-958bc568daa0/frontend/client/client_darwin_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/ericwq/aprilsh/frontend"
    10  )
    11  
    12  func TestMainRun_Parameters2(t *testing.T) {
    13  	tc := []struct {
    14  		label  string
    15  		args   []string
    16  		term   string
    17  		expect []string
    18  	}{
    19  		{
    20  			"on mac, we have SSH_AUTH_SOCK and .ssh/id_rsa.pub .ssh/id_rsa file, so we have ssh agent and public key auths",
    21  			[]string{frontend.CommandClientName, "-vv", "ide@localhost2"},
    22  			"xterm-256color",
    23  			[]string{"No such host"},
    24  		},
    25  	}
    26  
    27  	for _, v := range tc {
    28  		t.Run(v.label, func(t *testing.T) {
    29  			// in case we can't log in with ssh
    30  			if _, err := os.Stat(defaultSSHClientID); err != nil {
    31  				t.Skip("no " + defaultSSHClientID + " skip this")
    32  			}
    33  			// intercept stdout
    34  			saveStdout := os.Stdout
    35  			r, w, _ := os.Pipe()
    36  			os.Stdout = w
    37  
    38  			// prepare data
    39  			os.Args = v.args
    40  			os.Setenv("TERM", v.term)
    41  			// test main
    42  			main()
    43  
    44  			// restore stdout
    45  			w.Close()
    46  			out, _ := io.ReadAll(r)
    47  			os.Stdout = saveStdout
    48  			r.Close()
    49  
    50  			// validate the result
    51  			result := string(out)
    52  			found := 0
    53  			for i := range v.expect {
    54  				if strings.Contains(result, v.expect[i]) {
    55  					// fmt.Printf("found %s\n", expect[i])
    56  					found++
    57  				}
    58  			}
    59  			if found != len(v.expect) {
    60  				t.Errorf("#test expect %s, got \n%s\n", v.expect, result)
    61  			}
    62  		})
    63  	}
    64  }