Repository navigation
Expand file tree
/
Copy pathpandoc_test.go
More file actions
156 lines (145 loc) · 6.15 KB
/
Copy pathpandoc_test.go
File metadata and controls
156 lines (145 loc) · 6.15 KB
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
package main
import (
"bytes"
"context"
"image"
"image/color"
"image/png"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"github.com/ftrain/mouseparty/apps"
)
func TestTLOOf(t *testing.T) {
kept := map[string]bool{"./media/a.png": true}
keep := func(src string) (apps.Picture, bool) {
return apps.Picture{Ref: "asset:0123456789abcdef", W: 640, H: 360}, kept[src]
}
md := "---\ntitle: \"Vertigo, a \\\"reading\\\"\"\nauthor: A student\n---\n\n# The tower\n\n" +
"Before  after.\n\nThe \"dolly\" zoom\n\n" +
"\n\n| Shot |  |\n|---|---|\n\n" +
"------------------------------------------------------------------------\n\n## Part two\n"
got := tloOf(md, "write", keep)
want := "# Vertigo, a \"reading\" {.title}\n\n*A student*\n\n# The tower\n\nBefore after.\n\n" +
"```image {alt=\"The 'dolly' zoom\" w=640 h=360}\nasset:0123456789abcdef\n```\n\n" +
"From the web\n\n| Shot | x |\n|---|---|\n\n--- {.rule}\n\n## Part two\n"
if got != want {
t.Errorf("write:\n%s\nwant:\n%s", got, want)
}
deck := tloOf("## One\n\nWords\n\n------\n\n# Two\n\n- a\n", "deck", keep)
if want := "## One\n\nWords\n\n---\n\n## Two\n\n- a\n"; deck != want {
t.Errorf("deck:\n%s\nwant:\n%s", deck, want)
}
}
// A picture a document names is taken only from what pandoc wrote: a file in its own folder,
// not a link out of it, a folder, a device or anything too big to be a picture
func TestPicturesComeOnlyFromPandocsFolder(t *testing.T) {
dir := t.TempDir()
outside := filepath.Join(t.TempDir(), "secret.png")
os.WriteFile(outside, []byte("\x89PNG secret"), 0o600)
os.MkdirAll(filepath.Join(dir, "media", "folder.png"), 0o700)
os.WriteFile(filepath.Join(dir, "media", "a.png"), []byte("\x89PNG a"), 0o600)
big, _ := os.Create(filepath.Join(dir, "media", "big.png"))
big.Truncate(pictureMaxBytes + 1)
big.Close()
linked := os.Symlink(outside, filepath.Join(dir, "media", "link.png")) == nil
root, err := os.OpenRoot(dir)
if err != nil {
t.Fatal(err)
}
defer root.Close()
for _, src := range []string{"./media/a.png", "/media/a.png", "media/a.png", `media\a.png`, "../media/a.png", "../../media/a.png"} {
if b, ok := extracted(root, src); !ok || string(b) != "\x89PNG a" {
t.Errorf("%q: %q %v", src, b, ok)
}
}
for _, src := range []string{"", ".", "media/folder.png", "media/big.png", "media/none.png", "COM1", "CON", outside,
"../" + filepath.Base(filepath.Dir(outside)) + "/secret.png"} {
if _, ok := extracted(root, src); ok {
t.Errorf("%q was read", src)
}
}
if _, ok := extracted(root, "media/link.png"); linked && ok {
t.Error("a link out of the folder was followed")
}
}
// a document made by pandoc itself (so nothing but pandoc.wasm is needed), read back
func TestReadDocument(t *testing.T) {
h := &Host{pics: newPictures(nil), pandoc: newWasmTool("pandoc"), reading: make(chan struct{}, 1)}
if !h.pandoc.ok() {
t.Skip("no pandoc.wasm: scripts/fetch_pandoc.sh")
}
was := wasmCacheDir
wasmCacheDir = func() string { abs, _ := filepath.Abs("build/wasm-cache"); return abs }
defer func() { wasmCacheDir = was; h.pandoc.close() }()
dir := t.TempDir()
img := image.NewRGBA(image.Rect(0, 0, 320, 180))
for i := range img.Pix {
img.Pix[i] = uint8(i)
}
img.Set(0, 0, color.White)
var pngBytes bytes.Buffer
png.Encode(&pngBytes, img)
os.WriteFile(filepath.Join(dir, "frame.png"), pngBytes.Bytes(), 0o600)
os.WriteFile(filepath.Join(dir, "in.md"), []byte("---\ntitle: Vertigo\n---\n\n# The tower\n\nScottie *climbs*.\n\n"+
"\n\n| Shot | Time |\n|---|---|\n| CU | 1:02 |\n\n---\n\n# The bell\n\nMadeleine.\n"), 0o600)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
made := map[string][]byte{}
for _, f := range []string{"essay.docx", "talk.pptx"} {
var stderr bytes.Buffer
if err := h.pandoc.run(ctx, dir, nil, &stderr, "/in.md", "-o", "/"+f); err != nil {
t.Fatalf("making %s: %v %s", f, err, stderr.String())
}
made[f], _ = os.ReadFile(filepath.Join(dir, f))
}
doc, err := h.readDocument(ctx, "essay.docx", made["essay.docx"], "write")
if err != nil {
t.Fatal(err)
}
for _, want := range []string{"# Vertigo {.title}", "# The tower", "Scottie *climbs*.", "Shot | Time", "--- {.rule}", "# The bell"} {
if !strings.Contains(doc, want) {
t.Errorf("the document has no %q:\n%s", want, doc)
}
}
fence := regexp.MustCompile("```image \\{alt=\"The dolly zoom\" w=320 h=180\\}\nasset:([0-9a-f]{16})\n```").FindStringSubmatch(doc)
if fence == nil {
t.Fatalf("no picture fence:\n%s", doc)
}
if pic, ok := h.pics.get(fence[1]); !ok || !bytes.Equal(pic.b, pngBytes.Bytes()) && pic.mime != "image/png" {
t.Errorf("the picture isn't kept as it was")
}
if strings.Contains(doc, "\nThe dolly zoom\n") || strings.Contains(doc, "](") {
t.Errorf("a caption again, or a link left:\n%s", doc)
}
deck, err := h.readDocument(ctx, "talk.pptx", made["talk.pptx"], "deck")
if err != nil {
t.Fatal(err)
}
if strings.Count(deck, "\n---\n") < 2 || !strings.Contains(deck, "## The tower") || !strings.Contains(deck, "asset:") {
t.Errorf("not slides:\n%s", deck)
}
sheet, err := h.readDocument(ctx, "shots.csv", []byte("Shot,Time,Note\nCU,1:02,\"Madeleine, at the bell\"\nLS,=SUM(1),x\n"), "sheet")
if want := "Shot | Time | Note\n--- | --- | ---\nCU | 1:02 | Madeleine, at the bell\nLS | '=SUM(1) | x\n"; err != nil || sheet != want {
t.Errorf("csv into a sheet: %v\n%s", err, sheet)
}
if _, err := h.readDocument(ctx, "x.exe", []byte("MZ"), "write"); err == nil {
t.Error("an .exe was read")
}
if _, err := h.readDocument(ctx, "broken.docx", []byte("not a zip"), "write"); err == nil {
t.Error("a broken .docx was read")
}
}
func TestSheetOf(t *testing.T) {
md := "# Shots\n\n| Shot | Time | Note |\n|:-----|-----:|------|\n| CU | 1:02 | a \\| b |\n| LS | =1+1 | 'quoted |\n\n| Other | Table |\n|---|---|\n| x | y |\n"
want := "Shot | Time | Note\n--- | --- | ---\nCU | 1:02 | a \\| b\nLS | '=1+1 | ''quoted\n"
if got := tloOf(md, "sheet", nil); got != want {
t.Errorf("got:\n%s\nwant:\n%s", got, want)
}
if tloOf("no table here\n", "sheet", nil) != "" {
t.Error("a sheet from no table")
}
}