github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/tiltfile/print/print_test.go (about)

     1  package print
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/tilt-dev/tilt/internal/tiltfile/starkit"
    13  	"github.com/tilt-dev/tilt/pkg/logger"
    14  )
    15  
    16  func TestWarn(t *testing.T) {
    17  	f := newFixture(t)
    18  
    19  	f.File("Tiltfile", "warn('problem 1')")
    20  	_, err := f.ExecFile("Tiltfile")
    21  	require.NoError(t, err)
    22  	assert.Contains(t, f.PrintOutput(), "problem 1")
    23  }
    24  
    25  func TestFail(t *testing.T) {
    26  	f := newFixture(t)
    27  	f.File("Tiltfile", "fail('problem 1')")
    28  	_, err := f.ExecFile("Tiltfile")
    29  	if assert.Error(t, err) {
    30  		assert.Contains(t, err.Error(), "problem 1")
    31  	}
    32  }
    33  
    34  func TestExitArgTypes(t *testing.T) {
    35  	type tc struct {
    36  		name        string
    37  		exitArg     string
    38  		expectedLog string
    39  	}
    40  
    41  	tcs := []tc{
    42  		{"Omitted", ``, ""},
    43  		{"String", `"goodbye"`, "goodbye"},
    44  		{"StringNamed", `code='ciao'`, "ciao"},
    45  		{"Int", `123`, "123"},
    46  		{"Dict", `dict(foo='bar', baz=123)`, `{"foo": "bar", "baz": 123}`},
    47  		{"None", `None`, ""},
    48  	}
    49  
    50  	for _, tc := range tcs {
    51  		t.Run(tc.name, func(t *testing.T) {
    52  			f := newFixture(t)
    53  
    54  			f.File(
    55  				"Tiltfile", fmt.Sprintf(`
    56  exit(%s)
    57  fail("this can't happen!")
    58  `, tc.exitArg))
    59  
    60  			_, err := f.ExecFile("Tiltfile")
    61  			require.NoError(t, err)
    62  			out := f.PrintOutput()
    63  			if tc.expectedLog == "" {
    64  				assert.Empty(t, out)
    65  			} else {
    66  				assert.Contains(t, out, tc.expectedLog)
    67  				assert.NotContains(t, out, "this can't happen!")
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func TestExitLoadedTiltfile(t *testing.T) {
    74  	f := newFixture(t)
    75  
    76  	f.File("exit.tiltfile", `exit("later alligator")`)
    77  
    78  	// loaded Tiltfile can force the root Tiltfile to halt execution
    79  	// i.e. it's more like `sys.exit(0)` than `return`
    80  	f.File(
    81  		"Tiltfile", `
    82  load("./exit.tiltfile", "this_symbol_does_not_exist")
    83  fail("this can't happen!")
    84  `)
    85  
    86  	_, err := f.ExecFile("Tiltfile")
    87  	require.NoError(t, err)
    88  	out := f.PrintOutput()
    89  	assert.Contains(t, out, "later alligator")
    90  	assert.NotContains(t, out, "this can't happen!")
    91  }
    92  
    93  func newFixture(tb testing.TB) *starkit.Fixture {
    94  	f := starkit.NewFixture(tb, NewPlugin())
    95  	out := bytes.NewBuffer(nil)
    96  	f.SetOutput(out)
    97  	log := logger.NewLogger(logger.VerboseLvl, out)
    98  	ctx := logger.WithLogger(context.Background(), log)
    99  	f.SetContext(ctx)
   100  	return f
   101  }