github.com/jfrog/build-info-go@v1.9.26/utils/pythonutils/piputils_test.go (about)

     1  package pythonutils
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/jfrog/build-info-go/tests"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestGetProjectNameFromFileContent(t *testing.T) {
    13  	tests := []struct {
    14  		fileContent         string
    15  		expectedProjectName string
    16  	}{
    17  		{"Metadata-Version: 1.0\nName: jfrog-python-example-1\nVersion: 1.0\nSummary: Project example for building Python project with JFrog products\nHome-page: https://github.com/jfrog/project-examples\nAuthor: JFrog\nAuthor-email: jfrog@jfrog.com\nLicense: UNKNOWN\nDescription: UNKNOWN\nPlatform: UNKNOWN", "jfrog-python-example-1:1.0"},
    18  		{"Metadata-Version: Name: jfrog-python-example-2\nLicense: UNKNOWN\nDescription: UNKNOWN\nPlatform: UNKNOWN\nName: jfrog-python-example-2\nVersion: 1.0\nSummary: Project example for building Python project with JFrog products\nHome-page: https://github.com/jfrog/project-examples\nAuthor: JFrog\nAuthor-email: jfrog@jfrog.com", "jfrog-python-example-2:1.0"},
    19  		{"Name:Metadata-Version: 3.0\nName: jfrog-python-example-3\nVersion: 1.0\nSummary: Project example for building Python project with JFrog products\nHome-page: https://github.com/jfrog/project-examples\nAuthor: JFrog\nAuthor-email: jfrog@jfrog.com\nName: jfrog-python-example-4", "jfrog-python-example-3:1.0"},
    20  	}
    21  
    22  	for _, test := range tests {
    23  		actualValue, err := getProjectIdFromFileContent([]byte(test.fileContent))
    24  		if err != nil {
    25  			t.Error(err)
    26  		}
    27  		if actualValue != test.expectedProjectName {
    28  			t.Errorf("Expected value: %s, got: %s.", test.expectedProjectName, actualValue)
    29  		}
    30  	}
    31  }
    32  
    33  var moduleNameTestProvider = []struct {
    34  	projectName         string
    35  	moduleName          string
    36  	expectedModuleName  string
    37  	expectedPackageName string
    38  }{
    39  	{"setuppyproject", "", "jfrog-python-example:1.0", "jfrog-python-example:1.0"},
    40  	{"setuppyproject", "overidden-module", "overidden-module", "jfrog-python-example:1.0"},
    41  	{"requirementsproject", "", "", ""},
    42  	{"requirementsproject", "overidden-module", "overidden-module", ""},
    43  }
    44  
    45  func TestDetermineModuleName(t *testing.T) {
    46  	for _, test := range moduleNameTestProvider {
    47  		t.Run(strings.Join([]string{test.projectName, test.moduleName}, "/"), func(t *testing.T) {
    48  			tmpProjectPath, cleanup := tests.CreateTestProject(t, filepath.Join("..", "testdata", "pip", test.projectName))
    49  			defer cleanup()
    50  
    51  			// Determine module name
    52  			packageName, err := getPackageNameFromSetuppy(tmpProjectPath)
    53  			if assert.NoError(t, err) {
    54  				assert.Equal(t, test.expectedPackageName, packageName)
    55  			}
    56  		})
    57  	}
    58  }