From 92c65f58f7009d1a19005388dd56182616f8bca1 Mon Sep 17 00:00:00 2001 From: I557621 Date: Tue, 11 Jul 2023 22:50:39 +0200 Subject: [PATCH 01/11] quickly try to only specify base private repo URLs with git config --- cmd/golangBuild.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/golangBuild.go b/cmd/golangBuild.go index 0b831a394e..0297d8bd6f 100644 --- a/cmd/golangBuild.go +++ b/cmd/golangBuild.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net/http" + "net/url" "os" "path" "path/filepath" @@ -349,13 +350,20 @@ func prepareGolangEnvironment(config *golangBuildOptions, goModFile *modfile.Fil // configure credentials git shall use for pulling repos for _, repoURL := range repoURLs { - if match, _ := regexp.MatchString("(?i)^https?://", repoURL); !match { + + parsedRepoURL, err := url.Parse(repoURL) + if err != nil { + return err + } + repoBaseURL := fmt.Sprintf("%s://%s", parsedRepoURL.Scheme, parsedRepoURL.Host) + + if match, _ := regexp.MatchString("(?i)^https?://", repoBaseURL); !match { continue } - authenticatedRepoURL := strings.Replace(repoURL, "://", fmt.Sprintf("://%s@", config.PrivateModulesGitToken), 1) + authenticatedRepoURL := strings.Replace(repoBaseURL, "://", fmt.Sprintf("://%s@", config.PrivateModulesGitToken), 1) - err = utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoURL) + err = utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoBaseURL) if err != nil { return err } From b7c1344ee6dc5d78d413fac13ac2a7e78ac927fe Mon Sep 17 00:00:00 2001 From: asadu Date: Tue, 25 Jul 2023 16:03:52 +0600 Subject: [PATCH 02/11] fix the test --- cmd/golangBuild_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/golangBuild_test.go b/cmd/golangBuild_test.go index f65145f352..b935175287 100644 --- a/cmd/golangBuild_test.go +++ b/cmd/golangBuild_test.go @@ -910,8 +910,8 @@ go 1.17` expect: expectations{ envVars: []string{"GOPRIVATE=*.example.com"}, commandsExecuted: [][]string{ - {"git", "config", "--global", "url.https://secret@private1.example.com/private/repo.git.insteadOf", "https://private1.example.com/private/repo.git"}, - {"git", "config", "--global", "url.https://secret@private2.example.com/another/repo.git.insteadOf", "https://private2.example.com/another/repo.git"}, + {"git", "config", "--global", "url.https://secret@private1.example.com.insteadOf", "https://private1.example.com"}, + {"git", "config", "--global", "url.https://secret@private2.example.com.insteadOf", "https://private2.example.com"}, }, }, }, From 29b3ffd0b6ad2befef46f8701670a1cd1c525aea Mon Sep 17 00:00:00 2001 From: asadu Date: Thu, 27 Jul 2023 15:45:31 +0600 Subject: [PATCH 03/11] refactoring of private modules --- cmd/golangBuild.go | 56 ++++++++++++++++++----------------------- cmd/golangBuild_test.go | 4 +-- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/cmd/golangBuild.go b/cmd/golangBuild.go index 0076ba10d4..185470aef6 100644 --- a/cmd/golangBuild.go +++ b/cmd/golangBuild.go @@ -342,33 +342,12 @@ func prepareGolangEnvironment(config *golangBuildOptions, goModFile *modfile.Fil // pass private repos to go process os.Setenv("GOPRIVATE", config.PrivateModules) - repoURLs, err := lookupGolangPrivateModulesRepositories(goModFile, config.PrivateModules, utils) + err = lookupGolangPrivateModulesRepositories(goModFile, config.PrivateModules, config.PrivateModulesGitToken, utils) if err != nil { return err } - // configure credentials git shall use for pulling repos - for _, repoURL := range repoURLs { - - parsedRepoURL, err := url.Parse(repoURL) - if err != nil { - return err - } - repoBaseURL := fmt.Sprintf("%s://%s", parsedRepoURL.Scheme, parsedRepoURL.Host) - - if match, _ := regexp.MatchString("(?i)^https?://", repoBaseURL); !match { - continue - } - - authenticatedRepoURL := strings.Replace(repoBaseURL, "://", fmt.Sprintf("://%s@", config.PrivateModulesGitToken), 1) - - err = utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoBaseURL) - if err != nil { - return err - } - } - return nil } @@ -548,33 +527,48 @@ func runGolangBuildPerArchitecture(config *golangBuildOptions, goModFile *modfil } // lookupPrivateModulesRepositories returns a slice of all modules that match the given glob pattern -func lookupGolangPrivateModulesRepositories(goModFile *modfile.File, globPattern string, utils golangBuildUtils) ([]string, error) { +func lookupGolangPrivateModulesRepositories(goModFile *modfile.File, globPattern string, token string, utils golangBuildUtils) error { if globPattern == "" { - return []string{}, nil + return nil } if goModFile == nil { - return nil, fmt.Errorf("couldn't find go.mod file") + return fmt.Errorf("couldn't find go.mod file") } else if goModFile.Require == nil { - return []string{}, nil // no modules referenced, nothing to do + return nil // no modules referenced, nothing to do } - privateModules := []string{} - for _, goModule := range goModFile.Require { if !module.MatchPrefixPatterns(globPattern, goModule.Mod.Path) { + continue } repo, err := utils.GetRepositoryURL(goModule.Mod.Path) if err != nil { - return nil, err + return err + } + + parsedRepoURL, err := url.Parse(repo) + if err != nil { + return err + } + repoBaseURL := fmt.Sprintf("%s://%s", parsedRepoURL.Scheme, parsedRepoURL.Host) + + if match, err := regexp.MatchString("(?i)^https?://", repoBaseURL); !match { + log.Entry().Infof("private repostiory %s doesn't match : %v", repoBaseURL, err) + } + + authenticatedRepoURL := strings.Replace(repoBaseURL, "://", fmt.Sprintf("://%s@", token), 1) + + err = utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoBaseURL) + if err != nil { + return err } - privateModules = append(privateModules, repo) } - return privateModules, nil + return nil } func runBOMCreation(utils golangBuildUtils, outputFilename string) error { diff --git a/cmd/golangBuild_test.go b/cmd/golangBuild_test.go index b935175287..93bd9efd31 100644 --- a/cmd/golangBuild_test.go +++ b/cmd/golangBuild_test.go @@ -965,6 +965,7 @@ go 1.17` modFileContent string globPattern string expect expectations + token string }{ { name: "Does nothing if glob pattern is empty", @@ -1013,11 +1014,10 @@ go 1.17` goModFile, _ := modfile.Parse("", []byte(tt.modFileContent), nil) - repos, err := lookupGolangPrivateModulesRepositories(goModFile, tt.globPattern, utils) + err := lookupGolangPrivateModulesRepositories(goModFile, tt.globPattern, tt.token, utils) if tt.expect.errorMessage == "" { assert.NoError(t, err) - assert.Equal(t, tt.expect.repos, repos) } else { assert.EqualError(t, err, tt.expect.errorMessage) } From 6a284d826f34150a6d38a6431070c6b3d1e8c6e0 Mon Sep 17 00:00:00 2001 From: asadu Date: Thu, 3 Aug 2023 15:36:44 +0600 Subject: [PATCH 04/11] test --- cmd/golangBuild.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/golangBuild.go b/cmd/golangBuild.go index c1c3eefed5..9247dab6f9 100644 --- a/cmd/golangBuild.go +++ b/cmd/golangBuild.go @@ -341,8 +341,14 @@ func prepareGolangEnvironment(config *golangBuildOptions, goModFile *modfile.Fil // pass private repos to go process os.Setenv("GOPRIVATE", config.PrivateModules) - - err = lookupGolangPrivateModulesRepositories(goModFile, config.PrivateModules, config.PrivateModulesGitToken, utils) + authenticatedRepoURL := fmt.Sprintf("%s@github.tools.sap", config.PrivateModulesGitToken) + repoBaseURL := " https://github.tools.sap" + //git config --global url.https://****@github.tools.sap.insteadOf https://github.tools.sap + err = utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoBaseURL) + if err != nil { + return err + } + //err = lookupGolangPrivateModulesRepositories(goModFile, config.PrivateModules, config.PrivateModulesGitToken, utils) if err != nil { return err From bbb770eb5808740b5c4e13b991ba4066c9ba5b64 Mon Sep 17 00:00:00 2001 From: asadu Date: Thu, 3 Aug 2023 17:03:39 +0600 Subject: [PATCH 05/11] fix test --- cmd/golangBuild_test.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/golangBuild_test.go b/cmd/golangBuild_test.go index 1338fc0524..5c4366d840 100644 --- a/cmd/golangBuild_test.go +++ b/cmd/golangBuild_test.go @@ -903,19 +903,19 @@ go 1.17` gitToken: "secret", expect: expectations{}, }, - { - name: "success - goprivate is set and authentication properly configured", - modFileContent: modTestFile, - globPattern: "*.example.com", - gitToken: "secret", - expect: expectations{ - envVars: []string{"GOPRIVATE=*.example.com"}, - commandsExecuted: [][]string{ - {"git", "config", "--global", "url.https://secret@private1.example.com.insteadOf", "https://private1.example.com"}, - {"git", "config", "--global", "url.https://secret@private2.example.com.insteadOf", "https://private2.example.com"}, - }, - }, - }, + //{ + // name: "success - goprivate is set and authentication properly configured", + // modFileContent: modTestFile, + // globPattern: "*.example.com", + // gitToken: "secret", + // expect: expectations{ + // envVars: []string{"GOPRIVATE=*.example.com"}, + // commandsExecuted: [][]string{ + // {"git", "config", "--global", "url.https://secret@private1.example.com.insteadOf", "https://private1.example.com"}, + // {"git", "config", "--global", "url.https://secret@private2.example.com.insteadOf", "https://private2.example.com"}, + // }, + // }, + //}, } for _, tt := range tests { From 3342a3b9f72fcedfea8866ea6ebc43ec27f9b67c Mon Sep 17 00:00:00 2001 From: asadu Date: Fri, 4 Aug 2023 13:32:53 +0600 Subject: [PATCH 06/11] fix url --- cmd/golangBuild.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/golangBuild.go b/cmd/golangBuild.go index 9247dab6f9..4bfbb91aa7 100644 --- a/cmd/golangBuild.go +++ b/cmd/golangBuild.go @@ -341,7 +341,7 @@ func prepareGolangEnvironment(config *golangBuildOptions, goModFile *modfile.Fil // pass private repos to go process os.Setenv("GOPRIVATE", config.PrivateModules) - authenticatedRepoURL := fmt.Sprintf("%s@github.tools.sap", config.PrivateModulesGitToken) + authenticatedRepoURL := fmt.Sprintf("https://%s@github.tools.sap", config.PrivateModulesGitToken) repoBaseURL := " https://github.tools.sap" //git config --global url.https://****@github.tools.sap.insteadOf https://github.tools.sap err = utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoBaseURL) From aaf9502b63c233d9ac3eb68704fd15140cad7bcd Mon Sep 17 00:00:00 2001 From: asadu Date: Fri, 4 Aug 2023 18:28:45 +0600 Subject: [PATCH 07/11] typo --- cmd/golangBuild.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/golangBuild.go b/cmd/golangBuild.go index 4bfbb91aa7..5d8ddebb7c 100644 --- a/cmd/golangBuild.go +++ b/cmd/golangBuild.go @@ -342,7 +342,7 @@ func prepareGolangEnvironment(config *golangBuildOptions, goModFile *modfile.Fil // pass private repos to go process os.Setenv("GOPRIVATE", config.PrivateModules) authenticatedRepoURL := fmt.Sprintf("https://%s@github.tools.sap", config.PrivateModulesGitToken) - repoBaseURL := " https://github.tools.sap" + repoBaseURL := "https://github.tools.sap" //git config --global url.https://****@github.tools.sap.insteadOf https://github.tools.sap err = utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoBaseURL) if err != nil { From d35488e3bdb5f2938958735ae4eac60363c2cb81 Mon Sep 17 00:00:00 2001 From: asadu Date: Mon, 7 Aug 2023 00:07:13 +0600 Subject: [PATCH 08/11] Adding gitConfiguration --- cmd/golangBuild.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/cmd/golangBuild.go b/cmd/golangBuild.go index 5d8ddebb7c..b28a81f53c 100644 --- a/cmd/golangBuild.go +++ b/cmd/golangBuild.go @@ -341,13 +341,8 @@ func prepareGolangEnvironment(config *golangBuildOptions, goModFile *modfile.Fil // pass private repos to go process os.Setenv("GOPRIVATE", config.PrivateModules) - authenticatedRepoURL := fmt.Sprintf("https://%s@github.tools.sap", config.PrivateModulesGitToken) - repoBaseURL := "https://github.tools.sap" - //git config --global url.https://****@github.tools.sap.insteadOf https://github.tools.sap - err = utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoBaseURL) - if err != nil { - return err - } + + err = gitConfigurationForPrivateModule(config.PrivateModules, config.PrivateModulesGitToken, utils) //err = lookupGolangPrivateModulesRepositories(goModFile, config.PrivateModules, config.PrivateModulesGitToken, utils) if err != nil { @@ -639,3 +634,19 @@ func isMainPackage(utils golangBuildUtils, pkg string) (bool, error) { return true, nil } + +func gitConfigurationForPrivateModule(privateMod string, token string, utils golangBuildUtils) error { + privateMod = strings.ReplaceAll(privateMod, "/*", "") + modules := strings.Split(privateMod, ",") + for _, v := range modules { + authenticatedRepoURL := fmt.Sprintf("https://%s@%s", token, v) + repoBaseURL := fmt.Sprintf("https://%s", v) + err := utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoBaseURL) + if err != nil { + return err + } + + } + + return nil +} From 7bc5c887e9f23aad66b784911759535a39fa89dc Mon Sep 17 00:00:00 2001 From: asadu Date: Tue, 8 Aug 2023 12:52:19 +0600 Subject: [PATCH 09/11] typo --- cmd/golangBuild.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/golangBuild.go b/cmd/golangBuild.go index b28a81f53c..3b50c876c5 100644 --- a/cmd/golangBuild.go +++ b/cmd/golangBuild.go @@ -342,8 +342,7 @@ func prepareGolangEnvironment(config *golangBuildOptions, goModFile *modfile.Fil // pass private repos to go process os.Setenv("GOPRIVATE", config.PrivateModules) - err = gitConfigurationForPrivateModule(config.PrivateModules, config.PrivateModulesGitToken, utils) - //err = lookupGolangPrivateModulesRepositories(goModFile, config.PrivateModules, config.PrivateModulesGitToken, utils) + err = gitConfigurationForPrivateModules(config.PrivateModules, config.PrivateModulesGitToken, utils) if err != nil { return err @@ -635,7 +634,7 @@ func isMainPackage(utils golangBuildUtils, pkg string) (bool, error) { return true, nil } -func gitConfigurationForPrivateModule(privateMod string, token string, utils golangBuildUtils) error { +func gitConfigurationForPrivateModules(privateMod string, token string, utils golangBuildUtils) error { privateMod = strings.ReplaceAll(privateMod, "/*", "") modules := strings.Split(privateMod, ",") for _, v := range modules { From 6c69aeb4c49a968ecaa6c80953b516ad6a434ea8 Mon Sep 17 00:00:00 2001 From: asadu Date: Tue, 8 Aug 2023 13:09:47 +0600 Subject: [PATCH 10/11] unit test --- cmd/golangBuild.go | 2 +- cmd/golangBuild_test.go | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/cmd/golangBuild.go b/cmd/golangBuild.go index 3b50c876c5..a8ec389b69 100644 --- a/cmd/golangBuild.go +++ b/cmd/golangBuild.go @@ -343,7 +343,6 @@ func prepareGolangEnvironment(config *golangBuildOptions, goModFile *modfile.Fil os.Setenv("GOPRIVATE", config.PrivateModules) err = gitConfigurationForPrivateModules(config.PrivateModules, config.PrivateModulesGitToken, utils) - if err != nil { return err } @@ -636,6 +635,7 @@ func isMainPackage(utils golangBuildUtils, pkg string) (bool, error) { func gitConfigurationForPrivateModules(privateMod string, token string, utils golangBuildUtils) error { privateMod = strings.ReplaceAll(privateMod, "/*", "") + privateMod = strings.ReplaceAll(privateMod, "*.", "") modules := strings.Split(privateMod, ",") for _, v := range modules { authenticatedRepoURL := fmt.Sprintf("https://%s@%s", token, v) diff --git a/cmd/golangBuild_test.go b/cmd/golangBuild_test.go index 5c4366d840..d7400ba7cd 100644 --- a/cmd/golangBuild_test.go +++ b/cmd/golangBuild_test.go @@ -903,19 +903,18 @@ go 1.17` gitToken: "secret", expect: expectations{}, }, - //{ - // name: "success - goprivate is set and authentication properly configured", - // modFileContent: modTestFile, - // globPattern: "*.example.com", - // gitToken: "secret", - // expect: expectations{ - // envVars: []string{"GOPRIVATE=*.example.com"}, - // commandsExecuted: [][]string{ - // {"git", "config", "--global", "url.https://secret@private1.example.com.insteadOf", "https://private1.example.com"}, - // {"git", "config", "--global", "url.https://secret@private2.example.com.insteadOf", "https://private2.example.com"}, - // }, - // }, - //}, + { + name: "success - goprivate is set and authentication properly configured", + modFileContent: modTestFile, + globPattern: "*.example.com", + gitToken: "secret", + expect: expectations{ + envVars: []string{"GOPRIVATE=*.example.com"}, + commandsExecuted: [][]string{ + {"git", "config", "--global", "url.https://secret@example.com.insteadOf", "https://example.com"}, + }, + }, + }, } for _, tt := range tests { From 127ef7799a8af47da6c3054fb8df728ec00c11ca Mon Sep 17 00:00:00 2001 From: asadu Date: Thu, 10 Aug 2023 14:55:02 +0600 Subject: [PATCH 11/11] unit test --- cmd/golangBuild.go | 48 -------------- cmd/golangBuild_test.go | 139 ++++++++++++++++------------------------ 2 files changed, 56 insertions(+), 131 deletions(-) diff --git a/cmd/golangBuild.go b/cmd/golangBuild.go index a8ec389b69..c746d356b2 100644 --- a/cmd/golangBuild.go +++ b/cmd/golangBuild.go @@ -4,11 +4,9 @@ import ( "bytes" "fmt" "net/http" - "net/url" "os" "path" "path/filepath" - "regexp" "strings" "github.com/SAP/jenkins-library/pkg/buildsettings" @@ -25,7 +23,6 @@ import ( "github.com/SAP/jenkins-library/pkg/versioning" "golang.org/x/mod/modfile" - "golang.org/x/mod/module" ) const ( @@ -525,51 +522,6 @@ func runGolangBuildPerArchitecture(config *golangBuildOptions, goModFile *modfil return binaryNames, nil } -// lookupPrivateModulesRepositories returns a slice of all modules that match the given glob pattern -func lookupGolangPrivateModulesRepositories(goModFile *modfile.File, globPattern string, token string, utils golangBuildUtils) error { - if globPattern == "" { - return nil - } - - if goModFile == nil { - return fmt.Errorf("couldn't find go.mod file") - } else if goModFile.Require == nil { - return nil // no modules referenced, nothing to do - } - - for _, goModule := range goModFile.Require { - if !module.MatchPrefixPatterns(globPattern, goModule.Mod.Path) { - - continue - } - - repo, err := utils.GetRepositoryURL(goModule.Mod.Path) - - if err != nil { - return err - } - - parsedRepoURL, err := url.Parse(repo) - if err != nil { - return err - } - repoBaseURL := fmt.Sprintf("%s://%s", parsedRepoURL.Scheme, parsedRepoURL.Host) - - if match, err := regexp.MatchString("(?i)^https?://", repoBaseURL); !match { - log.Entry().Infof("private repostiory %s doesn't match : %v", repoBaseURL, err) - } - - authenticatedRepoURL := strings.Replace(repoBaseURL, "://", fmt.Sprintf("://%s@", token), 1) - - err = utils.RunExecutable("git", "config", "--global", fmt.Sprintf("url.%s.insteadOf", authenticatedRepoURL), repoBaseURL) - if err != nil { - return err - } - - } - return nil -} - func runBOMCreation(utils golangBuildUtils, outputFilename string) error { if err := utils.RunExecutable("cyclonedx-gomod", "mod", "-licenses", fmt.Sprintf("-verbose=%t", GeneralConfig.Verbose), "-test", "-output", outputFilename, "-output-version", "1.4"); err != nil { diff --git a/cmd/golangBuild_test.go b/cmd/golangBuild_test.go index d7400ba7cd..93f2d4cb24 100644 --- a/cmd/golangBuild_test.go +++ b/cmd/golangBuild_test.go @@ -942,89 +942,6 @@ go 1.17` } } -func TestLookupGolangPrivateModulesRepositories(t *testing.T) { - t.Parallel() - - modTestFile := ` -module private.example.com/m - -require ( - example.com/public/module v1.0.0 - private1.example.com/private/repo v0.1.0 - private2.example.com/another/repo v0.2.0 -) - -go 1.17` - - type expectations struct { - repos []string - errorMessage string - } - tests := []struct { - name string - modFileContent string - globPattern string - expect expectations - token string - }{ - { - name: "Does nothing if glob pattern is empty", - modFileContent: modTestFile, - expect: expectations{ - repos: []string{}, - }, - }, - { - name: "Does nothing if there is no go.mod file", - globPattern: "private.example.com", - modFileContent: "", - expect: expectations{ - repos: []string{}, - }, - }, - { - name: "Detects all private repos using a glob pattern", - modFileContent: modTestFile, - globPattern: "*.example.com", - expect: expectations{ - repos: []string{"https://private1.example.com/private/repo.git", "https://private2.example.com/another/repo.git"}, - }, - }, - { - name: "Detects all private repos", - modFileContent: modTestFile, - globPattern: "private1.example.com,private2.example.com", - expect: expectations{ - repos: []string{"https://private1.example.com/private/repo.git", "https://private2.example.com/another/repo.git"}, - }, - }, - { - name: "Detects a dedicated repo", - modFileContent: modTestFile, - globPattern: "private2.example.com", - expect: expectations{ - repos: []string{"https://private2.example.com/another/repo.git"}, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - utils := newGolangBuildTestsUtils() - - goModFile, _ := modfile.Parse("", []byte(tt.modFileContent), nil) - - err := lookupGolangPrivateModulesRepositories(goModFile, tt.globPattern, tt.token, utils) - - if tt.expect.errorMessage == "" { - assert.NoError(t, err) - } else { - assert.EqualError(t, err, tt.expect.errorMessage) - } - }) - } -} - func TestRunGolangciLint(t *testing.T) { t.Parallel() @@ -1152,3 +1069,59 @@ func TestRetrieveGolangciLint(t *testing.T) { }) } } + +func Test_gitConfigurationForPrivateModules(t *testing.T) { + type args struct { + privateMod string + token string + } + type expectations struct { + commandsExecuted [][]string + } + tests := []struct { + name string + args args + + expected expectations + }{ + { + name: "with one private module", + args: args{ + privateMod: "example.com/*", + token: "mytoken", + }, + expected: expectations{ + commandsExecuted: [][]string{ + {"git", "config", "--global", "url.https://mytoken@example.com.insteadOf", "https://example.com"}, + }, + }, + }, + { + name: "with multiple private modules", + args: args{ + privateMod: "example.com/*,test.com/*", + token: "mytoken", + }, + expected: expectations{ + commandsExecuted: [][]string{ + {"git", "config", "--global", "url.https://mytoken@example.com.insteadOf", "https://example.com"}, + {"git", "config", "--global", "url.https://mytoken@test.com.insteadOf", "https://test.com"}, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + utils := newGolangBuildTestsUtils() + + err := gitConfigurationForPrivateModules(tt.args.privateMod, tt.args.token, utils) + + if assert.NoError(t, err) { + for i, expectedCommand := range tt.expected.commandsExecuted { + assert.Equal(t, expectedCommand[0], utils.Calls[i].Exec) + assert.Equal(t, expectedCommand[1:], utils.Calls[i].Params) + } + } + }) + } +}