Skip to content

feat: Add the diff command #1963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220626175859-9abda183db8e
github.com/bytecodealliance/wasmtime-go v1.0.0
github.com/cubicdaiya/gonp v1.0.4
github.com/davecgh/go-spew v1.1.1
github.com/go-sql-driver/mysql v1.6.0
github.com/google/go-cmp v0.5.9
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/cubicdaiya/gonp v1.0.4 h1:ky2uIAJh81WiLcGKBVD5R7KsM/36W6IqqTy6Bo6rGws=
github.com/cubicdaiya/gonp v1.0.4/go.mod h1:iWGuP/7+JVTn02OWhRemVbMmG1DOUnmrGTYYACpOI0I=
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso=
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
49 changes: 49 additions & 0 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"bufio"
"bytes"
"context"
"fmt"
"io"
Expand All @@ -9,6 +11,7 @@ import (
"path/filepath"
"runtime/trace"

"github.com/cubicdaiya/gonp"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
Expand All @@ -31,6 +34,7 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int
rootCmd.PersistentFlags().BoolP("experimental", "x", false, "enable experimental features (default: false)")

rootCmd.AddCommand(checkCmd)
rootCmd.AddCommand(diffCmd)
rootCmd.AddCommand(genCmd)
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(versionCmd)
Expand Down Expand Up @@ -201,3 +205,48 @@ var checkCmd = &cobra.Command{
return nil
},
}

func getLines(f []byte) []string {
fp := bytes.NewReader(f)
scanner := bufio.NewScanner(fp)
lines := make([]string, 0)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}

func filterHunks[T gonp.Elem](uniHunks []gonp.UniHunk[T]) []gonp.UniHunk[T] {
var out []gonp.UniHunk[T]
for i, uniHunk := range uniHunks {
var changed bool
for _, e := range uniHunk.GetChanges() {
switch e.GetType() {
case gonp.SesDelete:
changed = true
case gonp.SesAdd:
changed = true
}
}
if changed {
out = append(out, uniHunks[i])
}
}
return out
}

var diffCmd = &cobra.Command{
Use: "diff",
Short: "Compare the generated files to the existing files",
RunE: func(cmd *cobra.Command, args []string) error {
if debug.Traced {
defer trace.StartRegion(cmd.Context(), "diff").End()
}
stderr := cmd.ErrOrStderr()
dir, name := getConfigPath(stderr, cmd.Flag("file"))
if err := Diff(cmd.Context(), ParseEnv(cmd), dir, name, stderr); err != nil {
os.Exit(1)
}
return nil
},
}
62 changes: 62 additions & 0 deletions internal/cmd/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"context"
"errors"
"fmt"
"io"
"os"
"runtime/trace"
"sort"
"strings"

"github.com/cubicdaiya/gonp"
"github.com/kyleconroy/sqlc/internal/debug"
)

func Diff(ctx context.Context, e Env, dir, name string, stderr io.Writer) error {
output, err := Generate(ctx, e, dir, name, stderr)
if err != nil {
return err
}
if debug.Traced {
defer trace.StartRegion(ctx, "checkfiles").End()
}
var errored bool

keys := make([]string, 0, len(output))
for k, _ := range output {
kk := k
keys = append(keys, kk)
}
sort.Strings(keys)

for _, filename := range keys {
source := output[filename]
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
errored = true
// stdout message
continue
}
existing, err := os.ReadFile(filename)
if err != nil {
errored = true
fmt.Fprintf(stderr, "%s: %s\n", filename, err)
continue
}
diff := gonp.New(getLines(existing), getLines([]byte(source)))
diff.Compose()
uniHunks := filterHunks(diff.UnifiedHunks())

if len(uniHunks) > 0 {
errored = true
fmt.Fprintf(stderr, "--- a%s\n", strings.TrimPrefix(filename, dir))
fmt.Fprintf(stderr, "+++ b%s\n", strings.TrimPrefix(filename, dir))
diff.FprintUniHunks(stderr, uniHunks)
}
}
if errored {
return errors.New("diff found")
}
return nil
}
49 changes: 45 additions & 4 deletions internal/endtoend/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -92,14 +93,31 @@ func TestReplay(t *testing.T) {
tc := replay
t.Run(tc, func(t *testing.T) {
t.Parallel()
path, _ := filepath.Abs(tc)

var stderr bytes.Buffer
var output map[string]string
var err error

path, _ := filepath.Abs(tc)
args := parseExec(t, path)
expected := expectedStderr(t, path)
output, err := cmd.Generate(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)

switch args.Command {
case "diff":
err = cmd.Diff(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)
case "generate":
output, err = cmd.Generate(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)
if err == nil {
cmpDirectory(t, path, output)
}
default:
t.Fatalf("unknown command")
}

if len(expected) == 0 && err != nil {
t.Fatalf("sqlc generate failed: %s", stderr.String())
t.Fatalf("sqlc %s failed: %s", args.Command, stderr.String())
}
cmpDirectory(t, path, output)

if diff := cmp.Diff(expected, stderr.String()); diff != "" {
t.Errorf("stderr differed (-want +got):\n%s", diff)
}
Expand Down Expand Up @@ -179,6 +197,29 @@ func expectedStderr(t *testing.T, dir string) string {
return ""
}

type exec struct {
Command string `json:"command"`
}

func parseExec(t *testing.T, dir string) exec {
t.Helper()
var e exec
path := filepath.Join(dir, "exec.json")
if _, err := os.Stat(path); !os.IsNotExist(err) {
blob, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(blob, &e); err != nil {
t.Fatal(err)
}
}
if e.Command == "" {
e.Command = "generate"
}
return e
}

func BenchmarkReplay(b *testing.B) {
ctx := context.Background()
var dirs []string
Expand Down
3 changes: 3 additions & 0 deletions internal/endtoend/testdata/diff_no_output/exec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"command": "diff"
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/diff_no_output/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions internal/endtoend/testdata/diff_no_output/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions internal/endtoend/testdata/diff_no_output/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/endtoend/testdata/diff_no_output/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY bio;

-- name: SelectOne :one
SELECT 1;

-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING *;
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/diff_no_output/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);

CREATE TABLE books (
id BIGSERIAL PRIMARY KEY,
title text NOT NULL
);

Loading