github.com/jfrog/jfrog-cli-core/v2@v2.52.0/artifactory/commands/golang/go_test.go (about)

     1  package golang
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
     6  	goutils "github.com/jfrog/jfrog-cli-core/v2/utils/golang"
     7  	testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
     8  	"github.com/stretchr/testify/assert"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestBuildPackageVersionRequest(t *testing.T) {
    16  	tests := []struct {
    17  		packageName     string
    18  		branchName      string
    19  		expectedRequest string
    20  	}{
    21  		{"github.com/jfrog/jfrog-cli", "", "github.com/jfrog/jfrog-cli/@v/latest.info"},
    22  		{"github.com/jfrog/jfrog-cli", "dev", "github.com/jfrog/jfrog-cli/@v/dev.info"},
    23  		{"github.com/jfrog/jfrog-cli", "v1.0.7", "github.com/jfrog/jfrog-cli/@v/v1.0.7.info"},
    24  	}
    25  	for _, test := range tests {
    26  		t.Run(test.expectedRequest, func(t *testing.T) {
    27  			versionRequest := buildPackageVersionRequest(test.packageName, test.branchName)
    28  			if versionRequest != test.expectedRequest {
    29  				t.Error("Failed to build package version request. The version request is", versionRequest, " but it is expected to be", test.expectedRequest)
    30  			}
    31  		})
    32  	}
    33  }
    34  
    35  func TestGetPackageFilesPath(t *testing.T) {
    36  	packageCachePath, err := goutils.GetGoModCachePath()
    37  	assert.NoError(t, err)
    38  	packageName := "github.com/golang/mock/mockgen"
    39  	version := "v1.4.1"
    40  	expectedPackagePath := filepath.Join(packageCachePath, "github.com/golang/mock@"+version)
    41  	err = os.MkdirAll(expectedPackagePath, os.ModePerm)
    42  	assert.NoError(t, err)
    43  	defer testsutils.RemoveAllAndAssert(t, expectedPackagePath)
    44  	actualPackagePath, err := getFileSystemPackagePath(packageCachePath, packageName, version)
    45  	assert.NoError(t, err)
    46  	assert.Equal(t, expectedPackagePath, actualPackagePath)
    47  }
    48  
    49  func TestSetArtifactoryAsResolutionServer(t *testing.T) {
    50  	server := &config.ServerDetails{
    51  		Url:            "http://localhost:8080/",
    52  		ArtifactoryUrl: "http://localhost:8080/artifactory/",
    53  		User:           "myUser",
    54  		Password:       "myPassword",
    55  		ServerId:       "myServer",
    56  	}
    57  	repo := "myRepo"
    58  
    59  	// Setting the GOPROXY value to "" to ensure that the new value set in SetArtifactoryAsResolutionServer is correctly validated.
    60  	cleanup := testsutils.SetEnvWithCallbackAndAssert(t, "GOPROXY", "")
    61  	defer cleanup()
    62  
    63  	assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo))
    64  
    65  	serverUrlWithoutHttp := strings.TrimPrefix(server.ArtifactoryUrl, "http://")
    66  	expectedGoProxy := fmt.Sprintf("http://%s:%s@%sapi/go/%s|direct", server.User, server.Password, serverUrlWithoutHttp, repo)
    67  	assert.Equal(t, expectedGoProxy, os.Getenv("GOPROXY"))
    68  }