github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/format/common/spdxhelpers/document_namespace_test.go (about)

     1  package spdxhelpers
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/anchore/syft/syft/sbom"
    11  	"github.com/anchore/syft/syft/source"
    12  	"github.com/lineaje-labs/syft/syft/internal/sourcemetadata"
    13  )
    14  
    15  func Test_documentNamespace(t *testing.T) {
    16  	tracker := sourcemetadata.NewCompletionTester(t)
    17  
    18  	tests := []struct {
    19  		name      string
    20  		inputName string
    21  		src       source.Description
    22  		expected  string
    23  	}{
    24  		{
    25  			name:      "image",
    26  			inputName: "my-name",
    27  			src: source.Description{
    28  				Metadata: source.StereoscopeImageSourceMetadata{
    29  					UserInput:      "image-repo/name:tag",
    30  					ID:             "id",
    31  					ManifestDigest: "digest",
    32  				},
    33  			},
    34  			expected: "https://anchore.com/syft/image/my-name-",
    35  		},
    36  		{
    37  			name:      "directory",
    38  			inputName: "my-name",
    39  			src: source.Description{
    40  				Metadata: source.DirectorySourceMetadata{
    41  					Path: "some/path/to/place",
    42  				},
    43  			},
    44  			expected: "https://anchore.com/syft/dir/my-name-",
    45  		},
    46  		{
    47  			name:      "file",
    48  			inputName: "my-name",
    49  			src: source.Description{
    50  				Metadata: source.FileSourceMetadata{
    51  					Path: "some/path/to/place",
    52  				},
    53  			},
    54  			expected: "https://anchore.com/syft/file/my-name-",
    55  		},
    56  	}
    57  	for _, test := range tests {
    58  		t.Run(test.name, func(t *testing.T) {
    59  			actual := DocumentNamespace(test.inputName, test.src, sbom.Descriptor{
    60  				Name: "syft",
    61  			})
    62  
    63  			// note: since the namespace ends with a UUID we check the prefix
    64  			assert.True(t, strings.HasPrefix(actual, test.expected), fmt.Sprintf("expected prefix: '%s' got: '%s'", test.expected, actual))
    65  
    66  			// track each scheme tested (passed or not)
    67  			tracker.Tested(t, test.src.Metadata)
    68  		})
    69  	}
    70  }