diff --git a/cli/internal/metadataobject/querycollections/query_collections.go b/cli/internal/metadataobject/querycollections/query_collections.go index 3694efe462ea6..90bb55defd25c 100644 --- a/cli/internal/metadataobject/querycollections/query_collections.go +++ b/cli/internal/metadataobject/querycollections/query_collections.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "path/filepath" + "github.com/hasura/graphql-engine/cli/v2/internal/errors" "github.com/hasura/graphql-engine/cli/v2/internal/metadataobject" "github.com/sirupsen/logrus" @@ -35,28 +36,30 @@ func (q *QueryCollectionConfig) Validate() error { } func (q *QueryCollectionConfig) CreateFiles() error { + var op errors.Op = "querycollections.QueryCollectionsConfig.CreateFiles" v := make([]interface{}, 0) buf := new(bytes.Buffer) err := metadataobject.GetEncoder(buf).Encode(v) if err != nil { - return err + return errors.E(op, err) } err = ioutil.WriteFile(filepath.Join(q.MetadataDir, q.Filename()), buf.Bytes(), 0644) if err != nil { - return err + return errors.E(op, err) } return nil } func (q *QueryCollectionConfig) Build() (map[string]interface{}, error) { + var op errors.Op = "querycollections.QueryCollectionConfig.Build" data, err := metadataobject.ReadMetadataFile(filepath.Join(q.MetadataDir, q.Filename())) if err != nil { - return nil, q.error(err) + return nil, errors.E(op, q.error(err)) } var obj []yaml.Node err = yaml.Unmarshal(data, &obj) if err != nil { - return nil, q.error(err) + return nil, errors.E(op, errors.KindBadInput, q.error(err)) } return map[string]interface{}{q.Key(): obj}, nil } @@ -75,6 +78,7 @@ type query struct { } func (q *QueryCollectionConfig) Export(metadata map[string]yaml.Node) (map[string][]byte, error) { + var op errors.Op = "querycollections.QueryCollectionConfig.Export" var value interface{} if v, ok := metadata[q.Key()]; !ok { value = []yaml.Node{} @@ -82,10 +86,10 @@ func (q *QueryCollectionConfig) Export(metadata map[string]yaml.Node) (map[strin var collections []querycollection bs, err := yaml.Marshal(v) if err != nil { - return nil, q.error(err) + return nil, errors.E(op, q.error(err)) } if err := yaml.Unmarshal(bs, &collections); err != nil { - return nil, q.error(err) + return nil, errors.E(op, q.error(err)) } for collectionIdx := range collections { for queryIdx := range collections[collectionIdx].Definition.Queries { @@ -94,7 +98,7 @@ func (q *QueryCollectionConfig) Export(metadata map[string]yaml.Node) (map[strin Input: collections[collectionIdx].Definition.Queries[queryIdx].Query, }) if err != nil { - return nil, q.error(err) + return nil, errors.E(op, q.error(err)) } gqlFormatter := formatter.NewFormatter(buf, formatter.WithIndent(" ")) gqlFormatter.FormatQueryDocument(queryDoc) @@ -109,7 +113,7 @@ func (q *QueryCollectionConfig) Export(metadata map[string]yaml.Node) (map[strin var buf bytes.Buffer err := metadataobject.GetEncoder(&buf).Encode(value) if err != nil { - return nil, q.error(err) + return nil, errors.E(op, q.error(err)) } return map[string][]byte{ filepath.ToSlash(filepath.Join(q.BaseDirectory(), q.Filename())): buf.Bytes(), @@ -125,18 +129,20 @@ func (q *QueryCollectionConfig) Filename() string { } func (q *QueryCollectionConfig) GetFiles() ([]string, error) { + var op errors.Op = "querycollections.QueryCollectionConfig.GetFiles" rootFile := filepath.Join(q.BaseDirectory(), q.Filename()) files, err := metadataobject.DefaultGetFiles(rootFile) if err != nil { - return nil, q.error(err) + return nil, errors.E(op, q.error(err)) } return files, nil } func (q *QueryCollectionConfig) WriteDiff(opts metadataobject.WriteDiffOpts) error { + var op errors.Op = "querycollections.QueryCollectionConfig.WriteDiff" err := metadataobject.DefaultWriteDiff(metadataobject.DefaultWriteDiffOpts{From: q, WriteDiffOpts: opts}) if err != nil { - return q.error(err) + return errors.E(op, q.error(err)) } return nil } diff --git a/cli/internal/metadataobject/querycollections/query_collections_test.go b/cli/internal/metadataobject/querycollections/query_collections_test.go index 757d6d0d8c67b..cbef91b260e00 100644 --- a/cli/internal/metadataobject/querycollections/query_collections_test.go +++ b/cli/internal/metadataobject/querycollections/query_collections_test.go @@ -8,6 +8,7 @@ import ( "github.com/hasura/graphql-engine/cli/v2/internal/metadatautil" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" ) @@ -22,6 +23,7 @@ func TestQueryCollectionConfig_Build(t *testing.T) { fields fields wantGolden string wantErr bool + assertErr require.ErrorAssertionFunc }{ { "t1", @@ -32,6 +34,7 @@ func TestQueryCollectionConfig_Build(t *testing.T) { }, "testdata/build_test/t1/want.golden.json", false, + require.NoError, }, { "t2", @@ -42,6 +45,7 @@ func TestQueryCollectionConfig_Build(t *testing.T) { }, "testdata/build_test/t2/want.golden.json", false, + require.NoError, }, } for _, tt := range tests { @@ -51,9 +55,8 @@ func TestQueryCollectionConfig_Build(t *testing.T) { logger: tt.fields.logger, } got, err := q.Build() - if tt.wantErr { - assert.Error(t, err) - } else { + tt.assertErr(t, err) + if !tt.wantErr { assert.NoError(t, err) gotbs, err := yaml.Marshal(got) assert.NoError(t, err) @@ -80,12 +83,13 @@ func TestQueryCollectionConfig_Export(t *testing.T) { metadata map[string]yaml.Node } tests := []struct { - id string - name string - fields fields - args args - want map[string][]byte - wantErr bool + id string + name string + fields fields + args args + want map[string][]byte + wantErr bool + assertErr require.ErrorAssertionFunc }{ { "t1", @@ -113,6 +117,7 @@ func TestQueryCollectionConfig_Export(t *testing.T) { }(), }, false, + require.NoError, }, { "t2", @@ -140,6 +145,7 @@ func TestQueryCollectionConfig_Export(t *testing.T) { }(), }, false, + require.NoError, }, { "t3", @@ -167,6 +173,7 @@ func TestQueryCollectionConfig_Export(t *testing.T) { }(), }, false, + require.NoError, }, { "t4", @@ -194,6 +201,7 @@ func TestQueryCollectionConfig_Export(t *testing.T) { }(), }, false, + require.NoError, }, { "t5", @@ -209,6 +217,7 @@ func TestQueryCollectionConfig_Export(t *testing.T) { "metadata/query_collections.yaml": readYamlFileAndEmitBytes(t, "testdata/export_test/t5/want.query_collections.yaml"), }, false, + require.NoError, }, } for _, tt := range tests { @@ -218,9 +227,8 @@ func TestQueryCollectionConfig_Export(t *testing.T) { logger: tt.fields.logger, } got, err := q.Export(tt.args.metadata) - if tt.wantErr { - assert.Error(t, err) - } else { + tt.assertErr(t, err) + if !tt.wantErr { assert.NoError(t, err) for k, v := range got { assert.Contains(t, tt.want, k)