1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
package unidiff
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"gitea.com/noerw/unidiff-comments/types"
"gitea.com/noerw/unidiff-comments/writer"
)
type diffTest struct {
name string
in []byte
out []byte
err []byte
}
func TestReadChangeset(t *testing.T) {
for _, testCase := range getParseTests("_test") {
expected := string(testCase.out)
changeset, err := ReadChangeset(bytes.NewBuffer(testCase.in))
if err != nil {
if err.Error() != strings.TrimSpace(string(testCase.err)) {
t.Logf("while testing on `%s`\n", testCase.name)
t.Fatal(err)
} else {
continue
}
}
buf := &bytes.Buffer{}
err = writer.WriteChangeset(changeset, buf)
if err != nil {
panic(err)
}
actual := buf.String()
if actual != expected {
t.Logf("while testing on `%s`\n", testCase.name)
t.Logf("expected:\n%#v", expected)
t.Logf("actual:\n%#v", actual)
t.Logf("diff:\n%v", makeDiff(actual, expected))
t.FailNow()
}
}
}
func TestNewCommentForLine(t *testing.T) {
tests := []struct {
file string
expected types.CommentAnchor
}{
{"_test/new_comment_to_add.diff",
types.CommentAnchor{
Line: 3, LineType: types.SegmentTypeAdded, Path: "/tmp/b"}},
{"_test/new_comment_to_del.diff",
types.CommentAnchor{
Line: 3,
LineType: types.SegmentTypeRemoved,
Path: "/tmp/a",
}},
{"_test/new_comment_to_ctx_with_add.diff",
types.CommentAnchor{
Line: 3,
LineType: types.SegmentTypeContext,
Path: "/tmp/a",
}},
{"_test/new_comment_to_ctx_with_add_del.diff",
types.CommentAnchor{
Line: 5,
LineType: types.SegmentTypeContext,
Path: "/tmp/a",
}},
}
for _, test := range tests {
changesetText, err := ioutil.ReadFile(test.file)
if err != nil {
t.Fatal(err)
}
changeset, err := ReadChangeset(bytes.NewBuffer(changesetText))
if err != nil {
t.Fatal(err)
}
if len(changeset.Diffs) != 1 {
t.Fatal("unexpected number of changeset")
t.FailNow()
}
comment := changeset.Diffs[0].LineComments[0]
if comment.Anchor.Line != test.expected.Line {
t.Fatal("comment binded to incorrect line")
t.FailNow()
}
if comment.Anchor.LineType != test.expected.LineType {
t.Fatal("unexpected line type")
t.FailNow()
}
if comment.Anchor.Path != test.expected.Path {
t.Fatal("unexpected path", comment.Anchor.Path)
t.FailNow()
}
}
}
func TestDoNotAddNestedCommentsToLineComments(t *testing.T) {
changesetText, err := ioutil.ReadFile("_test/with_one_nested_comment.diff")
if err != nil {
t.Fatal(err)
}
changeset, err := ReadChangeset(bytes.NewBuffer(changesetText))
if err != nil {
t.Fatal(err)
}
if len(changeset.Diffs[0].LineComments) != 1 {
t.Fatal("expected only one line comment")
}
}
func getParseTests(dir string) []*diffTest {
diffTests := make(map[string]*diffTest)
diffTestsList := make([]*diffTest, 0)
reChangesetTest := regexp.MustCompile(`/([^/]+)\.(in|out|err)\.diff$`)
filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if !reChangesetTest.MatchString(path) {
return nil
}
matches := reChangesetTest.FindStringSubmatch(path)
caseName := matches[1]
if _, exist := diffTests[caseName]; !exist {
diffTests[caseName] = &diffTest{}
}
diffTests[caseName].name = caseName
var target *[]byte
switch matches[2] {
case "in":
target = &diffTests[caseName].in
case "out":
target = &diffTests[caseName].out
case "err":
target = &diffTests[caseName].err
}
*target, _ = ioutil.ReadFile(path)
diffTestsList = append(diffTestsList, diffTests[caseName])
return nil
})
for _, val := range diffTests {
if val.out == nil {
val.out = val.in
}
}
return diffTestsList
}
func makeDiff(actual, expected string) string {
a, _ := ioutil.TempFile(os.TempDir(), "actual")
defer func() {
os.Remove(a.Name())
}()
b, _ := ioutil.TempFile(os.TempDir(), "expected")
defer func() {
os.Remove(b.Name())
}()
a.WriteString(actual)
b.WriteString(expected)
cmd := exec.Command("diff", "-u", b.Name(), a.Name())
buf := bytes.NewBuffer([]byte{})
cmd.Stdout = buf
cmd.Run()
return buf.String()
}
|