Repository navigation
Expand file tree
/
Copy pathmark_properties_test.go
More file actions
174 lines (140 loc) · 6.08 KB
/
Copy pathmark_properties_test.go
File metadata and controls
174 lines (140 loc) · 6.08 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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"
)
// propertiesFixture publishes one document and returns the server, the page id
// and the config used, so a test can republish.
func propertiesFixture(t *testing.T, doc string, config func(*Config)) (*confluencetest.Server, string, Config) {
t.Helper()
server := confluencetest.New(t)
home := server.AddPage("DOCS", "Home", "page", "")
server.SetHomepage("DOCS", home.ID)
server.AddPage("DOCS", "Parent", "page", home.ID)
dir := t.TempDir()
writeFile(t, dir, "doc.md", doc)
cfg := Config{
BaseURL: server.URL, Username: "user", Password: "token",
Files: filepath.Join(dir, "doc.md"),
Features: []string{"mention"},
Output: io.Discard,
}
if config != nil {
config(&cfg)
}
require.NoError(t, Run(cfg))
api := confluence.NewAPI(server.URL, "user", "token", false)
page, err := api.FindPage("DOCS", "Doc", "page")
require.NoError(t, err)
require.NotNil(t, page)
return server, page.ID, cfg
}
const propertyHeaders = "<!-- Space: DOCS -->\n<!-- Parent: Parent -->\n<!-- Title: Doc -->\n"
// TestPropertyHeaderSetsAContentProperty covers the singular, repeatable header
// form, which matches Parent, Label and Attachment.
func TestPropertyHeaderSetsAContentProperty(t *testing.T) {
server, id, _ := propertiesFixture(t,
propertyHeaders+
"<!-- Property: team=platform -->\n"+
"<!-- Property: reviewed=2026-08 -->\n\nBody.\n", nil)
require.NotNil(t, server.SpaceProperty(id, "team"))
assert.JSONEq(t, `"platform"`, string(server.SpaceProperty(id, "team").Value))
require.NotNil(t, server.SpaceProperty(id, "reviewed"))
assert.JSONEq(t, `"2026-08"`, string(server.SpaceProperty(id, "reviewed").Value))
}
// TestPropertiesFrontMatterSetsProperties covers the plural front matter form,
// which matches parents, labels and attachments -- and which, unlike the
// header, can carry a value that is not a string.
func TestPropertiesFrontMatterSetsProperties(t *testing.T) {
server, id, _ := propertiesFixture(t,
"---\n"+
"title: Doc\n"+
"space: DOCS\n"+
"parents:\n - Parent\n"+
"properties:\n"+
" team: platform\n"+
" reviewers: 3\n"+
" tags:\n - one\n - two\n"+
"---\n\nBody.\n",
func(c *Config) { c.Features = []string{"mention", "frontmatter"} })
require.NotNil(t, server.SpaceProperty(id, "team"))
assert.JSONEq(t, `"platform"`, string(server.SpaceProperty(id, "team").Value))
require.NotNil(t, server.SpaceProperty(id, "reviewers"))
assert.JSONEq(t, `3`, string(server.SpaceProperty(id, "reviewers").Value))
require.NotNil(t, server.SpaceProperty(id, "tags"))
assert.JSONEq(t, `["one","two"]`, string(server.SpaceProperty(id, "tags").Value))
}
func TestGlobalPropertiesApplyToEveryPage(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "properties.yaml")
require.NoError(t, os.WriteFile(path, []byte("owner: docs-team\nsource: git\n"), 0o600))
server, id, _ := propertiesFixture(t, propertyHeaders+"\nBody.\n",
func(c *Config) { c.GlobalProperties = path })
require.NotNil(t, server.SpaceProperty(id, "owner"))
assert.JSONEq(t, `"docs-team"`, string(server.SpaceProperty(id, "owner").Value))
require.NotNil(t, server.SpaceProperty(id, "source"))
}
// TestDocumentPropertyWinsOverGlobal: a document naming a property is being
// specific about its own page, which is the more particular statement.
func TestDocumentPropertyWinsOverGlobal(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "properties.json")
require.NoError(t, os.WriteFile(path, []byte(`{"owner": "docs-team"}`), 0o600))
server, id, _ := propertiesFixture(t,
propertyHeaders+"<!-- Property: owner=platform-team -->\n\nBody.\n",
func(c *Config) { c.GlobalProperties = path })
assert.JSONEq(t, `"platform-team"`, string(server.SpaceProperty(id, "owner").Value))
}
// TestPropertyUnchangedIsNotRewritten: a property holds a version Confluence
// increments on every write, so rewriting an unchanged value would fill its
// history the way republishing an unchanged page fills the page's.
func TestPropertyUnchangedIsNotRewritten(t *testing.T) {
server, id, config := propertiesFixture(t,
propertyHeaders+"<!-- Property: team=platform -->\n\nBody.\n", nil)
first := server.SpaceProperty(id, "team").Version
require.NoError(t, Run(config))
assert.Equal(t, first, server.SpaceProperty(id, "team").Version,
"an unchanged property must not be written again")
}
func TestPropertyChangedIsRewritten(t *testing.T) {
server, id, config := propertiesFixture(t,
propertyHeaders+"<!-- Property: team=platform -->\n\nBody.\n", nil)
first := server.SpaceProperty(id, "team").Version
writeFile(t, filepath.Dir(config.Files), "doc.md",
propertyHeaders+"<!-- Property: team=infrastructure -->\n\nBody.\n")
require.NoError(t, Run(config))
assert.Greater(t, server.SpaceProperty(id, "team").Version, first)
assert.JSONEq(t, `"infrastructure"`, string(server.SpaceProperty(id, "team").Value))
}
func TestPropertyHeaderMustBeKeyValue(t *testing.T) {
server := confluencetest.New(t)
home := server.AddPage("DOCS", "Home", "page", "")
server.SetHomepage("DOCS", home.ID)
dir := t.TempDir()
writeFile(t, dir, "doc.md",
"<!-- Space: DOCS -->\n<!-- Title: Doc -->\n<!-- Property: nonsense -->\n\nBody.\n")
err := Run(Config{
BaseURL: server.URL, Username: "user", Password: "token",
Files: filepath.Join(dir, "doc.md"), Output: io.Discard,
})
require.Error(t, err)
assert.Contains(t, err.Error(), "key=value")
}
func TestGlobalPropertiesFileMustParse(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "properties.yaml")
require.NoError(t, os.WriteFile(path, []byte("owner: [unclosed\n"), 0o600))
writeFile(t, dir, "doc.md", "<!-- Space: DOCS -->\n<!-- Title: Doc -->\n\nBody.\n")
err := Run(Config{
BaseURL: "http://127.0.0.1:1", Username: "user", Password: "token",
Files: filepath.Join(dir, "*.md"), GlobalProperties: path, Output: io.Discard,
})
require.Error(t, err)
assert.Contains(t, err.Error(), "properties file")
}