e2e/blobrender_test.go

v1.26.0
gitbay/e2e/blobrender_test.go history · blame · raw

55 lines · 2478 bytes

 1package e2e
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8)
 9
10// Markdown and org files render on the blob page, with the source one
11// click away; everything else is unchanged.
12func TestBlobRendersMarkup(t *testing.T) {
13	inst := startInstance(t)
14	aliceKey := inst.newKey(t, "alice")
15	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
16	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/docs"); code != 0 {
17		t.Fatal("repo create failed")
18	}
19	work := t.TempDir()
20	env := inst.gitEnv(aliceKey)
21	mustGit(t, work, env, "clone", inst.sshURL("alice/docs"), "w")
22	dir := filepath.Join(work, "w")
23	os.MkdirAll(filepath.Join(dir, "docs"), 0o755)
24	os.WriteFile(filepath.Join(dir, "docs", "notes.md"), []byte("# Field notes\n\nSee [the plan](plan.org).\n"), 0o644)
25	os.WriteFile(filepath.Join(dir, "docs", "plan.org"), []byte("* The plan\n\nA *bold* claim.\n"), 0o644)
26	os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n"), 0o644)
27	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
28	mustGit(t, dir, env, "add", ".")
29	mustGit(t, dir, env, "commit", "-q", "-m", "docs")
30	mustGit(t, dir, env, "push", "-q", "origin", "main")
31
32	status, body := inst.get(t, "/alice/docs/blob/main/docs/notes.md")
33	if status != 200 || !strings.Contains(body, `<h2 id="field-notes">Field notes</h2>`) {
34		t.Fatalf("markdown not rendered: %d\n%s", status, body)
35	}
36	if !strings.Contains(body, `href="?view=source"`) || !strings.Contains(body, "<strong>rendered</strong>") {
37		t.Fatalf("no source toggle:\n%s", body)
38	}
39	// A relative link resolves against the file's directory, as in a README.
40	if !strings.Contains(body, "/alice/docs/blob/main/docs/plan.org") {
41		t.Fatalf("relative link not rewritten:\n%s", body)
42	}
43	_, body = inst.get(t, "/alice/docs/blob/main/docs/notes.md?view=source")
44	if !strings.Contains(body, `class="code"`) || strings.Contains(body, "Field notes</h1>") || !strings.Contains(body, `href="?"`) {
45		t.Fatalf("source view wrong:\n%s", body)
46	}
47	_, body = inst.get(t, "/alice/docs/blob/main/docs/plan.org")
48	if !strings.Contains(body, "The plan") || !strings.Contains(body, "<strong>bold</strong>") || !strings.Contains(body, `href="?view=source"`) {
49		t.Fatalf("org not rendered:\n%s", body)
50	}
51	_, body = inst.get(t, "/alice/docs/blob/main/main.go")
52	if strings.Contains(body, "view=source") || !strings.Contains(body, `class="code"`) {
53		t.Fatalf("plain file gained a toggle or lost its code view:\n%s", body)
54	}
55}