github.com/grahambrereton-form3/tilt@v0.10.18/internal/build/pipeline_state_test.go (about)

     1  package build
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"testing"
     9  
    10  	"github.com/windmilleng/tilt/pkg/logger"
    11  )
    12  
    13  // NOTE(dmiller): set at runtime with:
    14  // go test -ldflags="-X 'github.com/windmilleng/tilt/internal/build.PipelineStateWriteGoldenMaster=1'" github.com/windmilleng/tilt/internal/build -run ^TestPipeline
    15  var PipelineStateWriteGoldenMaster = "0"
    16  
    17  func TestPipeline(t *testing.T) {
    18  	var err error
    19  	out := &bytes.Buffer{}
    20  	ctx := logger.WithLogger(context.Background(), logger.NewLogger(logger.InfoLvl, out))
    21  	ps := NewPipelineState(ctx, 1, fakeClock{})
    22  	ps.StartPipelineStep(ctx, "%s %s", "hello", "world")
    23  	ps.Printf(ctx, "in ur step")
    24  	ps.EndPipelineStep(ctx)
    25  	ps.End(ctx, err)
    26  
    27  	assertSnapshot(t, out.String())
    28  }
    29  
    30  func TestPipelineErrored(t *testing.T) {
    31  	err := fmt.Errorf("oh noes")
    32  	out := &bytes.Buffer{}
    33  	ctx := logger.WithLogger(context.Background(), logger.NewLogger(logger.InfoLvl, out))
    34  	ps := NewPipelineState(ctx, 1, fakeClock{})
    35  	ps.StartPipelineStep(ctx, "%s %s", "hello", "world")
    36  	ps.Printf(ctx, "in ur step")
    37  	ps.EndPipelineStep(ctx)
    38  	ps.End(ctx, err)
    39  
    40  	assertSnapshot(t, out.String())
    41  }
    42  
    43  func TestPipelineMultilinePrint(t *testing.T) {
    44  	var err error
    45  	out := &bytes.Buffer{}
    46  	ctx := logger.WithLogger(context.Background(), logger.NewLogger(logger.InfoLvl, out))
    47  	ps := NewPipelineState(ctx, 1, fakeClock{})
    48  	ps.StartPipelineStep(ctx, "%s %s", "hello", "world")
    49  	ps.Printf(ctx, "line 1\nline 2\n")
    50  	ps.EndPipelineStep(ctx)
    51  	ps.End(ctx, err)
    52  
    53  	assertSnapshot(t, out.String())
    54  }
    55  
    56  func assertSnapshot(t *testing.T, output string) {
    57  	d1 := []byte(output)
    58  	gmPath := fmt.Sprintf("testdata/%s_master", t.Name())
    59  	if PipelineStateWriteGoldenMaster == "1" {
    60  		err := ioutil.WriteFile(gmPath, d1, 0644)
    61  		if err != nil {
    62  			t.Fatal(err)
    63  		}
    64  	}
    65  	expected, err := ioutil.ReadFile(gmPath)
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	if string(expected) != output {
    71  		t.Errorf("Expected: %s != Output: %s", expected, output)
    72  	}
    73  }