Repository: grafana/tanka
Pull request #368 (2020-09-01 19:35:59)

fix(jsonnet): Provide full filename
Now invokes the evaluation with the absolute filename of `main.jsonnet`.

Without doing so, this had the effect that `std.thisFile` reported literally `main.jsonnet`, which renders this value unusable to detect the surroundings of that file.

Removed `pkg/kubernetes/data_test.go` because this file was orphaned (forget while moving the functionality to `pkg/process`, replacement is `pkg/process/data_test.go`)


Patch:
diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go
index 10354456..6752d0ae 100644
--- a/pkg/helm/helm.go
+++ b/pkg/helm/helm.go
@@ -46,6 +46,7 @@ func (e ExecHelm) Pull(chart, version string, opts PullOpts) error {
 		"--version", version,
 		"--destination", opts.Destination,
 		"--repository-config", repoFile,
+		"--untar",
 	)
 
 	return cmd.Run()
diff --git a/pkg/jsonnet/eval.go b/pkg/jsonnet/eval.go
index b2ae10fc..787e5cf9 100644
--- a/pkg/jsonnet/eval.go
+++ b/pkg/jsonnet/eval.go
@@ -26,11 +26,11 @@ func EvaluateFile(jsonnetFile string, mods ...Modifier) (string, error) {
 	if err != nil {
 		return "", errors.Wrap(err, "resolving jpath")
 	}
-	return Evaluate(string(bytes), jpath, mods...)
+	return Evaluate(jsonnetFile, string(bytes), jpath, mods...)
 }
 
 // Evaluate renders the given jsonnet into a string
-func Evaluate(sonnet string, jpath []string, mods ...Modifier) (string, error) {
+func Evaluate(filename, sonnet string, jpath []string, mods ...Modifier) (string, error) {
 	vm := jsonnet.MakeVM()
 	vm.Importer(NewExtendedImporter(jpath))
 
@@ -44,7 +44,7 @@ func Evaluate(sonnet string, jpath []string, mods ...Modifier) (string, error) {
 		vm.NativeFunction(nf)
 	}
 
-	return vm.EvaluateSnippet("main.jsonnet", sonnet)
+	return vm.EvaluateSnippet(filename, sonnet)
 }
 
 // WithExtCode allows to make the supplied snippet available to Jsonnet as an


Test patch:
diff --git a/pkg/kubernetes/data_test.go b/pkg/kubernetes/data_test.go
deleted file mode 100644
index 9cd65f3d..00000000
--- a/pkg/kubernetes/data_test.go
+++ /dev/null
@@ -1,64 +0,0 @@
-package kubernetes
-
-import (
-	"encoding/json"
-	"fmt"
-	"io/ioutil"
-	"strings"
-
-	"github.com/grafana/tanka/pkg/jsonnet"
-	"github.com/grafana/tanka/pkg/kubernetes/manifest"
-)
-
-// testData holds data for tests
-type testData struct {
-	Deep interface{}                  `json:"deep"`
-	Flat map[string]manifest.Manifest `json:"flat"`
-}
-
-func loadFixture(name string) testData {
-	raw, err := ioutil.ReadFile("./testdata/td" + strings.Title(name) + ".jsonnet")
-	if err != nil {
-		panic(fmt.Sprint("loading fixture:", err))
-	}
-
-	data, err := jsonnet.Evaluate(string(raw), []string{"./testdata"})
-	if err != nil {
-		panic(fmt.Sprint("loading fixture:", err))
-	}
-
-	var d testData
-	if err := json.Unmarshal([]byte(data), &d); err != nil {
-		panic(fmt.Sprint("loading fixture:", err))
-	}
-
-	return d
-}
-
-// testDataRegular is a regular output of jsonnet without special things, but it
-// is nested.
-func testDataRegular() testData {
-	return loadFixture("regular")
-}
-
-// testDataFlat is a flat manifest that does not need reconciliation
-func testDataFlat() testData {
-	return loadFixture("flat")
-}
-
-// testDataPrimitive is an invalid manifest, because it ends with a primitive
-// without including required fields
-func testDataPrimitive() testData {
-	return loadFixture("invalidPrimitive")
-}
-
-// testDataDeep is super deeply nested on multiple levels
-func testDataDeep() testData {
-	return loadFixture("deep")
-}
-
-// testDataArray is an array of (deeply nested) dicts that should be fully
-// flattened
-func testDataArray() testData {
-	return loadFixture("array")
-}
diff --git a/pkg/process/data_test.go b/pkg/process/data_test.go
index c38c9ec5..d18cdf94 100644
--- a/pkg/process/data_test.go
+++ b/pkg/process/data_test.go
@@ -17,12 +17,13 @@ type testData struct {
 }
 
 func loadFixture(name string) testData {
-	raw, err := ioutil.ReadFile("./testdata/td" + strings.Title(name) + ".jsonnet")
+	filename := "./testdata/td" + strings.Title(name) + ".jsonnet"
+	raw, err := ioutil.ReadFile(filename)
 	if err != nil {
 		panic(fmt.Sprint("loading fixture:", err))
 	}
 
-	data, err := jsonnet.Evaluate(string(raw), []string{"./testdata"})
+	data, err := jsonnet.Evaluate(filename, string(raw), []string{"./testdata"})
 	if err != nil {
 		panic(fmt.Sprint("loading fixture:", err))
 	}