github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/pkg/tiltextension/name_test.go (about)

     1  package tiltextension
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  var validNameTests = []struct {
    11  	in          string
    12  	valid       bool
    13  	errContains string
    14  }{
    15  	// valid
    16  	{"test", true, ""},
    17  	{"some-extension", true, ""},
    18  	{"example.com", true, ""},
    19  	{"under_score", true, ""},
    20  	{"test.go", true, ""},
    21  	{"123numeric", true, ""},
    22  	// invalid
    23  	{"crazy!", false, `name can only contain URL-friendly characters`},
    24  	{"", false, "name length must be greater than zero"},
    25  	{".start-with-period", false, "name cannot start with a period"},
    26  	{"_start-with-underscore", false, "name cannot start with an underscore"},
    27  	{"contain:colons", false, "name cannot contain `:`"},
    28  	{" leading-space", false, "name cannot contain leading or trailing spaces"},
    29  	{"trailing-space ", false, "name cannot contain leading or trailing spaces"},
    30  	{"s/l/a/s/h/e/s", false, "name can only contain URL-friendly characters"},
    31  	{"tilt_modules", false, "tilt_modules is a banned name"},
    32  	{"Tiltfile", false, "Tiltfile is a banned name"},
    33  	{strings.Repeat("long", 200), false, "name cannot contain more than 214 characters"},
    34  }
    35  
    36  func TestValidateName(t *testing.T) {
    37  	for _, tt := range validNameTests {
    38  		t.Run(tt.in, func(t *testing.T) {
    39  			result := ValidateName(tt.in)
    40  			if tt.valid && result != nil {
    41  				t.Errorf("Expected valid, got %v", result)
    42  			} else if !tt.valid && result == nil {
    43  				t.Errorf("Expected invalid, got valid")
    44  			} else if !tt.valid && result != nil {
    45  				assert.Contains(t, result.Error(), tt.errContains)
    46  			}
    47  		})
    48  	}
    49  }