github.com/jenkins-x/jx-api@v0.0.24/pkg/util/commands_test.go (about)

     1  package util_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/jenkins-x/jx-api/pkg/util"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func testCommand(t *testing.T, file string) {
    16  	startPath, err := filepath.Abs("")
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  	tempfile, err := os.Create(filepath.Join(startPath, "/test_data/scripts", file))
    21  	assert.NoError(t, err)
    22  	tempfile.Close()
    23  	defer os.Remove(tempfile.Name())
    24  
    25  	cmd := util.Command{
    26  		Name:    getFailIteratorScript(),
    27  		Dir:     filepath.Join(startPath, "/test_data/scripts"),
    28  		Args:    []string{file, "100"},
    29  		Timeout: 3 * time.Second,
    30  	}
    31  
    32  	res, err := cmd.RunWithoutRetry()
    33  
    34  	assert.Error(t, err, "Run should exit with failure")
    35  	assert.Equal(t, "FAILURE!", res)
    36  	assert.Equal(t, true, cmd.DidError())
    37  	assert.Equal(t, true, cmd.DidFail())
    38  	assert.Equal(t, 1, len(cmd.Errors))
    39  	assert.Equal(t, 1, cmd.Attempts())
    40  }
    41  func TestRunWithoutRetry(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	tmpFileName := "test_run_without_retry.txt"
    45  	testCommand(t, tmpFileName)
    46  }
    47  
    48  func TestRunVerbose(t *testing.T) {
    49  	t.Parallel()
    50  
    51  	tmpFileName := "test_run_verbose.txt"
    52  	testCommand(t, tmpFileName)
    53  }
    54  
    55  func TestRunQuiet(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	tmpFileName := "test_run_quiet.txt"
    59  	testCommand(t, tmpFileName)
    60  }
    61  
    62  func getFailIteratorScript() string {
    63  	ex := "fail_iterator.sh"
    64  	if runtime.GOOS == "windows" {
    65  		ex = "fail_iterator.bat"
    66  	}
    67  	return ex
    68  }