Repository: grafana/tanka
Pull request #641 (2021-11-09 15:37:40)

match Add() and AddRepos() and correct typos
Supersedes https://github.com/grafana/tanka/pull/640 (adding unit tests)

redradrat: @julienduchesne thx for helping out my lazy ass with the tests here ;) 

Patch:
diff --git a/pkg/helm/charts.go b/pkg/helm/charts.go
index 92b660e3..5a007532 100644
--- a/pkg/helm/charts.go
+++ b/pkg/helm/charts.go
@@ -147,10 +147,6 @@ func (c Charts) Vendor() error {
 func (c *Charts) Add(reqs []string) error {
 	log.Printf("Adding %v Charts ...", len(reqs))
 
-	skip := func(s string, err error) {
-		log.Printf(" Skipping %s: %s.", s, err)
-	}
-
 	// parse new charts, append in memory
 	added := 0
 	for _, s := range reqs {
@@ -177,7 +173,7 @@ func (c *Charts) Add(reqs []string) error {
 
 	// skipped some? fail then
 	if added != len(reqs) {
-		return fmt.Errorf("%v Charts were skipped. Please check above logs for details", len(reqs)-added)
+		return fmt.Errorf("%v Chart(s) were skipped. Please check above logs for details", len(reqs)-added)
 	}
 
 	// worked fine? vendor it
@@ -189,11 +185,13 @@ func (c *Charts) AddRepos(repos ...Repo) error {
 	added := 0
 	for _, r := range repos {
 		if c.Manifest.Repositories.Has(r) {
-			log.Printf("Skipping %s: Already exists", r.Name)
+			skip(r.Name, fmt.Errorf("already exists"))
 			continue
 		}
 
 		c.Manifest.Repositories = append(c.Manifest.Repositories, r)
+		added++
+		log.Println(" OK:", r.Name)
 	}
 
 	// write out
@@ -202,7 +200,7 @@ func (c *Charts) AddRepos(repos ...Repo) error {
 	}
 
 	if added != len(repos) {
-		return fmt.Errorf("%v Chart(s) were skipped. Please check above logs for details", len(repos)-added)
+		return fmt.Errorf("%v Repo(s) were skipped. Please check above logs for details", len(repos)-added)
 	}
 
 	return nil
@@ -264,3 +262,7 @@ func parseReqName(s string) string {
 	name := elems[1]
 	return name
 }
+
+func skip(s string, err error) {
+	log.Printf(" Skipping %s: %s.", s, err)
+}


Test patch:
diff --git a/pkg/helm/charts_test.go b/pkg/helm/charts_test.go
new file mode 100644
index 00000000..c2c43a0c
--- /dev/null
+++ b/pkg/helm/charts_test.go
@@ -0,0 +1,57 @@
+package helm
+
+import (
+	"path/filepath"
+	"testing"
+
+	"github.com/grafana/tanka/pkg/kubernetes/manifest"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+type fakeHelm struct{}
+
+func (f fakeHelm) Pull(chart, version string, opts PullOpts) error {
+	return nil
+}
+
+func (f fakeHelm) RepoUpdate(opts Opts) error {
+	return nil
+}
+
+func (f fakeHelm) Template(name, chart string, opts TemplateOpts) (manifest.List, error) {
+	return nil, nil
+}
+
+func TestAddRepos(t *testing.T) {
+	c, err := InitChartfile(filepath.Join(t.TempDir(), Filename))
+	require.NoError(t, err)
+
+	err = c.AddRepos(
+		Repo{Name: "foo", URL: "https://foo.com"},
+		Repo{Name: "foo2", URL: "https://foo2.com"},
+	)
+	assert.NoError(t, err)
+
+	err = c.AddRepos(
+		Repo{Name: "foo", URL: "https://foo.com"},
+	)
+	assert.EqualError(t, err, "1 Repo(s) were skipped. Please check above logs for details")
+}
+
+func TestAdd(t *testing.T) {
+	c, err := InitChartfile(filepath.Join(t.TempDir(), Filename))
+	require.NoError(t, err)
+	c.Helm = &fakeHelm{}
+
+	err = c.Add([]string{"stable/package@1.0.0"})
+	assert.NoError(t, err)
+
+	// Adding again the same chart
+	err = c.Add([]string{"stable/package@1.0.0"})
+	assert.EqualError(t, err, "1 Chart(s) were skipped. Please check above logs for details")
+
+	// Update a version
+	err = c.Add([]string{"stable/package@1.1.0"})
+	assert.NoError(t, err)
+}