...

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

Documentation: github.com/codeskyblue/go-sh

     1  package sh
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestAlias(t *testing.T) {
    12  	s := NewSession()
    13  	s.Alias("gr", "echo", "hi")
    14  	out, err := s.Command("gr", "sky").Output()
    15  	if err != nil {
    16  		t.Error(err)
    17  	}
    18  	if string(out) != "hi sky\n" {
    19  		t.Errorf("expect 'hi sky' but got:%s", string(out))
    20  	}
    21  }
    22  
    23  func ExampleSession_Command() {
    24  	s := NewSession()
    25  	out, err := s.Command("echo", "hello").Output()
    26  	if err != nil {
    27  		log.Fatal(err)
    28  	}
    29  	fmt.Println(string(out))
    30  	// Output: hello
    31  }
    32  
    33  func ExampleSession_Command_pipe() {
    34  	s := NewSession()
    35  	out, err := s.Command("echo", "hello", "world").Command("awk", "{print $2}").Output()
    36  	if err != nil {
    37  		log.Fatal(err)
    38  	}
    39  	fmt.Println(string(out))
    40  	// Output: world
    41  }
    42  
    43  func ExampleSession_Alias() {
    44  	s := NewSession()
    45  	s.Alias("alias_echo_hello", "echo", "hello")
    46  	out, err := s.Command("alias_echo_hello", "world").Output()
    47  	if err != nil {
    48  		log.Fatal(err)
    49  	}
    50  	fmt.Println(string(out))
    51  	// Output: hello world
    52  }
    53  
    54  func TestEcho(t *testing.T) {
    55  	out, err := Echo("one two three").Command("wc", "-w").Output()
    56  	if err != nil {
    57  		t.Error(err)
    58  	}
    59  	if strings.TrimSpace(string(out)) != "3" {
    60  		t.Errorf("expect '3' but got:%s", string(out))
    61  	}
    62  }
    63  
    64  func TestSession(t *testing.T) {
    65  	if runtime.GOOS == "windows" {
    66  		t.Log("ignore test on windows")
    67  		return
    68  	}
    69  	session := NewSession()
    70  	session.ShowCMD = true
    71  	err := session.Call("pwd")
    72  	if err != nil {
    73  		t.Error(err)
    74  	}
    75  	out, err := session.SetDir("/").Command("pwd").Output()
    76  	if err != nil {
    77  		t.Error(err)
    78  	}
    79  	if string(out) != "/\n" {
    80  		t.Errorf("expect /, but got %s", string(out))
    81  	}
    82  }
    83  
    84  /*
    85  	#!/bin/bash -
    86  	#
    87  	export PATH=/usr/bin:/bin
    88  	alias ll='ls -l'
    89  	cd /usr
    90  	if test -d "local"
    91  	then
    92  		ll local | awk '{print $1, $NF}' | grep bin
    93  	fi
    94  */
    95  func Example(t *testing.T) {
    96  	s := NewSession()
    97  	//s.ShowCMD = true
    98  	s.Env["PATH"] = "/usr/bin:/bin"
    99  	s.SetDir("/bin")
   100  	s.Alias("ll", "ls", "-l")
   101  
   102  	if s.Test("d", "local") {
   103  		//s.Command("ll", []string{"local"}).Command("awk", []string{"{print $1, $NF}"}).Command("grep", []string{"bin"}).Run()
   104  		s.Command("ll", "local").Command("awk", "{print $1, $NF}").Command("grep", "bin").Run()
   105  	}
   106  }
   107  

View as plain text