Skip to content

Commit 4018e77

Browse files
yxxherodominykas
authored andcommitted
update resolver unittest for git
Signed-off-by: yxxhero <[email protected]> add license headers for gitutils Signed-off-by: yxxhero <[email protected]>
1 parent a709372 commit 4018e77

File tree

4 files changed

+91
-14
lines changed

4 files changed

+91
-14
lines changed

internal/resolver/resolver.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import (
2727
"github.com/Masterminds/semver/v3"
2828
"github.com/pkg/errors"
2929

30-
"github.com/Masterminds/vcs"
3130
"helm.sh/helm/v3/pkg/chart"
3231
"helm.sh/helm/v3/pkg/chart/loader"
3332
"helm.sh/helm/v3/pkg/gates"
33+
"helm.sh/helm/v3/pkg/gitutils"
3434
"helm.sh/helm/v3/pkg/helmpath"
3535
"helm.sh/helm/v3/pkg/provenance"
3636
"helm.sh/helm/v3/pkg/registry"
@@ -39,6 +39,8 @@ import (
3939

4040
const FeatureGateOCI = gates.Gate("HELM_EXPERIMENTAL_OCI")
4141

42+
var hasGitReference = gitutils.HasGitReference
43+
4244
// Resolver resolves dependencies from semantic version ranges to a particular version.
4345
type Resolver struct {
4446
chartpath string
@@ -113,18 +115,12 @@ func (r *Resolver) Resolve(reqs []*chart.Dependency, repoNames map[string]string
113115

114116
if strings.HasPrefix(d.Repository, "git://") {
115117

116-
local, err := os.MkdirTemp("", d.Name)
117-
if err != nil {
118-
return nil, err
119-
}
120-
repo, err := vcs.NewRepo(strings.TrimPrefix(d.Repository, "git://"), local)
118+
found, err := hasGitReference(strings.TrimPrefix(d.Repository, "git://"), d.Version, d.Name)
121119

122120
if err != nil {
123121
return nil, err
124122
}
125123

126-
found := repo.IsReference(d.Version)
127-
128124
if !found {
129125
return nil, fmt.Errorf(`dependency %q is missing git branch or tag: %s.
130126
When using a "git://" type repository, the "version" should be a valid branch or tag name`, d.Name, d.Version)

internal/resolver/resolver_test.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@ import (
2323
"helm.sh/helm/v3/pkg/registry"
2424
)
2525

26+
func fakeGitReference(gitRepo, ref, repoName string) (bool, error) {
27+
gitRefs := map[string]string{
28+
"1.0.0": "",
29+
}
30+
31+
_, found := gitRefs[ref]
32+
return found, nil
33+
}
2634
func TestResolve(t *testing.T) {
35+
hasGitReference = fakeGitReference
2736
tests := []struct {
2837
name string
2938
req []*chart.Dependency
@@ -138,25 +147,49 @@ func TestResolve(t *testing.T) {
138147
err: true,
139148
},
140149
{
141-
name: "repo from git ssh url",
150+
name: "repo from git https url",
142151
req: []*chart.Dependency{
143-
{Name: "gitdependency", Repository: "git://git@github.com:helm/gitdependency.git", Version: "1.0.0"},
152+
{Name: "gitdependencyok", Repository: "git://https://github.com/helm/helmchart.git", Version: "1.0.0"},
144153
},
145154
expect: &chart.Lock{
146155
Dependencies: []*chart.Dependency{
147-
{Name: "gitdependency", Repository: "git://git@github.com:helm/gitdependency.git", Version: "1.0.0"},
156+
{Name: "gitdependencyok", Repository: "git://https://github.com/helm/helmchart.git", Version: "1.0.0"},
148157
},
149158
},
150-
err: true,
159+
err: false,
151160
},
152161
{
153162
name: "repo from git https url",
154163
req: []*chart.Dependency{
155-
{Name: "gitdependency", Repository: "git://https://github.com/helm/gitdependency.git", Version: "1.0.0"},
164+
{Name: "gitdependencyerror", Repository: "git://https://github.com/helm/helmchart.git", Version: "2.0.0"},
165+
},
166+
expect: &chart.Lock{
167+
Dependencies: []*chart.Dependency{
168+
{Name: "gitdependencyerror", Repository: "git://https://github.com/helm/helmchart.git", Version: "2.0.0"},
169+
},
170+
},
171+
err: true,
172+
},
173+
{
174+
name: "repo from git ssh url",
175+
req: []*chart.Dependency{
176+
{Name: "gitdependency", Repository: "git://[email protected]:helm/helmchart.git", Version: "1.0.0"},
177+
},
178+
expect: &chart.Lock{
179+
Dependencies: []*chart.Dependency{
180+
{Name: "gitdependency", Repository: "git://[email protected]:helm/helmchart.git", Version: "1.0.0"},
181+
},
182+
},
183+
err: false,
184+
},
185+
{
186+
name: "repo from git ssh url",
187+
req: []*chart.Dependency{
188+
{Name: "gitdependencyerror", Repository: "git://[email protected]:helm/helmchart.git", Version: "2.0.0"},
156189
},
157190
expect: &chart.Lock{
158191
Dependencies: []*chart.Dependency{
159-
{Name: "gitdependency", Repository: "git://https://github.com/helm/gitdependency.git", Version: "1.0.0"},
192+
{Name: "gitdependencyerror", Repository: "git://git@github.com:helm/helmchart.git", Version: "2.0.0"},
160193
},
161194
},
162195
err: true,

pkg/getter/gitgetter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"path/filepath"
2525

2626
"github.com/Masterminds/vcs"
27+
2728
"helm.sh/helm/v3/internal/fileutil"
2829
)
2930

pkg/gitutils/gitutils.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright The Helm Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/*Package engine implements the Go text template engine as needed for Helm.
18+
19+
When Helm renders templates it does so with additional functions and different
20+
modes (e.g., strict, lint mode). This package handles the helm specific
21+
implementation.
22+
*/
23+
package gitutils
24+
25+
import (
26+
"os"
27+
28+
"github.com/Masterminds/vcs"
29+
)
30+
31+
func HasGitReference(gitRepo, ref, repoName string) (bool, error) {
32+
local, err := os.MkdirTemp("", repoName)
33+
if err != nil {
34+
return false, err
35+
}
36+
repo, err := vcs.NewRepo(gitRepo, local)
37+
38+
if err != nil {
39+
return false, err
40+
}
41+
42+
if err := repo.Get(); err != nil {
43+
return false, err
44+
}
45+
defer os.RemoveAll(local)
46+
return repo.IsReference(ref), nil
47+
}

0 commit comments

Comments
 (0)