forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(golangBuild): BOM creation failed with private Go modules (SAP#4460)
* quickly try to only specify base private repo URLs with git config * fix the test * refactoring of private modules * test * fix test * fix url * typo * Adding gitConfiguration * typo * unit test * unit test --------- Co-authored-by: I557621 <[email protected]> Co-authored-by: aibaend1 <[email protected]> Co-authored-by: asadu <[email protected]>
- Loading branch information
Showing
2 changed files
with
75 additions
and
133 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -911,8 +911,7 @@ go 1.17` | |
expect: expectations{ | ||
envVars: []string{"GOPRIVATE=*.example.com"}, | ||
commandsExecuted: [][]string{ | ||
{"git", "config", "--global", "url.https://[email protected]/private/repo.git.insteadOf", "https://private1.example.com/private/repo.git"}, | ||
{"git", "config", "--global", "url.https://[email protected]/another/repo.git.insteadOf", "https://private2.example.com/another/repo.git"}, | ||
{"git", "config", "--global", "url.https://[email protected]", "https://example.com"}, | ||
}, | ||
}, | ||
}, | ||
|
@@ -943,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 | ||
}{ | ||
{ | ||
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) | ||
|
||
repos, err := lookupGolangPrivateModulesRepositories(goModFile, tt.globPattern, utils) | ||
|
||
if tt.expect.errorMessage == "" { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expect.repos, repos) | ||
} else { | ||
assert.EqualError(t, err, tt.expect.errorMessage) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRunGolangciLint(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
@@ -1153,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://[email protected]", "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://[email protected]", "https://example.com"}, | ||
{"git", "config", "--global", "url.https://[email protected]", "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) | ||
} | ||
} | ||
}) | ||
} | ||
} |