github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/build/pipeline_state_test.go (about)

     1  package build
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/tilt-dev/tilt/pkg/logger"
    14  )
    15  
    16  // NOTE(dmiller): set at runtime with:
    17  // go test -ldflags="-X 'github.com/tilt-dev/tilt/internal/build.PipelineStateWriteGoldenMaster=1'" ./internal/build -run ^TestPipeline
    18  var PipelineStateWriteGoldenMaster = "0"
    19  
    20  func TestPipeline(t *testing.T) {
    21  	var err error
    22  	out := &bytes.Buffer{}
    23  	ctx := logger.WithLogger(context.Background(), logger.NewLogger(logger.InfoLvl, out))
    24  	ps := NewPipelineState(ctx, 1, fakeClock{})
    25  	ps.StartPipelineStep(ctx, "%s %s", "hello", "world")
    26  	ps.Printf(ctx, "in ur step")
    27  	ps.EndPipelineStep(ctx)
    28  	ps.End(ctx, err)
    29  
    30  	assertSnapshot(t, out.String())
    31  }
    32  
    33  func TestPipelineErrored(t *testing.T) {
    34  	err := fmt.Errorf("oh noes")
    35  	out := &bytes.Buffer{}
    36  	ctx := logger.WithLogger(context.Background(), logger.NewLogger(logger.InfoLvl, out))
    37  	ps := NewPipelineState(ctx, 1, fakeClock{})
    38  	ps.StartPipelineStep(ctx, "%s %s", "hello", "world")
    39  	ps.Printf(ctx, "in ur step")
    40  	ps.EndPipelineStep(ctx)
    41  	ps.End(ctx, err)
    42  
    43  	assertSnapshot(t, out.String())
    44  }
    45  
    46  func TestPipelineMultilinePrint(t *testing.T) {
    47  	var err error
    48  	out := &bytes.Buffer{}
    49  	ctx := logger.WithLogger(context.Background(), logger.NewLogger(logger.InfoLvl, out))
    50  	ps := NewPipelineState(ctx, 1, fakeClock{})
    51  	ps.StartPipelineStep(ctx, "%s %s", "hello", "world")
    52  	ps.Printf(ctx, "line 1\nline 2\n")
    53  	ps.EndPipelineStep(ctx)
    54  	ps.End(ctx, err)
    55  
    56  	assertSnapshot(t, out.String())
    57  }
    58  
    59  func TestPipelineMultistep(t *testing.T) {
    60  	var err error
    61  	out := &bytes.Buffer{}
    62  	ctx := logger.WithLogger(context.Background(), logger.NewLogger(logger.InfoLvl, out))
    63  	ps := NewPipelineState(ctx, 3, fakeClock{})
    64  	ps.StartPipelineStep(ctx, "%s %s", "hello", "world")
    65  	ps.Printf(ctx, "in ur step")
    66  	ps.EndPipelineStep(ctx)
    67  	ps.StartPipelineStep(ctx, "doing stuff")
    68  	ps.Printf(ctx, "here is some stuff!")
    69  	ps.EndPipelineStep(ctx)
    70  	ps.StartPipelineStep(ctx, "different stuff")
    71  	ps.Printf(ctx, "even more stuff")
    72  	ps.Printf(ctx, "i can't believe it's not stuff")
    73  	ps.EndPipelineStep(ctx)
    74  	ps.End(ctx, err)
    75  
    76  	assertSnapshot(t, out.String())
    77  }
    78  
    79  func assertSnapshot(t *testing.T, output string) {
    80  	d1 := []byte(output)
    81  	gmPath := fmt.Sprintf("testdata/%s_master", t.Name())
    82  	if PipelineStateWriteGoldenMaster == "1" {
    83  		err := os.WriteFile(gmPath, d1, 0644)
    84  		if err != nil {
    85  			t.Fatal(err)
    86  		}
    87  	}
    88  	expected, err := os.ReadFile(gmPath)
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	assert.Equal(t, normalize(string(expected)), normalize(output))
    94  }
    95  
    96  func normalize(str string) string {
    97  	return strings.ReplaceAll(str, "\r\n", "\n")
    98  }