github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/tiltfile/loaddynamic/load_test.go (about) 1 package loaddynamic 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 "go.starlark.net/starlark" 10 11 "github.com/tilt-dev/tilt/internal/tiltfile/starkit" 12 ) 13 14 func TestLoadDynamicOK(t *testing.T) { 15 f := NewFixture(t) 16 17 f.File("Tiltfile", ` 18 stuff = load_dynamic('./foo/Tiltfile') 19 print('stuff.x=' + str(stuff['x'])) 20 `) 21 f.File("foo/Tiltfile", ` 22 x = 1 23 print('x=' + str(x)) 24 `) 25 26 _, err := f.ExecFile("Tiltfile") 27 require.NoError(t, err) 28 assert.Equal(t, "x=1\nstuff.x=1\n", f.PrintOutput()) 29 } 30 31 func TestLoadDynamicCachedLoad(t *testing.T) { 32 f := NewFixture(t) 33 34 f.File("Tiltfile", ` 35 stuff1 = load_dynamic('./foo/Tiltfile') 36 print('stuff1.x=' + str(stuff1['x'])) 37 stuff2 = load_dynamic('./foo/Tiltfile') 38 print('stuff2.x=' + str(stuff2['x'])) 39 `) 40 f.File("foo/Tiltfile", ` 41 x = 1 42 print('x=' + str(x)) 43 `) 44 45 _, err := f.ExecFile("Tiltfile") 46 require.NoError(t, err) 47 assert.Equal(t, "x=1\nstuff1.x=1\nstuff2.x=1\n", f.PrintOutput()) 48 } 49 50 func TestLoadDynamicFrozen(t *testing.T) { 51 f := NewFixture(t) 52 53 f.File("Tiltfile", ` 54 stuff = load_dynamic('./foo/Tiltfile') 55 stuff['x'] = 2 56 `) 57 f.File("foo/Tiltfile", ` 58 x = 1 59 print('x=' + str(x)) 60 `) 61 62 _, err := f.ExecFile("Tiltfile") 63 if assert.Error(t, err) { 64 backtrace := err.(*starlark.EvalError).Backtrace() 65 assert.Contains(t, backtrace, fmt.Sprintf("%s:3:6: in <toplevel>", f.JoinPath("Tiltfile"))) 66 assert.Contains(t, backtrace, "cannot insert into frozen hash table") 67 } 68 } 69 70 func TestLoadDynamicError(t *testing.T) { 71 f := NewFixture(t) 72 73 f.File("Tiltfile", ` 74 load_dynamic('./foo/Tiltfile') 75 `) 76 f.File("foo/Tiltfile", ` 77 x = 1 78 y = x // 0 79 `) 80 81 _, err := f.ExecFile("Tiltfile") 82 if assert.Error(t, err) { 83 backtrace := err.(*starlark.EvalError).Backtrace() 84 assert.Contains(t, backtrace, fmt.Sprintf("%s:2:13: in <toplevel>", f.JoinPath("Tiltfile"))) 85 assert.Contains(t, backtrace, fmt.Sprintf("%s:3:7: in <toplevel>", f.JoinPath("foo", "Tiltfile"))) 86 } 87 } 88 89 func NewFixture(tb testing.TB) *starkit.Fixture { 90 return starkit.NewFixture(tb, &LoadDynamicFn{}) 91 }