-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: better test structure for diff generation (#92)
- Loading branch information
Showing
5 changed files
with
78 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
2 changes: 1 addition & 1 deletion
2
server/internal/github/test_hunk.diff → ...ernal/github/testdata/implicit.input.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "side": "RIGHT", "line": 185 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |