get.porter.sh/porter@v1.3.0/pkg/exec/builder/output_regex_test.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"get.porter.sh/porter/pkg/portercontext"
     9  	"get.porter.sh/porter/pkg/runtime"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  type TestRegexOutput struct {
    15  	Name  string
    16  	Regex string
    17  }
    18  
    19  func (o TestRegexOutput) GetName() string {
    20  	return o.Name
    21  }
    22  
    23  func (o TestRegexOutput) GetRegex() string {
    24  	return o.Regex
    25  }
    26  
    27  func TestTestRegexOutputs(t *testing.T) {
    28  	ctx := context.Background()
    29  	c := runtime.NewTestRuntimeConfig(t)
    30  
    31  	step := TestStep{
    32  		Outputs: []Output{
    33  			TestRegexOutput{Name: "failed-test", Regex: `--- FAIL: (.*) \(.*\)`},
    34  		},
    35  	}
    36  
    37  	stdout := `--- FAIL: TestMixin_Install (0.00s)
    38  stuff
    39  things
    40  --- FAIL: TestMixin_Upgrade (0.00s)
    41  more
    42  logs`
    43  	err := ProcessRegexOutputs(ctx, c.RuntimeConfig, step, stdout)
    44  	require.NoError(t, err, "ProcessRegexOutputs should not return an error")
    45  
    46  	f := filepath.Join(portercontext.MixinOutputsDir, "failed-test")
    47  	gotOutput, err := c.FileSystem.ReadFile(f)
    48  	require.NoError(t, err, "could not read output file %s", f)
    49  
    50  	wantOutput := `TestMixin_Install
    51  TestMixin_Upgrade`
    52  
    53  	assert.Equal(t, wantOutput, string(gotOutput))
    54  }