git@cosmocr.at/gitweb.git @ HEAD · summary refs log
HEAD / internal / render / diff_test.go /
internal/render/diff_test.go · 1.4 KiB · raw
view: Source ☐ force
 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
package render

import (
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"

	"github.com/go-git/go-git/v5"
)

func TestDiffRendersChangeClasses(t *testing.T) {
	if _, err := exec.LookPath("git"); err != nil {
		t.Skip("git not available")
	}
	work := t.TempDir()
	run := func(args ...string) {
		t.Helper()
		cmd := exec.Command("git", args...)
		cmd.Dir = work
		cmd.Env = append(os.Environ(),
			"GIT_AUTHOR_NAME=T", "GIT_AUTHOR_EMAIL=t@e.com",
			"GIT_COMMITTER_NAME=T", "GIT_COMMITTER_EMAIL=t@e.com")
		if out, err := cmd.CombinedOutput(); err != nil {
			t.Fatalf("git %v: %v\n%s", args, err, out)
		}
	}
	run("init", "-b", "main")
	os.WriteFile(filepath.Join(work, "f.txt"), []byte("one\ntwo\n"), 0o644)
	run("add", ".")
	run("commit", "-m", "first")
	os.WriteFile(filepath.Join(work, "f.txt"), []byte("one\nthree\n"), 0o644)
	run("commit", "-am", "second")

	repo, err := git.PlainOpen(work)
	if err != nil {
		t.Fatal(err)
	}
	head, _ := repo.Head()
	child, _ := repo.CommitObject(head.Hash())
	parent, _ := child.Parent(0)
	patch, err := parent.Patch(child)
	if err != nil {
		t.Fatal(err)
	}

	html := string(Diff(patch))
	for _, want := range []string{"diff-add", "diff-del", "file-head", "f.txt"} {
		if !strings.Contains(html, want) {
			t.Fatalf("diff html missing %q:\n%s", want, html)
		}
	}
	if strings.Contains(html, "style=") {
		t.Fatal("unexpected inline style in diff output")
	}
}