Skip to content

Commit

Permalink
Several more test cases, disable most of encoding/cue
Browse files Browse the repository at this point in the history
  • Loading branch information
sam boyer committed May 2, 2023
1 parent 9cda1a6 commit eff90d0
Show file tree
Hide file tree
Showing 14 changed files with 649 additions and 384 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ linters:
- dogsled
- errcheck
- exportloopref
- funlen
- gochecknoinits
- gocritic
- goconst
Expand Down
84 changes: 42 additions & 42 deletions cmd/thema/lineage_bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package main

import (
"fmt"
"os"

"cuelang.org/go/cue/ast"
"github.com/grafana/thema"
"github.com/grafana/thema/encoding/cue"
tastutil "github.com/grafana/thema/internal/astutil"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -36,44 +31,49 @@ func (bc *bumpCommand) setup(cmd *cobra.Command) {
lineageBumpCmd.Flags().BoolVar(&bc.maj, "major", false, "Bump the major version (breaking change) instead of the minor version")
lineageBumpCmd.Flags().BoolVar(&bc.maj, "no-fill", false, "Do not pre-fill the new schema with the prior schema")
lineageBumpCmd.PreRunE = bc.lla.validateLineageInput
lineageBumpCmd.Run = bc.run
}

func (bc *bumpCommand) run(cmd *cobra.Command, args []string) {
if err := bc.do(cmd, args); err != nil {
fmt.Fprintf(cmd.ErrOrStderr(), "%s\n", err)
os.Exit(1)
// lineageBumpCmd.Run = bc.run
// TODO un-hide this once cue encoding package is refactored
lineageBumpCmd.Hidden = true
lineageBumpCmd.Run = func(cmd *cobra.Command, args []string) {
fmt.Fprint(cmd.OutOrStdout(), "bump subcommand is disabled, pending refactor")
}
}

func (bc *bumpCommand) do(cmd *cobra.Command, args []string) error {
lv := thema.LatestVersion(bc.lla.dl.lin)
lsch := thema.SchemaP(bc.lla.dl.lin, lv)
// TODO UGH EVAL
schlit := tastutil.Format(lsch.Underlying().Eval())

var err error
var nlin ast.Node
if bc.maj {
nlin = bc.lla.dl.lin.Underlying().Source()
err = cue.InsertSchemaNodeAs(nlin, tastutil.ToExpr(schlit), thema.SV(lv[0]+1, 0))
if err != nil {
return err
}
} else {
nlin, err = cue.Append(bc.lla.dl.lin, lsch.Underlying())
if err != nil {
return err
}
}

b, err := tastutil.FmtNode(tastutil.ToExpr(nlin))
if err != nil {
return err
}

// TODO write back to subpath
// func (bc *bumpCommand) run(cmd *cobra.Command, args []string) {
// if err := bc.do(cmd, args); err != nil {
// fmt.Fprintf(cmd.ErrOrStderr(), "%s\n", err)
// os.Exit(1)
// }
// }

fmt.Fprint(cmd.OutOrStdout(), string(b))
return nil
}
// func (bc *bumpCommand) do(cmd *cobra.Command, args []string) error {
// lv := thema.LatestVersion(bc.lla.dl.lin)
// lsch := thema.SchemaP(bc.lla.dl.lin, lv)
// // TODO UGH EVAL
// schlit := tastutil.Format(lsch.Underlying().Eval())
//
// var err error
// var nlin ast.Node
// if bc.maj {
// nlin = bc.lla.dl.lin.Underlying().Source()
// err = cue.InsertSchemaNodeAs(nlin, tastutil.ToExpr(schlit), thema.SV(lv[0]+1, 0))
// if err != nil {
// return err
// }
// } else {
// nlin, err = cue.Append(bc.lla.dl.lin, lsch.Underlying())
// if err != nil {
// return err
// }
// }
//
// b, err := tastutil.FmtNode(tastutil.ToExpr(nlin))
// if err != nil {
// return err
// }
//
// // TODO write back to subpath
//
// fmt.Fprint(cmd.OutOrStdout(), string(b))
// return nil
// }
12 changes: 6 additions & 6 deletions cmd/thema/lineage_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"cuelang.org/go/encoding/jsonschema"
"cuelang.org/go/encoding/openapi"
"cuelang.org/go/encoding/yaml"
"github.com/grafana/thema"
"github.com/grafana/thema/encoding/cue"
tastutil "github.com/grafana/thema/internal/astutil"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -175,12 +174,13 @@ func (ic *initCommand) runEmpty(cmd *cobra.Command, args []string) {
return
}

// TODO uncomment after re-export
// Have to re-insert because comments get lost somehow by NewLineage()
err = cue.InsertSchemaNodeAs(linf, expr, thema.SV(0, 0))
if err != nil {
ic.err = err
return
}
// err = cue.InsertSchemaNodeAs(linf, expr, thema.SV(0, 0))
// if err != nil {
// ic.err = err
// return
// }

linf, err = toSubpath(ic.cuepath, linf)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions encoding/cue/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func NewLineage(sch cue.Value, name, pkgname string) (*ast.File, error) {
}
b, err := astutil.FmtNode(x)
if err != nil {

return nil, fmt.Errorf("failed to convert input schema to string: %ww", err)
}

Expand Down Expand Up @@ -58,7 +57,7 @@ func NewLineage(sch cue.Value, name, pkgname string) (*ast.File, error) {
// The provided lineage node is modified in place. Corresponding lenses are not
// generated. The result is not checked for Thema validity. Behavior is
// undefined if the provided lineage node is not well-formed.
func InsertSchemaNodeAs(lin ast.Node, sch ast.Expr, v thema.SyntacticVersion) error {
func insertSchemaNodeAs(lin ast.Node, sch ast.Expr, v thema.SyntacticVersion) error {
seql := astutil.FindSeqs(lin)
if seql == nil {
return fmt.Errorf("could not find seqs list in input - invalid lineage ast?")
Expand Down Expand Up @@ -121,7 +120,7 @@ schemas: [
// lineage, the new schema will be appended to the latest sequence (minor
// version bump). Otherwise, a new sequence will be created with the provided
// schema as its only element (major version bump).
func Append(lin thema.Lineage, sch cue.Value) (ast.Node, error) {
func appendlin(lin thema.Lineage, sch cue.Value) (ast.Node, error) {
linf := astutil.Format(lin.Underlying()).(*ast.File)
schnode := astutil.ToExpr(astutil.Format(sch))

Expand Down
35 changes: 1 addition & 34 deletions encoding/cue/encode_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,11 @@
package cue

import (
"testing"

"cuelang.org/go/cue/cuecontext"
"github.com/grafana/thema"
"github.com/grafana/thema/exemplars"
)

var ctx = cuecontext.New()
var rt = thema.NewRuntime(ctx)

func TestDoSimpleGenLineage(t *testing.T) {
cuestr := `
foo: string
bar: int
`
v := ctx.CompileString(cuestr)

f, err := NewLineage(v, "somelineage", "testpkg")
if err != nil {
t.Fatal(err)
}
_ = f
// fmt.Println(string(astutil.FmtNode(f)))
}

func TestSimpleAppendLineage(t *testing.T) {
lin, _ := exemplars.NarrowingLineage(rt)

cuestr := `
properbool: bool
foo?: int
`
v := ctx.CompileString(cuestr)

f, err := Append(lin, v)
if err != nil {
t.Fatal(err)
}
_ = f
// fmt.Println(string(astutil.FmtNodeP(f)))
}
// TODO rewrite everything here on top of the corpus
1 change: 1 addition & 0 deletions encoding/gocode/gocode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestGenerate(t *testing.T) {
Name: "encoding/gocode/TestGenerate",
ToDo: map[string]string{
"lineage/defaultchange": "default backcompat invariants not working properly yet",
"lineage/optional": "Optional fields do not satisfy struct.MinFields(), causing #Lineage constraints to fail",
},
}

Expand Down
76 changes: 0 additions & 76 deletions encoding/gocode/testdata/joinschema.txtar

This file was deleted.

Loading

0 comments on commit eff90d0

Please sign in to comment.