Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
walkowif committed Jan 8, 2024
1 parent c1ce137 commit 8f0a9de
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 8 deletions.
18 changes: 10 additions & 8 deletions cmd/renv.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,9 @@ func GetDefaultBranchSha(gitDirectory string, repoURL string,
// UpdateGitPackages iterates through the packages in renv.lock and updates the entries
// corresponding to packages stored in git repositories. Package version and latest commit SHA
// are updated in the renvLock struct. Only packages matching the updatePackageRegexp are updated.
func UpdateGitPackages(renvLock *RenvLock, updatePackageRegexp string) {
gitUpdatesDirectory := localTempDirectory + "/git_updates/"
err := os.RemoveAll(gitUpdatesDirectory)
checkError(err)
err = os.MkdirAll(gitUpdatesDirectory, os.ModePerm)
checkError(err)
func UpdateGitPackages(renvLock *RenvLock, updatePackageRegexp string,
getDefaultBranchShaFunction func(string, string, string) string,
gitUpdatesDirectory string) {
for k, v := range renvLock.Packages {
match, err := regexp.MatchString(updatePackageRegexp, k)
checkError(err)
Expand All @@ -183,7 +180,7 @@ func UpdateGitPackages(renvLock *RenvLock, updatePackageRegexp string) {
}
log.Trace("Package ", k, " matches updated packages regexp ",
updatePackageRegexp)
newPackageSha := GetDefaultBranchSha(
newPackageSha := getDefaultBranchShaFunction(
gitUpdatesDirectory+k, GetGitRepositoryURL(v), v.Source,
)
// Read newest package version from DESCRIPTION.
Expand Down Expand Up @@ -300,7 +297,12 @@ func UpdateRenvLock(inputFileName, updatePackages string) RenvLock {
checkError(err)

updatePackageRegex := GetPackageRegex(updatePackages)
UpdateGitPackages(&renvLock, updatePackageRegex)
gitUpdatesDirectory := localTempDirectory + "/git_updates/"
err = os.RemoveAll(gitUpdatesDirectory)
checkError(err)
err = os.MkdirAll(gitUpdatesDirectory, os.ModePerm)
checkError(err)
UpdateGitPackages(&renvLock, updatePackageRegex, GetDefaultBranchSha, gitUpdatesDirectory)
repositoryPackagesFiles := GetPackagesFiles(renvLock)
UpdateRepositoryPackages(&renvLock, updatePackageRegex, repositoryPackagesFiles)
return renvLock
Expand Down
82 changes: 82 additions & 0 deletions cmd/renv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,85 @@ func Test_GetGitRepositoryURL(t *testing.T) {
})
assert.Equal(t, repoURL3, "https://gitlab.example.com/org3/org4/repo-name-3")
}

func mockedGetDefaultBranchSha(_ string, repoURL string, _ string) string {
switch {
case repoURL == "https://github.com/group1/group2/package11":
return "eee111555bbbccc"
case repoURL == "https://gitlab.example.com/group3/group4/package12":
return "888444dddbbbaaa"
}
return ""
}

func Test_UpdateGitPackages(t *testing.T) {
renvLock := RenvLock{
RenvLockContents{
[]RenvLockRepository{
{"Repo1", "https://repo1.example.com/repo1"},
},
},
map[string]PackageDescription{
"package11": {
"package11",
"1.0.2",
"GitHub",
"",
[]Dependency{},
"github",
"api.github.com",
"group1/group2",
"package11",
"subdirectory1",
"main",
"aaabbb444333",
[]string{},
},
"package12": {
"package12",
"2.5.4.3",
"GitLab",
"",
[]Dependency{},
"gitlab",
"https://gitlab.example.com",
"group3/group4",
"package12",
"subdirectory2",
"v2.5.4.3",
"eee888222aaa",
[]string{},
},
"package3": {
"package3",
"3.2.7.8",
"Repository",
"Repo1",
[]Dependency{},
"", "", "", "", "", "", "", []string{},
},
"package4": {
"package4",
"3.7.0",
"GitLab",
"",
[]Dependency{},
"gitlab",
"https://gitlab.example.com",
"group6/group7",
"package4",
"",
"v3.7.0",
"ccceee444999",
[]string{},
},
},
}
UpdateGitPackages(&renvLock, "package1*", mockedGetDefaultBranchSha, "testdata/git_updates/")
assert.Equal(t, renvLock.Packages["package11"].Version, "1.0.4")
assert.Equal(t, renvLock.Packages["package12"].Version, "2.6.1.1")
assert.Equal(t, renvLock.Packages["package4"].Version, "3.7.0")
assert.Equal(t, renvLock.Packages["package11"].RemoteSha, "eee111555bbbccc")
assert.Equal(t, renvLock.Packages["package12"].RemoteSha, "888444dddbbbaaa")
assert.Equal(t, renvLock.Packages["package4"].RemoteSha, "ccceee444999")
}
2 changes: 2 additions & 0 deletions cmd/testdata/git_updates/package11/subdirectory1/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: package11
Version: 1.0.4
2 changes: 2 additions & 0 deletions cmd/testdata/git_updates/package12/subdirectory2/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: package12
Version: 2.6.1.1

0 comments on commit 8f0a9de

Please sign in to comment.