-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean code, remove redundant, and unite common build tools logic (#1287)
- Loading branch information
1 parent
85df769
commit 59ac976
Showing
36 changed files
with
572 additions
and
1,008 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,14 @@ package golang | |
|
||
import ( | ||
"fmt" | ||
biutils "github.com/jfrog/build-info-go/utils" | ||
"github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" | ||
goutils "github.com/jfrog/jfrog-cli-core/v2/utils/golang" | ||
"github.com/jfrog/jfrog-client-go/artifactory/auth" | ||
testsutils "github.com/jfrog/jfrog-client-go/utils/tests" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
@@ -34,7 +37,7 @@ func TestBuildPackageVersionRequest(t *testing.T) { | |
} | ||
|
||
func TestGetPackageFilesPath(t *testing.T) { | ||
packageCachePath, err := goutils.GetGoModCachePath() | ||
packageCachePath, err := biutils.GetGoModCachePath() | ||
assert.NoError(t, err) | ||
packageName := "github.com/golang/mock/mockgen" | ||
version := "v1.4.1" | ||
|
@@ -61,7 +64,7 @@ func TestSetArtifactoryAsResolutionServer(t *testing.T) { | |
cleanup := testsutils.SetEnvWithCallbackAndAssert(t, "GOPROXY", "") | ||
defer cleanup() | ||
|
||
assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo, goutils.GoProxyUrlParams{Direct: true})) | ||
assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo, GoProxyUrlParams{Direct: true})) | ||
|
||
serverUrlWithoutHttp := strings.TrimPrefix(server.ArtifactoryUrl, "http://") | ||
expectedGoProxy := fmt.Sprintf("http://%s:%s@%sapi/go/%s|direct", server.User, server.Password, serverUrlWithoutHttp, repo) | ||
|
@@ -70,9 +73,95 @@ func TestSetArtifactoryAsResolutionServer(t *testing.T) { | |
// Verify that the EndpointPrefix value is correctly added to the GOPROXY. | ||
// In this test case, the endpoint prefix is set to api/curation/audit/. | ||
// This parameter allows downloading dependencies from a custom API instead of the default one. | ||
assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo, goutils.GoProxyUrlParams{Direct: true, EndpointPrefix: coreutils.CurationPassThroughApi})) | ||
assert.NoError(t, SetArtifactoryAsResolutionServer(server, repo, GoProxyUrlParams{Direct: true, EndpointPrefix: coreutils.CurationPassThroughApi})) | ||
|
||
serverUrlWithoutHttp = strings.TrimPrefix(server.ArtifactoryUrl, "http://") | ||
expectedGoProxy = fmt.Sprintf("http://%s:%s@%sapi/curation/audit/api/go/%s|direct", server.User, server.Password, serverUrlWithoutHttp, repo) | ||
assert.Equal(t, expectedGoProxy, os.Getenv("GOPROXY")) | ||
} | ||
|
||
func TestGetArtifactoryRemoteRepoUrl(t *testing.T) { | ||
server := &config.ServerDetails{ | ||
ArtifactoryUrl: "https://server.com/artifactory", | ||
AccessToken: "eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA", | ||
} | ||
repoName := "test-repo" | ||
repoUrl, err := getArtifactoryRemoteRepoUrl(server, repoName, GoProxyUrlParams{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "https://test:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@server.com/artifactory/api/go/test-repo", repoUrl) | ||
} | ||
|
||
func TestGetArtifactoryApiUrl(t *testing.T) { | ||
details := auth.NewArtifactoryDetails() | ||
details.SetUrl("https://test.com/artifactory/") | ||
|
||
// Test username and password | ||
details.SetUser("frog") | ||
details.SetPassword("passfrog") | ||
url, err := getArtifactoryApiUrl("test-repo", details, GoProxyUrlParams{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "https://frog:[email protected]/artifactory/api/go/test-repo", url) | ||
|
||
// Test username and password with EndpointPrefix and direct | ||
details.SetUser("frog") | ||
details.SetPassword("passfrog") | ||
url, err = getArtifactoryApiUrl("test-repo", details, GoProxyUrlParams{EndpointPrefix: "test", Direct: true}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "https://frog:[email protected]/artifactory/test/api/go/test-repo|direct", url) | ||
|
||
// Test access token | ||
// Set fake access token with username "test" | ||
details.SetUser("") | ||
details.SetAccessToken("eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA") | ||
url, err = getArtifactoryApiUrl("test-repo", details, GoProxyUrlParams{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "https://test:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@test.com/artifactory/api/go/test-repo", url) | ||
|
||
// Test access token with username | ||
// Set fake access token with username "test" | ||
// Expect username to be "frog" | ||
details.SetUser("frog") | ||
details.SetAccessToken("eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA") | ||
url, err = getArtifactoryApiUrl("test-repo", details, GoProxyUrlParams{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "https://frog:eyJ0eXAiOiJKV1QifQ.eyJzdWIiOiJmYWtlXC91c2Vyc1wvdGVzdCJ9.MTIzNDU2Nzg5MA@test.com/artifactory/api/go/test-repo", url) | ||
} | ||
|
||
func TestGoProxyUrlParams_BuildUrl(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
RepoName string | ||
Direct bool | ||
EndpointPrefix string | ||
ExpectedUrl string | ||
}{ | ||
{ | ||
name: "Url Without direct or Prefix", | ||
RepoName: "go", | ||
ExpectedUrl: "https://test/api/go/go", | ||
}, | ||
{ | ||
name: "Url With direct", | ||
RepoName: "go", | ||
Direct: true, | ||
ExpectedUrl: "https://test/api/go/go|direct", | ||
}, | ||
{ | ||
name: "Url With Prefix", | ||
RepoName: "go", | ||
EndpointPrefix: "prefix", | ||
ExpectedUrl: "https://test/prefix/api/go/go", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
remoteUrl, err := url.Parse("https://test") | ||
require.NoError(t, err) | ||
gdu := &GoProxyUrlParams{ | ||
Direct: tt.Direct, | ||
EndpointPrefix: tt.EndpointPrefix, | ||
} | ||
assert.Equalf(t, tt.ExpectedUrl, gdu.BuildUrl(remoteUrl, tt.RepoName), "BuildUrl(%v, %v)", remoteUrl, tt.RepoName) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.