github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zshell/shell_test.go (about)

     1  package zshell
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/sohaha/zlsgo/zutil"
    12  
    13  	"github.com/sohaha/zlsgo"
    14  )
    15  
    16  func TestPipe(t *testing.T) {
    17  	if zutil.IsWin() {
    18  		t.Log("ignore windows")
    19  		return
    20  	}
    21  	tt := zlsgo.NewTest(t)
    22  	ctx := context.Background()
    23  
    24  	commands := [][]string{
    25  		{"ls", "-a"},
    26  		{"grep", "go"},
    27  		{"grep", "shell_notwin"},
    28  	}
    29  
    30  	code, outStr, errStr, err := PipeExecCommand(ctx, commands)
    31  	t.Log(outStr, errStr, err)
    32  	tt.EqualExit(0, code)
    33  	tt.EqualExit("shell_notwin.go", strings.Trim(outStr, " \n"))
    34  
    35  	Dir = "../"
    36  	code, outStr, errStr, err = PipeExecCommand(ctx, [][]string{{"ls"}})
    37  	t.Log(code, outStr, errStr, err)
    38  
    39  	code, outStr, errStr, err = PipeExecCommand(ctx, [][]string{})
    40  	t.Log(code, outStr, errStr, err)
    41  }
    42  
    43  func TestBash(t *testing.T) {
    44  	Debug = true
    45  	tt := zlsgo.NewTest(t)
    46  
    47  	var res string
    48  	var errRes string
    49  	var code int
    50  	var err error
    51  
    52  	code, res, errRes, err = Run("")
    53  	tt.EqualExit(1, code)
    54  	tt.EqualExit(true, err != nil)
    55  	t.Log(res, errRes)
    56  
    57  	code, _, _, err = Run("lll")
    58  	tt.EqualExit(-1, code)
    59  	tt.EqualExit(true, err != nil)
    60  	t.Log(err)
    61  
    62  	if !zutil.IsWin() {
    63  		code, _, _, err = Run("ls")
    64  		tt.EqualExit(0, code)
    65  		tt.EqualExit(true, err == nil)
    66  		t.Log(err)
    67  	}
    68  
    69  	_, res, _, err = Run("ls -a /Applications/Google\\ Chrome.app")
    70  	t.Log(res)
    71  	t.Log(err)
    72  
    73  	err = BgRun("")
    74  	tt.EqualExit(true, err != nil)
    75  	err = BgRun("lll")
    76  	tt.EqualExit(true, err != nil)
    77  	t.Log(err)
    78  
    79  	Dir = "."
    80  	Env = []string{"kkk"}
    81  	code, res, errRes, err = OutRun("ls", os.Stdin, os.Stdout, os.Stdin)
    82  	t.Log(res, errRes, code, err)
    83  }
    84  
    85  func TestCallbackRun(t *testing.T) {
    86  	tt := zlsgo.NewTest(t)
    87  
    88  	var i = 0
    89  	var code <-chan int
    90  	var err error
    91  
    92  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    93  	code, _, err = CallbackRunContext(ctx, "ping www.npmjs.com", func(out string, isBasic bool) {
    94  		fmt.Println(out)
    95  		i = i + 1
    96  		if i > 3 {
    97  			cancel()
    98  		}
    99  	})
   100  	tt.NoError(err)
   101  	tt.Log("code", <-code)
   102  }
   103  
   104  func Test_fixCommand(t *testing.T) {
   105  	tt := zlsgo.NewTest(t)
   106  
   107  	e := []string{"ping", "www.npmjs.com"}
   108  	r := fixCommand("ping www.npmjs.com")
   109  	tt.Equal(e, r)
   110  
   111  	e = []string{"ls", "-a", "/Applications/Google Chrome.app"}
   112  	r = fixCommand("ls -a /Applications/Google\\ Chrome.app")
   113  	tt.Equal(e, r)
   114  
   115  	e = []string{"networksetup", "-setwebproxystate", "USB 10/100/1000 LAN", "on"}
   116  	r = fixCommand(`networksetup -setwebproxystate "USB 10/100/1000 LAN" on`)
   117  	tt.Equal(e, r)
   118  
   119  }
   120  
   121  func TestRunBash(t *testing.T) {
   122  	if zutil.IsWin() {
   123  		t.Log(RunBash(context.Background(), "dir"))
   124  	} else {
   125  		t.Log(RunBash(context.Background(), "ls && ls"))
   126  	}
   127  }