File: changeset_writer_test.go

package info (click to toggle)
golang-gitea-noerw-unidiff-comments 0.0~git20220822.50f4daa%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 336 kB
  • sloc: makefile: 4
file content (60 lines) | stat: -rw-r--r-- 1,162 bytes parent folder | download
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
package writer

import (
	"bytes"
	"io/ioutil"
	"os"
	"os/exec"
	"testing"

	"gitea.com/noerw/unidiff-comments"
)

func TestWillOmitDanglingWhitespaceOnRender(t *testing.T) {
	changeset := unidiff.Changeset{
		Diffs: []*unidiff.Diff{
			{
				FileComments: unidiff.CommentsTree{
					{
						Text: "\n\nevery dangling whitespace    \n\n" +
							"   should be removed\n\n\n",
					},
				},
			},
		},
	}

	buf := bytes.Buffer{}
	err := WriteChangeset(changeset, &buf)
	if err != nil {
		t.Fatal(err)
	}

	output, err := ioutil.ReadFile(
		"../_test/no_dangling_whitespaces.diff")

	if string(output) != buf.String() {
		t.Log("all dangling whitespaces should be trimmed")
		t.Fatal("\n" + makeDiff(buf.String(), string(output)))
	}
}

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()
}