github.com/ericwq/aprilsh@v0.0.0-20240517091432-958bc568daa0/frontend/client/client_linux_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  		{ // by default, we can't login with ssh
    20  			"only password auth, no ssh agent, no public key file",
    21  			[]string{frontend.CommandClientName, "-vv", "ide@localhost"},
    22  			"xterm-256color",
    23  			[]string{"Failed to connect ssh agent", "Unable to read private key", "inappropriate ioctl for device"},
    24  		},
    25  	}
    26  
    27  	for _, v := range tc {
    28  		t.Run(v.label, func(t *testing.T) {
    29  			// intercept stdout
    30  			saveStdout := os.Stdout
    31  			r, w, _ := os.Pipe()
    32  			os.Stdout = w
    33  
    34  			// prepare data
    35  			os.Args = v.args
    36  			os.Setenv("TERM", v.term)
    37  			// test main
    38  			main()
    39  
    40  			// restore stdout
    41  			w.Close()
    42  			out, _ := io.ReadAll(r)
    43  			os.Stdout = saveStdout
    44  			r.Close()
    45  
    46  			// validate the result
    47  			result := string(out)
    48  			found := 0
    49  			for i := range v.expect {
    50  				if strings.Contains(result, v.expect[i]) {
    51  					// fmt.Printf("found %s\n", expect[i])
    52  					found++
    53  				}
    54  			}
    55  			if found != len(v.expect) {
    56  				t.Errorf("#test expect %s, got \n%s\n", v.expect, result)
    57  			}
    58  		})
    59  	}
    60  }