github.com/criteo/command-launcher@v0.0.0-20230407142452-fb616f546e98/internal/helper/exec-cmd_test.go (about)

     1  package helper
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestCallExternalWithOutput(t *testing.T) {
    11  	cwd, _ := os.Getwd()
    12  	code, output, err := CallExternalWithOutput([]string{}, cwd, "echo", "hello world!")
    13  
    14  	assert.Equal(t, 0, code)
    15  	// Note here the returned string contains a newline different in fucntion of the OS
    16  	assert.Contains(t, output, "hello world!")
    17  	assert.Nil(t, err)
    18  }
    19  
    20  func TestCallExternalWithNonZeroExitCode(t *testing.T) {
    21  	cwd, _ := os.Getwd()
    22  	code, _, err := CallExternalWithOutput([]string{}, cwd, "ls", "folder-not-exists")
    23  
    24  	assert.NotEqual(t, 0, code)
    25  	assert.NotNil(t, err)
    26  }
    27  
    28  func TestCallExternalWithWrongWorkingDirectory(t *testing.T) {
    29  	code, err := CallExternalStdOut([]string{}, "folder-not-exists", "ls")
    30  
    31  	assert.NotEqual(t, 0, code)
    32  	assert.NotNil(t, err)
    33  }
    34  
    35  func TestCallExternalNoStdOut(t *testing.T) {
    36  	cwd, _ := os.Getwd()
    37  	code, err := CallExternalNoStdOut([]string{}, cwd, "echo", "hello world!")
    38  	// TODO: check no output is in stdout
    39  	assert.Equal(t, 0, code)
    40  	assert.Nil(t, err)
    41  
    42  	code, err = CallExternalNoStdOut([]string{}, cwd, "ls", "folder-not-exists")
    43  	// TODO: check no error is show in stderr and stdout
    44  	assert.NotEqual(t, 0, code)
    45  	assert.NotNil(t, err)
    46  }