Skip to content

Commit

Permalink
fix: Calculate the initial indent correctly in a couple edge cases. (#45
Browse files Browse the repository at this point in the history
)

1. When the first seen line is one of the `group_prefixes`.
2. When the first seen line is another keep-sorted directive.

Idempotency output before this change:

```
$ go test ./...
?       github.com/google/keep-sorted   [no test files]
?       github.com/google/keep-sorted/cmd       [no test files]
--- FAIL: TestGoldens (0.66s)
    --- FAIL: TestGoldens/group (0.00s)
        --- FAIL: TestGoldens/group/group (0.66s)
            golden_test.go:105: keep-sorted diff on keep-sorted output (should be idempotent) (-want +got)
                  (
                        """
                        ... // 180 identical lines
                          case 5:
                          // keep-sorted-test end
                -           return 10
                          case 2:
                -           return 2;
                          case 4:
                +         case 6:
                +           return 10
                +           return 2;
                            return 4;
                -         case 6:
                            return 6;
                          // keep-sorted-test end
                        """
                  )
FAIL
FAIL    github.com/google/keep-sorted/goldens   0.686s
ok      github.com/google/keep-sorted/keepsorted        (cached)
FAIL
```

Also, check the git history of this PR to see how group.out evolved.
  • Loading branch information
JeffFaer authored Oct 25, 2024
1 parent fa250d9 commit 3436ef5
Show file tree
Hide file tree
Showing 5 changed files with 404 additions and 308 deletions.
141 changes: 85 additions & 56 deletions goldens/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package golden_test

import (
"errors"
"fmt"
"io"
"maps"
"os"
Expand All @@ -29,13 +30,21 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestGoldens(t *testing.T) {
var (
dir, gitDir string
)

func init() {
_, fn, _, _ := runtime.Caller(0)
dir := filepath.Dir(fn)
gitDir, err := showTopLevel(dir)
dir = filepath.Dir(fn)
var err error
gitDir, err = showTopLevel(dir)
if err != nil {
t.Fatalf("Could not find root git dir: %v", err)
panic(fmt.Errorf("could not find root git dir: %w", err))
}
}

func TestGoldens(t *testing.T) {
des, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("Could not read goldens/ directory: %v", err)
Expand All @@ -53,62 +62,48 @@ func TestGoldens(t *testing.T) {
}

needsRegen := make(chan string, 2*len(tcs))
t.Run("group", func(t *testing.T) {
for _, tc := range tcs {
tc := tc
t.Run(tc, func(t *testing.T) {
t.Parallel()
inFile := filepath.Join(dir, tc+".in")
in, err := os.Open(inFile)
if err != nil {
t.Fatalf("Could not open .in file: %v", err)
}

wantOut, err := os.ReadFile(filepath.Join(dir, tc+".out"))
if err != nil {
t.Fatalf("Could not read .out file: %v", err)
}
wantErr, err := os.ReadFile(filepath.Join(dir, tc+".err"))
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("Could not read .err file: %v", err)
}
}
for _, tc := range tcs {
t.Run(tc, func(t *testing.T) {
t.Parallel()
inFile := filepath.Join(dir, tc+".in")
in, err := os.Open(inFile)
if err != nil {
t.Fatalf("Could not open .in file: %v", err)
}

cmd := exec.Command("go", "run", gitDir, "--id=keep-sorted-test", "--omit-timestamps", "-")
cmd.Stdin = in
stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatalf("Could not create stdout pipe: %v", err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
t.Fatalf("Could not create stderr pipe: %v", err)
}
if err := cmd.Start(); err != nil {
t.Errorf("could not start keep-sorted: %v", err)
wantOut, err := os.ReadFile(filepath.Join(dir, tc+".out"))
if err != nil {
t.Fatalf("Could not read .out file: %v", err)
}
wantErr, err := os.ReadFile(filepath.Join(dir, tc+".err"))
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("Could not read .err file: %v", err)
}
}

if gotErr, err := io.ReadAll(stderr); err != nil {
t.Errorf("could not read keep-sorted stderr: %v", err)
} else if diff := cmp.Diff(strings.Split(string(wantErr), "\n"), strings.Split(string(gotErr), "\n")); diff != "" {
t.Errorf("keep-sorted stderr:\n%s", diff)
needsRegen <- inFile
}
gotOut, gotErr, err := runKeepSorted(in)
if err != nil {
t.Errorf("Had trouble running keep-sorted: %v", err)
}
if diff := cmp.Diff(strings.Split(string(wantOut), "\n"), strings.Split(gotOut, "\n")); diff != "" {
t.Errorf("keep-sorted stdout diff (-want +got):\n%s", diff)
needsRegen <- inFile
}
if diff := cmp.Diff(strings.Split(string(wantErr), "\n"), strings.Split(gotErr, "\n")); diff != "" {
t.Errorf("keep-sorted stderr diff (-want +got):\n%s", diff)
needsRegen <- inFile
}

if gotOut, err := io.ReadAll(stdout); err != nil {
t.Errorf("could not read keep-sorted stdout: %v", err)
} else if diff := cmp.Diff(strings.Split(string(wantOut), "\n"), strings.Split(string(gotOut), "\n")); diff != "" {
t.Errorf("keep-sorted stdout diff (-want +got):\n%s", diff)
needsRegen <- inFile
}

if err := cmd.Wait(); err != nil {
t.Errorf("keep-sorted failed: %v", err)
}
})
}
})
gotOut2, _, err := runKeepSorted(strings.NewReader(gotOut))
if err != nil {
t.Errorf("Had trouble running keep-sorted on keep-sorted output: %v", err)
}
if diff := cmp.Diff(gotOut, gotOut2); diff != "" {
t.Errorf("keep-sorted diff on keep-sorted output (should be idempotent) (-want +got)\n%s", diff)
}
})
}

close(needsRegen)
files := make(map[string]bool)
Expand All @@ -125,3 +120,37 @@ func showTopLevel(dir string) (string, error) {
b, err := exec.Command("git", "-C", dir, "rev-parse", "--show-toplevel").Output()
return strings.TrimSpace(string(b)), err
}

func runKeepSorted(stdin io.Reader) (stdout, stderr string, err error) {
cmd := exec.Command("go", "run", gitDir, "--id=keep-sorted-test", "--omit-timestamps", "-")
cmd.Stdin = stdin
outPipe, err := cmd.StdoutPipe()
if err != nil {
return "", "", fmt.Errorf("could not create stdout pipe: %w", err)
}
errPipe, err := cmd.StderrPipe()
if err != nil {
return "", "", fmt.Errorf("could not create stderr pipe: %w", err)
}

if err := cmd.Start(); err != nil {
return "", "", fmt.Errorf("could not start keep-sorted: %w", err)
}

var errs []error
gotOut, err := io.ReadAll(outPipe)
if err != nil {
errs = append(errs, fmt.Errorf("could not read keep-sorted stdout: %w", err))
}

gotErr, err := io.ReadAll(errPipe)
if err != nil {
errs = append(errs, fmt.Errorf("could not read keep-sorted stderr: %w", err))
}

if err := cmd.Wait(); err != nil {
errs = append(errs, fmt.Errorf("keep-sorted failed: %w", err))
}

return string(gotOut), string(gotErr), errors.Join(errs...)
}
Loading

0 comments on commit 3436ef5

Please sign in to comment.