github.com/madlambda/nash@v0.2.2-0.20230113003044-f2284521680b/internal/sh/shell_regression_test.go (about)

     1  package sh_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  	"path"
     9  	"fmt"
    10  )
    11  
    12  func TestExecuteIssue68(t *testing.T) {
    13  	f, cleanup := setup(t)
    14  	defer cleanup()
    15  	
    16  	sh := f.shell
    17  
    18  	tmpDir, err := ioutil.TempDir("", "nash-tests")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	file := path.Join(tmpDir, "la")
    24  	err = sh.Exec("-input-", fmt.Sprintf(`echo lalalala | grep la > %s`, file))
    25  	if err != nil {
    26  		t.Error(err)
    27  		return
    28  	}
    29  
    30  	defer os.Remove(file)
    31  
    32  	contents, err := ioutil.ReadFile(file)
    33  
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	contentStr := strings.TrimSpace(string(contents))
    39  	if contentStr != "lalalala" {
    40  		t.Errorf("Strings differ: '%s' != '%s'", contentStr, "lalalala")
    41  		return
    42  	}
    43  }
    44  
    45  func TestExecuteErrorSuppression(t *testing.T) {
    46  	f, cleanup := setup(t)
    47  	defer cleanup()
    48  
    49  	sh := f.shell
    50  	err := sh.Exec("-input-", `-bllsdlfjlsd`)
    51  
    52  	if err != nil {
    53  		t.Errorf("Expected to not fail...: %s", err.Error())
    54  		return
    55  	}
    56  
    57  	// issue #72
    58  	err = sh.Exec("-input-", `echo lalala | -grep lelele`)
    59  
    60  	if err != nil {
    61  		t.Errorf("Expected to not fail...:(%s)", err.Error())
    62  		return
    63  	}
    64  }