gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/helpers/build_section_test.go (about)

     1  package helpers_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"gitlab.com/gitlab-org/gitlab-runner/helpers"
    11  )
    12  
    13  type testBuffer struct {
    14  	bytes.Buffer
    15  	Error error
    16  }
    17  
    18  func (b *testBuffer) SendRawLog(args ...interface{}) {
    19  	if b.Error != nil {
    20  		return
    21  	}
    22  
    23  	_, b.Error = b.WriteString(fmt.Sprintln(args...))
    24  }
    25  
    26  func TestBuildSection(t *testing.T) {
    27  	for num, tc := range []struct {
    28  		name        string
    29  		skipMetrics bool
    30  		error       error
    31  	}{
    32  		{"Success", false, nil},
    33  		{"Failure", false, fmt.Errorf("Failing test")},
    34  		{"SkipMetricsSuccess", true, nil},
    35  		{"SkipMetricsFailure", true, fmt.Errorf("Failing test")},
    36  	} {
    37  		t.Run(tc.name, func(t *testing.T) {
    38  			logger := new(testBuffer)
    39  
    40  			section := helpers.BuildSection{
    41  				Name:        tc.name,
    42  				SkipMetrics: tc.skipMetrics,
    43  				Run:         func() error { return tc.error },
    44  			}
    45  			section.Execute(logger)
    46  
    47  			output := logger.String()
    48  			assert.Nil(t, logger.Error, "case %d: Error: %s", num, logger.Error)
    49  			for _, str := range []string{"section_start:", "section_end:", tc.name} {
    50  				if tc.skipMetrics {
    51  					assert.NotContains(t, output, str)
    52  				} else {
    53  					assert.Contains(t, output, str)
    54  				}
    55  			}
    56  		})
    57  	}
    58  }