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