Skip to content

Commit

Permalink
chore: better test structure for diff generation (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanistan committed Jul 14, 2023
1 parent 86ffe28 commit 325a64e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 37 deletions.
30 changes: 30 additions & 0 deletions server/internal/github/diff/hunk_meta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package diff

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestParseHunkMeta(t *testing.T) {
t.Run("first case", func(t *testing.T) {
meta, err := ParseHunkMeta("@@ -230,6 +200,9 @@ if (!defined $initial_reply_to && $prompting) {")
require.NoError(t, err)
require.Equal(t, HunkMeta{
Original: HunkRange{StartingAt: 230, NumLines: 6, IgnorePrefix: "+"},
New: HunkRange{StartingAt: 200, NumLines: 9, IgnorePrefix: "-"},
}, meta)
})
t.Run("succeeds", func(t *testing.T) {
meta, err := ParseHunkMeta("@@ -0,6 +200,9 @@ if (!defined $initial_reply_to && $prompting) {")
require.NoError(t, err)
require.Equal(t, HunkMeta{
Original: HunkRange{StartingAt: 0, NumLines: 6, IgnorePrefix: "+"},
New: HunkRange{StartingAt: 200, NumLines: 9, IgnorePrefix: "-"},
}, meta)
})
t.Run("errs", func(t *testing.T) {
_, err := ParseHunkMeta("@@ -0,6 +a,9 @@ if (!defined $initial_reply_to && $prompting) {")
require.Error(t, err)
})
}
77 changes: 41 additions & 36 deletions server/internal/github/diff_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
package github

import (
_ "embed"
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/stanistan/present-me/internal/github/diff"
"github.com/stretchr/testify/require"
)

func TestParseDiffHunkPrefix(t *testing.T) {
t.Run("first case", func(t *testing.T) {
meta, err := diff.ParseHunkMeta("@@ -230,6 +200,9 @@ if (!defined $initial_reply_to && $prompting) {")
require.NoError(t, err)
require.Equal(t, diff.HunkMeta{
Original: diff.HunkRange{StartingAt: 230, NumLines: 6, IgnorePrefix: "+"},
New: diff.HunkRange{StartingAt: 200, NumLines: 9, IgnorePrefix: "-"},
}, meta)
})
t.Run("succeeds", func(t *testing.T) {
meta, err := diff.ParseHunkMeta("@@ -0,6 +200,9 @@ if (!defined $initial_reply_to && $prompting) {")
require.NoError(t, err)
require.Equal(t, diff.HunkMeta{
Original: diff.HunkRange{StartingAt: 0, NumLines: 6, IgnorePrefix: "+"},
New: diff.HunkRange{StartingAt: 200, NumLines: 9, IgnorePrefix: "-"},
}, meta)
})
t.Run("errs", func(t *testing.T) {
_, err := diff.ParseHunkMeta("@@ -0,6 +a,9 @@ if (!defined $initial_reply_to && $prompting) {")
require.Error(t, err)
})
const testDataDir = "testdata"

func TestDiffGenerator(t *testing.T) {
testpaths, err := filepath.Glob(filepath.Join(testDataDir, "*.json"))
require.NoError(t, err)

for _, testpath := range testpaths {
_, filename := filepath.Split(testpath)
test := filename[:len(filename)-len(filepath.Ext(testpath))]

t.Run(test, func(t *testing.T) {
var (
data = readFile(t, testpath)
input = diffFile(t, test, "input")
expected = diffFile(t, test, "out")

comment PullRequestComment
)

err = json.Unmarshal(data, &comment)
require.NoError(t, err, "test data file is an invalid json PullRequestComment")
comment.DiffHunk = &input

diff, err := generateDiff(&comment)
require.NoError(t, err, "failed to generate diff")
require.Equal(t, expected, diff+"\n", "generated diff doesn't match expected output")
})
}
}

//go:embed test_hunk.diff
var diffHunk string
func diffFile(t *testing.T, name, suffix string) string {
t.Helper()
bs := readFile(t, filepath.Join(testDataDir, name+"."+suffix+".diff"))
return string(bs)
}

func TestParser(t *testing.T) {
right := "RIGHT"
line := 185
comment := &PullRequestComment{DiffHunk: &diffHunk, Side: &right, Line: &line}
diff, err := generateDiff(comment)
require.NoError(t, err)
require.Equal(t, ` type serviceContext struct {
- Management ManagementType
- Actions *util.StringSet
+ Actions *util.StringSet
}`, diff)
func readFile(t *testing.T, path string) []byte {
t.Helper()
bs, err := os.ReadFile(path)
require.NoError(t, err, "could not read required test file")
return bs
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@@ -185,28 +180,8 @@ func (d *SyncIstio) Run(ctx context.Context) error {
return nil
}

-type ManagementType string
-
-const (
Expand Down
1 change: 1 addition & 0 deletions server/internal/github/testdata/implicit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "side": "RIGHT", "line": 185 }
5 changes: 5 additions & 0 deletions server/internal/github/testdata/implicit.out.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type serviceContext struct {
- Management ManagementType
- Actions *util.StringSet
+ Actions *util.StringSet
}

0 comments on commit 325a64e

Please sign in to comment.