From 9810a14b0eacd6309666eb92703ae8aafeb3464a Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Thu, 25 Jan 2024 19:33:57 -0800 Subject: [PATCH 1/3] refactor: uses cue-helper library for replacing CUE values --- tools/updater/cmd/main.go | 5 +-- tools/updater/go.mod | 12 ++++--- tools/updater/go.sum | 28 ++++++++++------ tools/updater/pkg/cue.go | 49 ---------------------------- tools/updater/pkg/cue_test.go | 61 ++--------------------------------- 5 files changed, 31 insertions(+), 124 deletions(-) diff --git a/tools/updater/cmd/main.go b/tools/updater/cmd/main.go index 0480df5d7..60e33a5f0 100644 --- a/tools/updater/cmd/main.go +++ b/tools/updater/cmd/main.go @@ -10,6 +10,7 @@ import ( "cuelang.org/go/cue/format" "github.com/alecthomas/kong" "github.com/input-output-hk/catalyst-ci/tools/updater/pkg" + ch "github.com/mheers/cue-helper/pkg/value" ) var cli struct { @@ -31,10 +32,10 @@ func main() { ctx.Fatalf("path %q does not exist", cli.Path) } - v, err = pkg.FillPathOverride(cuectx, v, cli.Path, cli.Value) + v, err = ch.Replace(v, cli.Path, cli.Value) ctx.FatalIfErrorf(err) - node := v.Syntax(cue.Final(), cue.Concrete(true)) + node := v.Syntax(cue.Final(), cue.Concrete(true), cue.Docs(true)) src, err := format.Node(node) ctx.FatalIfErrorf(err) diff --git a/tools/updater/go.mod b/tools/updater/go.mod index fb3922d2a..fbc8a1382 100644 --- a/tools/updater/go.mod +++ b/tools/updater/go.mod @@ -5,18 +5,22 @@ go 1.20 require ( cuelang.org/go v0.7.0 github.com/alecthomas/kong v0.8.1 - github.com/onsi/ginkgo/v2 v2.15.0 - github.com/onsi/gomega v1.31.0 + github.com/mheers/cue-helper v0.0.0-20231214081257-a325036f9a0d + github.com/onsi/ginkgo/v2 v2.9.2 + github.com/onsi/gomega v1.27.5 ) require ( github.com/cockroachdb/apd/v3 v3.2.1 // indirect - github.com/go-logr/logr v1.3.0 // indirect + github.com/evanphx/json-patch v5.6.0+incompatible // indirect + github.com/go-logr/logr v1.2.3 // indirect github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect - github.com/google/uuid v1.2.0 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/kubevela/workflow v0.6.0 // indirect github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect + github.com/pkg/errors v0.9.1 // indirect golang.org/x/net v0.19.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/tools/updater/go.sum b/tools/updater/go.sum index 2a68e4d58..bbde0c908 100644 --- a/tools/updater/go.sum +++ b/tools/updater/go.sum @@ -14,8 +14,10 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/emicklei/proto v1.10.0 h1:pDGyFRVV5RvV+nkBK9iy3q67FBy9Xa7vwrOTE+g5aGw= -github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= -github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U= +github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= @@ -24,29 +26,35 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= -github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= -github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kubevela/workflow v0.6.0 h1:fYXviOYD5zqHs3J61tNbM4HZ85EcZlPm7Fyz8Q5o9Fk= +github.com/kubevela/workflow v0.6.0/go.mod h1:sjLcYqKHKeCQ+w77gijoNILwIShJKnCU+e3q7ETtZGI= github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw= +github.com/mheers/cue-helper v0.0.0-20231214081257-a325036f9a0d h1:JZsFf1WWJjBq7swih1HeWbmnh5uKZ2Ol2HSZ8o6kcK8= +github.com/mheers/cue-helper v0.0.0-20231214081257-a325036f9a0d/go.mod h1:TvPPtiz/noXLe+NDP3XJLg8Y0ROt3m6rVlqUYlLuba0= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de h1:D5x39vF5KCwKQaw+OC9ZPiLVHXz3UFw2+psEX+gYcto= github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de/go.mod h1:kJun4WP5gFuHZgRjZUWWuH1DTxCtxbHDOIJsudS8jzY= -github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY= -github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM= -github.com/onsi/gomega v1.31.0 h1:54UJxxj6cPInHS3a35wm6BK/F9nHYueZ1NVujHDrnXE= -github.com/onsi/gomega v1.31.0/go.mod h1:DW9aCi7U6Yi40wNVAvT6kzFnEVEI5n3DloYBiKiT6zk= +github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU= +github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts= +github.com/onsi/gomega v1.27.5 h1:T/X6I0RNFw/kTqgfkZPcQ5KU6vCnWNBGdtrIx2dpGeQ= +github.com/onsi/gomega v1.27.5/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/image-spec v1.1.0-rc4 h1:oOxKUJWnFC4YGHCCMNql1x4YaDfYBTS5Y4x/Cgeo1E0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0 h1:sadMIsgmHpEOGbUs6VtHBXRR1OHevnj7hLx9ZcdNGW4= github.com/rogpeppe/go-internal v1.11.1-0.20231026093722-fa6a31e0812c h1:fPpdjePK1atuOg28PXfNSqgwf9I/qD1Hlo39JFwKBXk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= @@ -57,7 +65,7 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= -google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tools/updater/pkg/cue.go b/tools/updater/pkg/cue.go index ca6d6b798..d6f4f7621 100644 --- a/tools/updater/pkg/cue.go +++ b/tools/updater/pkg/cue.go @@ -3,41 +3,12 @@ package pkg // cspell: words cuelang import ( - "encoding/json" "fmt" "os" - "strings" "cuelang.org/go/cue" ) -// FillPathOverride is like cue.Value.FillPath, but it allows you to override concrete values. -// The default behavior of CUE is to support immutable values, so you can't normally override a concrete value. -// This function first converts the cue.Value to JSON, then to a map, and then modifies the map. -// Finally, it converts the map back to JSON and then to a cue.Value. -func FillPathOverride(ctx *cue.Context, v cue.Value, path string, value interface{}) (cue.Value, error) { - j, err := v.MarshalJSON() - if err != nil { - return cue.Value{}, fmt.Errorf("failed to marshal cue.Value to JSON: %w", err) - } - - var data map[string]interface{} - if err := json.Unmarshal(j, &data); err != nil { - return cue.Value{}, fmt.Errorf("failed to unmarshal JSON to map: %w", err) - } - - if err := setField(data, path, value); err != nil { - return cue.Value{}, fmt.Errorf("failed to set field %q to value %v: %w", path, value, err) - } - - modifiedJSON, err := json.Marshal(data) - if err != nil { - return cue.Value{}, fmt.Errorf("failed to marshal map to JSON: %w", err) - } - - return ctx.CompileBytes(modifiedJSON), nil -} - // ReadFile reads a CUE file and returns a cue.Value. func ReadFile(ctx *cue.Context, path string) (cue.Value, error) { contents, err := os.ReadFile(path) @@ -52,23 +23,3 @@ func ReadFile(ctx *cue.Context, path string) (cue.Value, error) { return v, nil } - -// setField sets the value at the given path in a map, creating nested maps as necessary. -func setField(m map[string]interface{}, path string, value interface{}) error { - parts := strings.Split(path, ".") - for i, part := range parts { - if i == len(parts)-1 { - m[part] = value - } else { - if _, ok := m[part]; !ok { - m[part] = make(map[string]interface{}) - } - var ok bool - m, ok = m[part].(map[string]interface{}) - if !ok { - return fmt.Errorf("invalid path: %s", path) - } - } - } - return nil -} diff --git a/tools/updater/pkg/cue_test.go b/tools/updater/pkg/cue_test.go index a610ccf79..3cc6e0f9d 100644 --- a/tools/updater/pkg/cue_test.go +++ b/tools/updater/pkg/cue_test.go @@ -3,67 +3,10 @@ package pkg_test // cspell: words cuelang cuecontext import ( - "cuelang.org/go/cue" - "cuelang.org/go/cue/cuecontext" - "github.com/input-output-hk/catalyst-ci/tools/updater/pkg" . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" + // . "github.com/onsi/gomega" ) var _ = Describe("Cue", func() { - Describe("FillPathOverride", func() { - var path, str string - - When("given a nested CUE source", func() { - When("the path exists", func() { - BeforeEach(func() { - path = "bundles.instances.module.values.image.tag" - str = ` - bundle: { - instances: { - module: { - values: { - image: tag: "test" - } - } - } - } - ` - }) - - It("should override the value at the given path", func() { - ctx := cuecontext.New() - v := ctx.CompileString(str) - - v, err := pkg.FillPathOverride(ctx, v, path, "test1") - Expect(err).ToNot(HaveOccurred()) - - result, err := v.LookupPath(cue.ParsePath(path)).String() - Expect(err).ToNot(HaveOccurred()) - Expect(result).To(Equal("test1")) - }) - }) - - When("the path does not include structs", func() { - BeforeEach(func() { - path = "bundles.instances.module.values.image.tag" - str = ` - bundles: { - instances: { - module: "test" - } - } - ` - }) - - It("should return an error", func() { - ctx := cuecontext.New() - v := ctx.CompileString(str) - - _, err := pkg.FillPathOverride(ctx, v, path, "test1") - Expect(err).To(HaveOccurred()) - }) - }) - }) - }) + // TODO: Add tests for pkg.ReadFile }) From d59e812e873e8d74f4debf60f57f23a7e0a95fe6 Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Fri, 26 Jan 2024 13:52:10 -0800 Subject: [PATCH 2/3] chore: adds missing test --- tools/updater/cmd/main.go | 5 +++-- tools/updater/go.mod | 3 ++- tools/updater/go.sum | 8 +++++--- tools/updater/pkg/cue.go | 8 ++++---- tools/updater/pkg/cue_test.go | 35 ++++++++++++++++++++++++++++++++--- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/tools/updater/cmd/main.go b/tools/updater/cmd/main.go index 60e33a5f0..707f66a8a 100644 --- a/tools/updater/cmd/main.go +++ b/tools/updater/cmd/main.go @@ -1,6 +1,6 @@ package main -// cspell: words alecthomas cuelang cuecontext cuectx existingfile Timoni nolint +// cspell: words afero alecthomas cuelang cuecontext cuectx existingfile Timoni nolint import ( "os" @@ -11,6 +11,7 @@ import ( "github.com/alecthomas/kong" "github.com/input-output-hk/catalyst-ci/tools/updater/pkg" ch "github.com/mheers/cue-helper/pkg/value" + "github.com/spf13/afero" ) var cli struct { @@ -25,7 +26,7 @@ func main() { kong.Description("A helper tool for modifying CUE files to override arbitrary values. Useful for updating Timoni bundles.")) cuectx := cuecontext.New() - v, err := pkg.ReadFile(cuectx, cli.BundleFile) + v, err := pkg.ReadFile(cuectx, cli.BundleFile, afero.NewOsFs()) ctx.FatalIfErrorf(err) if !v.LookupPath(cue.ParsePath(cli.Path)).Exists() { diff --git a/tools/updater/go.mod b/tools/updater/go.mod index fbc8a1382..9293266cf 100644 --- a/tools/updater/go.mod +++ b/tools/updater/go.mod @@ -8,6 +8,7 @@ require ( github.com/mheers/cue-helper v0.0.0-20231214081257-a325036f9a0d github.com/onsi/ginkgo/v2 v2.9.2 github.com/onsi/gomega v1.27.5 + github.com/spf13/afero v1.11.0 ) require ( @@ -17,7 +18,7 @@ require ( github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.4.0 // indirect github.com/kubevela/workflow v0.6.0 // indirect github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect github.com/pkg/errors v0.9.1 // indirect diff --git a/tools/updater/go.sum b/tools/updater/go.sum index bbde0c908..66e80d5f8 100644 --- a/tools/updater/go.sum +++ b/tools/updater/go.sum @@ -26,8 +26,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -52,6 +52,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/protocolbuffers/txtpbfmt v0.0.0-20230328191034-3462fbc510c0 h1:sadMIsgmHpEOGbUs6VtHBXRR1OHevnj7hLx9ZcdNGW4= github.com/rogpeppe/go-internal v1.11.1-0.20231026093722-fa6a31e0812c h1:fPpdjePK1atuOg28PXfNSqgwf9I/qD1Hlo39JFwKBXk= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= @@ -65,7 +67,7 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= -google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tools/updater/pkg/cue.go b/tools/updater/pkg/cue.go index d6f4f7621..29d2eaf3f 100644 --- a/tools/updater/pkg/cue.go +++ b/tools/updater/pkg/cue.go @@ -1,17 +1,17 @@ package pkg -// cspell: words cuelang +// cspell: words afero cuelang import ( "fmt" - "os" "cuelang.org/go/cue" + "github.com/spf13/afero" ) // ReadFile reads a CUE file and returns a cue.Value. -func ReadFile(ctx *cue.Context, path string) (cue.Value, error) { - contents, err := os.ReadFile(path) +func ReadFile(ctx *cue.Context, path string, os afero.Fs) (cue.Value, error) { + contents, err := afero.ReadFile(os, path) if err != nil { return cue.Value{}, fmt.Errorf("failed to read file %q: %w", path, err) } diff --git a/tools/updater/pkg/cue_test.go b/tools/updater/pkg/cue_test.go index 3cc6e0f9d..ba264aace 100644 --- a/tools/updater/pkg/cue_test.go +++ b/tools/updater/pkg/cue_test.go @@ -1,12 +1,41 @@ package pkg_test -// cspell: words cuelang cuecontext +// cspell: words afero cuelang cuecontext import ( + "cuelang.org/go/cue/cuecontext" + "github.com/input-output-hk/catalyst-ci/tools/updater/pkg" . "github.com/onsi/ginkgo/v2" - // . "github.com/onsi/gomega" + . "github.com/onsi/gomega" + "github.com/spf13/afero" ) var _ = Describe("Cue", func() { - // TODO: Add tests for pkg.ReadFile + Describe("ReadFile", func() { + var os afero.Fs + + BeforeEach(func() { + os = afero.NewMemMapFs() + }) + + When("the file exists", func() { + It("returns a cue.Value", func() { + afero.WriteFile(os, "foo.cue", []byte("foo: 1"), 0644) + ctx := cuecontext.New() + + v, err := pkg.ReadFile(ctx, "foo.cue", os) + Expect(err).ToNot(HaveOccurred()) + Expect(v).ToNot(BeNil()) + }) + }) + + When("the file does not exist", func() { + It("returns an error", func() { + ctx := cuecontext.New() + + _, err := pkg.ReadFile(ctx, "foo.cue", os) + Expect(err).To(HaveOccurred()) + }) + }) + }) }) From 0cc78e238656141cc5d36483fc8e57631e5866c6 Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Fri, 26 Jan 2024 13:55:45 -0800 Subject: [PATCH 3/3] chore: fixes linting --- tools/updater/cmd/main.go | 2 +- tools/updater/pkg/cue_test.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/updater/cmd/main.go b/tools/updater/cmd/main.go index 707f66a8a..6f2c8f1ba 100644 --- a/tools/updater/cmd/main.go +++ b/tools/updater/cmd/main.go @@ -1,6 +1,6 @@ package main -// cspell: words afero alecthomas cuelang cuecontext cuectx existingfile Timoni nolint +// cspell: words afero alecthomas cuelang cuecontext cuectx existingfile mheers Timoni nolint import ( "os" diff --git a/tools/updater/pkg/cue_test.go b/tools/updater/pkg/cue_test.go index ba264aace..738596f00 100644 --- a/tools/updater/pkg/cue_test.go +++ b/tools/updater/pkg/cue_test.go @@ -20,9 +20,10 @@ var _ = Describe("Cue", func() { When("the file exists", func() { It("returns a cue.Value", func() { - afero.WriteFile(os, "foo.cue", []byte("foo: 1"), 0644) - ctx := cuecontext.New() + err := afero.WriteFile(os, "foo.cue", []byte("foo: 1"), 0644) + Expect(err).ToNot(HaveOccurred()) + ctx := cuecontext.New() v, err := pkg.ReadFile(ctx, "foo.cue", os) Expect(err).ToNot(HaveOccurred()) Expect(v).ToNot(BeNil())