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