github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/project/namespace_test.go (about)

     1  package project
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/ActiveState/cli/internal/environment"
    10  )
    11  
    12  func TestParseNamespace(t *testing.T) {
    13  	_, err := ParseNamespace("valid/namespace")
    14  	assert.NoError(t, err, "should parse a valid namespace")
    15  
    16  	_, err = ParseNamespace("valid/namespace#00000000-0000-0000-0000-000000000000")
    17  	assert.NoError(t, err, "should parse a valid namespace with 'uuid'")
    18  
    19  	_, err = ParseNamespace("valid/namespace#a10-b11c12-d13e14-f15")
    20  	assert.Error(t, err, "should fail to parse an invalid 'uuid'")
    21  
    22  	_, err = ParseNamespace("valid/namespace#")
    23  	assert.NoError(t, err, "should parse a valid namespace with empty uuid")
    24  }
    25  
    26  func TestParseNamespace_Invalid(t *testing.T) {
    27  	_, err := ParseNamespace("invalid-namespace")
    28  	assert.Error(t, err, "should get error with invalid namespace")
    29  
    30  	_, err = ParseNamespace("valid/namespace#invalidcommitid")
    31  	assert.Error(t, err, "should get error with valid namespace and invalid commit id (basic hex and dash filter)")
    32  }
    33  
    34  func TestParseProjectNoOwner(t *testing.T) {
    35  	parsed, err := ParseProjectNoOwner("project")
    36  	assert.NoError(t, err, "should be able to parse project part of namspace")
    37  	assert.Empty(t, parsed.Owner)
    38  	assert.Equal(t, parsed.Project, "project")
    39  	assert.Empty(t, parsed.CommitID)
    40  	assert.True(t, parsed.AllowOmitOwner)
    41  }
    42  
    43  func TestParseNamespaceOrConfigfile(t *testing.T) {
    44  	rootpath, err := environment.GetRootPath()
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	validConfigFile := filepath.Join(rootpath, "pkg", "projectfile", "testdata", "activestate.yaml")
    49  	invalidConfigFile := filepath.Join(rootpath, "activestate.yaml.nope")
    50  
    51  	var tests = []struct {
    52  		name       string
    53  		configFile string
    54  		expected   *Namespaced
    55  	}{
    56  		{"InvalidConfigfile", invalidConfigFile, nil},
    57  		{"FromConfigFile", validConfigFile, &Namespaced{Owner: "ActiveState", Project: "CodeIntel"}},
    58  	}
    59  
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  
    63  			ns := NameSpaceForConfig(tt.configFile)
    64  			assert.Equal(t, tt.expected, ns)
    65  		})
    66  	}
    67  }