Repository navigation
Expand file tree
/
Copy pathmark_encoding_test.go
More file actions
103 lines (87 loc) · 3.77 KB
/
Copy pathmark_encoding_test.go
File metadata and controls
103 lines (87 loc) · 3.77 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
package mark
import (
"io"
"os"
"path/filepath"
"testing"
"github.com/kovetskiy/mark/v16/confluence"
"github.com/kovetskiy/mark/v16/confluence/confluencetest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// writeWithBOM writes a document behind a UTF-8 byte-order mark, the way a
// Windows editor set to "UTF-8 with BOM" does.
func writeWithBOM(t *testing.T, dir, name, content string) string {
t.Helper()
path := filepath.Join(dir, name)
require.NoError(t, os.WriteFile(path, append([]byte{0xEF, 0xBB, 0xBF}, content...), 0o600))
return path
}
// storedBody returns what is actually stored on the page carrying a title.
func storedBody(t *testing.T, server *confluencetest.Server, title string) string {
t.Helper()
found, err := confluence.NewAPI(server.URL, "user", "token", false).
FindPage("DOCS", title, "page")
require.NoError(t, err)
if found == nil {
return ""
}
return server.Page(found.ID).Body
}
// TestByteOrderMarkDoesNotHideMetadata: a BOM in front of the first header
// comment made every parser here see a file with no metadata at all, and the
// run failed with "doesn't contain metadata" -- pointing away from the headers,
// which are present and correct. "UTF-8 with BOM" is a one-click setting in VS
// Code, and PowerShell redirection produces one without being asked.
func TestByteOrderMarkDoesNotHideMetadata(t *testing.T) {
server, _ := docsSpace(t)
dir := t.TempDir()
path := writeWithBOM(t, dir, "doc.md",
"<!-- Space: DOCS -->\n<!-- Parent: Parent -->\n<!-- Title: With BOM -->\n\nBody text.\n")
require.NoError(t, Run(Config{
BaseURL: server.URL, Username: "user", Password: "token",
Files: path, Features: []string{"mention"}, Output: io.Discard,
}))
assert.Equal(t, 1, countPagesTitled(t, server, "With BOM"))
assert.NotContains(t, storedBody(t, server, "With BOM"), "\ufeff",
"the mark itself must not reach the page")
}
// TestByteOrderMarkDoesNotHideFrontMatter covers the other metadata form. The
// BOM sits in front of the opening "---", so it stops being a front-matter
// fence and becomes a horizontal rule with the YAML published beneath it as
// body text.
func TestByteOrderMarkDoesNotHideFrontMatter(t *testing.T) {
server, _ := docsSpace(t)
dir := t.TempDir()
path := writeWithBOM(t, dir, "doc.md",
"---\nspace: DOCS\ntitle: BOM Front Matter\nparents: [ Parent ]\n---\n\nBody text.\n")
require.NoError(t, Run(Config{
BaseURL: server.URL, Username: "user", Password: "token",
Files: path, Features: []string{"mention", "frontmatter"}, Output: io.Discard,
}))
assert.Equal(t, 1, countPagesTitled(t, server, "BOM Front Matter"))
assert.NotContains(t, storedBody(t, server, "BOM Front Matter"), "space: DOCS",
"the front matter must be read as metadata, not published as text")
}
// TestLinkToDocumentWithByteOrderMark: the document being published had its
// BOM stripped, a document it links to did not. The linked file's headers
// were invisible behind the mark, so the link was never rewritten to the page
// and was published pointing at other.md.
func TestLinkToDocumentWithByteOrderMark(t *testing.T) {
server, _ := docsSpace(t)
dir := t.TempDir()
header := "<!-- Space: DOCS -->\n<!-- Parent: Parent -->\n"
other := writeWithBOM(t, dir, "other.md", header+"<!-- Title: Other -->\n\nx\n")
doc := writeFile(t, dir, "doc.md", header+"<!-- Title: Doc -->\n\nSee [other](other.md).\n")
config := Config{
BaseURL: server.URL, Username: "user", Password: "token",
Features: []string{"mention"}, Output: io.Discard,
}
config.Files = other
require.NoError(t, Run(config))
config.Files = doc
require.NoError(t, Run(config))
body := storedBody(t, server, "Doc")
assert.NotContains(t, body, `href="other.md"`)
assert.Contains(t, body, `href="/wiki/x/`, "rewritten to the page it was published as")
}