...

Source file src/github.com/codeskyblue/go-sh/pipe_test.go

Documentation: github.com/codeskyblue/go-sh

     1  package sh
     2  
     3  import (
     4  	"encoding/xml"
     5  	"io"
     6  	"os"
     7  	"os/exec"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestUnmarshalJSON(t *testing.T) {
    14  	var a int
    15  	s := NewSession()
    16  	s.ShowCMD = true
    17  	err := s.Command("echo", []string{"1"}).UnmarshalJSON(&a)
    18  	if err != nil {
    19  		t.Error(err)
    20  	}
    21  	if a != 1 {
    22  		t.Errorf("expect a tobe 1, but got %d", a)
    23  	}
    24  }
    25  
    26  func TestUnmarshalXML(t *testing.T) {
    27  	s := NewSession()
    28  	xmlSample := `<?xml version="1.0" encoding="utf-8"?>
    29  <server version="1" />`
    30  	type server struct {
    31  		XMLName xml.Name `xml:"server"`
    32  		Version string   `xml:"version,attr"`
    33  	}
    34  	data := &server{}
    35  	s.Command("echo", xmlSample).UnmarshalXML(data)
    36  	if data.Version != "1" {
    37  		t.Error(data)
    38  	}
    39  }
    40  
    41  func TestPipe(t *testing.T) {
    42  	s := NewSession()
    43  	s.ShowCMD = true
    44  	s.Call("echo", "hello")
    45  	err := s.Command("echo", "hi").Command("cat", "-n").Start()
    46  	if err != nil {
    47  		t.Error(err)
    48  	}
    49  	err = s.Wait()
    50  	if err != nil {
    51  		t.Error(err)
    52  	}
    53  	out, err := s.Command("echo", []string{"hello"}).Output()
    54  	if err != nil {
    55  		t.Error(err)
    56  	}
    57  	if string(out) != "hello\n" {
    58  		t.Error("capture wrong output:", out)
    59  	}
    60  	s.Command("echo", []string{"hello\tworld"}).Command("cut", []string{"-f2"}).Run()
    61  }
    62  
    63  func TestPipeCommand(t *testing.T) {
    64  	c1 := exec.Command("echo", "good")
    65  	rd, wr := io.Pipe()
    66  	c1.Stdout = wr
    67  	c2 := exec.Command("cat", "-n")
    68  	c2.Stdout = os.Stdout
    69  	c2.Stdin = rd
    70  	c1.Start()
    71  	c2.Start()
    72  
    73  	c1.Wait()
    74  	wc, ok := c1.Stdout.(io.WriteCloser)
    75  	if ok {
    76  		wc.Close()
    77  	}
    78  	c2.Wait()
    79  }
    80  
    81  func TestPipeInput(t *testing.T) {
    82  	s := NewSession()
    83  	s.ShowCMD = true
    84  	s.SetInput("first line\nsecond line\n")
    85  	out, err := s.Command("grep", "second").Output()
    86  	if err != nil {
    87  		t.Error(err)
    88  	}
    89  	if string(out) != "second line\n" {
    90  		t.Error("capture wrong output:", out)
    91  	}
    92  }
    93  
    94  func TestTimeout(t *testing.T) {
    95  	s := NewSession()
    96  	err := s.Command("sleep", "2").Start()
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	err = s.WaitTimeout(time.Second)
   101  	if err != ErrExecTimeout {
   102  		t.Fatal(err)
   103  	}
   104  }
   105  
   106  func TestSetTimeout(t *testing.T) {
   107  	s := NewSession()
   108  	s.SetTimeout(time.Second)
   109  	defer s.SetTimeout(0)
   110  	err := s.Command("sleep", "2").Run()
   111  	if err != ErrExecTimeout {
   112  		t.Fatal(err)
   113  	}
   114  }
   115  
   116  func TestCombinedOutput(t *testing.T) {
   117  	s := NewSession()
   118  	bytes, err := s.Command("sh", "-c", "echo stderr >&2 ; echo stdout").CombinedOutput()
   119  	if err != nil {
   120  		t.Error(err)
   121  	}
   122  	stringOutput := string(bytes)
   123  	if !(strings.Contains(stringOutput, "stdout") && strings.Contains(stringOutput, "stderr")) {
   124  		t.Errorf("expect output from both output streams, got '%s'", strings.TrimSpace(stringOutput))
   125  	}
   126  }
   127  
   128  func TestPipeFail(t *testing.T) {
   129  	sh := NewSession()
   130  	sh.PipeFail = true
   131  	sh.PipeStdErrors = true
   132  	sh.Command("cat", "unknown-file")
   133  	sh.Command("echo")
   134  	if _, err := sh.Output(); err == nil {
   135  		t.Error("expected error")
   136  	}
   137  }
   138  

View as plain text