github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/exec/run_test.go (about)

     1  package exec_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/fossas/fossa-cli/exec"
    11  )
    12  
    13  func TestSetEnv(t *testing.T) {
    14  	c, _ := exec.BuildExec(exec.Cmd{
    15  		Name: "example",
    16  		Env: map[string]string{
    17  			"foo": "bar",
    18  		},
    19  	})
    20  
    21  	assert.Equal(t, []string{"foo=bar"}, c.Env)
    22  	assert.Len(t, c.Env, 1)
    23  }
    24  
    25  func TestAppendEnv(t *testing.T) {
    26  	os.Setenv("alice", "bob")
    27  	c, _ := exec.BuildExec(exec.Cmd{
    28  		Name: "example",
    29  		WithEnv: map[string]string{
    30  			"foo": "bar",
    31  		},
    32  	})
    33  
    34  	assert.Contains(t, c.Env, "foo=bar")
    35  	assert.Contains(t, c.Env, "alice=bob")
    36  }
    37  
    38  func TestDefaultEnv(t *testing.T) {
    39  	os.Setenv("alice", "bob")
    40  	c, _ := exec.BuildExec(exec.Cmd{
    41  		Name: "example",
    42  	})
    43  
    44  	assert.Contains(t, c.Env, "alice=bob")
    45  }
    46  
    47  func TestRun(t *testing.T) {
    48  	command := exec.Cmd{
    49  		Name: "pwd",
    50  	}
    51  
    52  	stdout, stderr, err := exec.Run(command)
    53  	assert.NoError(t, err)
    54  	assert.NotEmpty(t, stdout)
    55  	assert.Empty(t, stderr)
    56  }
    57  
    58  func TestRunFails(t *testing.T) {
    59  	command := exec.Cmd{
    60  		Name: "FakeCommand",
    61  	}
    62  
    63  	stdout, stderr, err := exec.Run(command)
    64  	assert.Error(t, err)
    65  	assert.Empty(t, stdout)
    66  	assert.Empty(t, stderr)
    67  }
    68  
    69  func TestRunTimeoutSucceeds(t *testing.T) {
    70  	command := exec.Cmd{
    71  		Name:    "pwd",
    72  		Timeout: "3s",
    73  	}
    74  
    75  	stdout, stderr, err := exec.Run(command)
    76  	assert.NoError(t, err)
    77  	assert.NotEmpty(t, stdout)
    78  	assert.Empty(t, stderr)
    79  }
    80  
    81  func TestRunTimeoutTimesOut(t *testing.T) {
    82  	command := exec.Cmd{
    83  		Name:    "sleep",
    84  		Argv:    []string{"2"},
    85  		Timeout: "3s",
    86  	}
    87  	_, _, err := exec.Run(command)
    88  	assert.NoError(t, err)
    89  
    90  	command.Timeout = "1s"
    91  	_, _, err = exec.Run(command)
    92  	assert.Contains(t, err.Error(), "timed out")
    93  }
    94  
    95  func TestRunRetry(t *testing.T) {
    96  	command := exec.Cmd{
    97  		Name:    "sleep",
    98  		Argv:    []string{"4"},
    99  		Timeout: "1s",
   100  		Retries: 1,
   101  	}
   102  
   103  	start := time.Now()
   104  	_, _, err := exec.Run(command)
   105  	assert.Error(t, err)
   106  	assert.WithinDuration(t, start, time.Now(), 3*time.Second)
   107  
   108  	command.Retries = 5
   109  	start = time.Now()
   110  	_, _, err = exec.Run(command)
   111  	end := time.Now()
   112  	assert.Error(t, err)
   113  	// 5 retries with a 1 second timeout means the command should take more than 6 seconds,
   114  	// opposed to the 4 seconds it would take without the timeout.
   115  	assert.True(t, end.Sub(start) > 6*time.Second)
   116  }