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

     1  package links
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/tilt-dev/tilt/internal/tiltfile/starkit"
    10  )
    11  
    12  func TestMaybeAddScheme(t *testing.T) {
    13  	cases := []struct {
    14  		name              string
    15  		url               string
    16  		expectErrContains string
    17  		expectURL         string
    18  	}{
    19  		{
    20  			name:      "preserves_scheme",
    21  			url:       "ws://www.zombo.com",
    22  			expectURL: "ws://www.zombo.com",
    23  		},
    24  		{
    25  			name:      "adds_http_if_no_scheme",
    26  			url:       "www.zombo.com",
    27  			expectURL: "http://www.zombo.com",
    28  		},
    29  		{
    30  			name:              "empty",
    31  			url:               "",
    32  			expectErrContains: "url empty",
    33  		},
    34  	}
    35  	for _, c := range cases {
    36  		t.Run(c.name, func(t *testing.T) {
    37  			actual, err := parseAndMaybeAddScheme(c.url)
    38  			if c.expectErrContains != "" {
    39  				require.Error(t, err, "expected error but got none")
    40  				require.Contains(t, err.Error(), c.expectErrContains, "error did not contain expected message")
    41  				return
    42  			}
    43  			require.NoError(t, err)
    44  			require.Equal(t, c.expectURL, actual.String(), "expected URL != actual URL")
    45  		})
    46  	}
    47  }
    48  
    49  func TestLinkProps(t *testing.T) {
    50  	f := starkit.NewFixture(t, NewPlugin())
    51  
    52  	f.File("Tiltfile", `
    53  l = link("localhost:4000", "web")
    54  print(l.url)
    55  print(l.name)
    56  `)
    57  
    58  	_, err := f.ExecFile("Tiltfile")
    59  	require.NoError(t, err)
    60  	assert.Equal(t, "localhost:4000\nweb\n", f.PrintOutput())
    61  }
    62  func TestLinkPropsImmutable(t *testing.T) {
    63  	f := starkit.NewFixture(t, NewPlugin())
    64  
    65  	f.File("Tiltfile", `
    66  l = link("localhost:4000", "web")
    67  l.url = "XXX"
    68  `)
    69  
    70  	_, err := f.ExecFile("Tiltfile")
    71  	require.Error(t, err)
    72  	assert.Contains(t, err.Error(), "can't assign to .url field of struct")
    73  }