github.com/reconquest/executil-go@v0.0.0-20181110204642-1f5c2d67813f/error_test.go (about)

     1  package executil
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestError_Output_IsCombinedOutputOfExecutedCommand(t *testing.T) {
    11  	test := assert.New(t)
    12  
    13  	for _, command := range commandsWithError {
    14  		if !command.isExitError {
    15  			continue
    16  		}
    17  
    18  		_, _, err := Run(command.get())
    19  		test.IsType(&Error{}, err)
    20  		test.Equal("stdout\nstderr\n", string(err.(*Error).Output))
    21  	}
    22  }
    23  
    24  func TestError_Cmd_IsActualExecutedCommand(t *testing.T) {
    25  	test := assert.New(t)
    26  
    27  	for _, command := range commandsWithError {
    28  		cmd := command.get()
    29  		_, _, err := Run(cmd)
    30  		test.IsType(&Error{}, err)
    31  		test.Equal(cmd, err.(*Error).Cmd)
    32  	}
    33  }
    34  
    35  func TestError_RunErr_IsActualErrorOfExecutedCommand(t *testing.T) {
    36  	test := assert.New(t)
    37  
    38  	for _, command := range commandsWithError {
    39  		_, _, err := Run(command.get())
    40  		test.IsType(&Error{}, err)
    41  
    42  		test.Equal(
    43  			command.get().Run().Error(), err.(*Error).RunErr.Error(),
    44  		)
    45  	}
    46  }
    47  
    48  func TestError_Error_ContainsOfActualError(t *testing.T) {
    49  	test := assert.New(t)
    50  
    51  	for _, command := range commandsWithError {
    52  		_, _, err := Run(command.get())
    53  		test.Contains(err.Error(), command.get().Run().Error())
    54  	}
    55  }
    56  
    57  func TestError_Error_ContainsOfCommandArgs(t *testing.T) {
    58  	test := assert.New(t)
    59  
    60  	for _, command := range commandsWithError {
    61  		_, _, err := Run(command.get())
    62  		test.Contains(err.Error(), fmt.Sprintf("%q", command.get().Args))
    63  	}
    64  }
    65  
    66  func TestError_Error_ContainsOfOutputIfOutputIsNotEmpty(t *testing.T) {
    67  	test := assert.New(t)
    68  
    69  	for _, command := range commandsWithError {
    70  		if !command.isExitError {
    71  			continue
    72  		}
    73  
    74  		_, _, err := Run(command.get())
    75  		expectedOutput, _ := command.get().CombinedOutput()
    76  		test.Contains(err.Error(), string(expectedOutput))
    77  	}
    78  }
    79  
    80  func TestError_Error_ContainsOfMessageWithOutputIfOutputIsNotEmpty(
    81  	t *testing.T,
    82  ) {
    83  	test := assert.New(t)
    84  
    85  	for _, command := range commandsWithError {
    86  		if !command.isExitError {
    87  			continue
    88  		}
    89  
    90  		_, _, err := Run(command.get())
    91  		test.Contains(err.Error(), "with output")
    92  		test.NotContains(err.Error(), "without output")
    93  	}
    94  }
    95  
    96  func TestError_Error_ContainsOfMessageWithoutOutputIfOutputIsEmpty(
    97  	t *testing.T,
    98  ) {
    99  	test := assert.New(t)
   100  
   101  	for _, command := range commandsWithError {
   102  		if command.isExitError {
   103  			continue
   104  		}
   105  
   106  		_, _, err := Run(command.get())
   107  		test.Contains(err.Error(), "without output")
   108  		test.NotContains(err.Error(), "with output")
   109  	}
   110  }