"""Test Document Renderer. The manifest is stood in for here rather than built, since what is being checked is what the renderer does with a document and not how the documentation as a whole is put together.""" #################################################################################################################################### from harness.test import * from common.error import * from common.var_store import VarStore from common.xml import xml_node_child, xml_node_normalize, xml_parse from command.render.render import DocRender, child_list, content_list #################################################################################################################################### class _Manifest: """Stands in for the documentation as a whole, which a renderer only asks a few things of.""" def __init__(self, source_map=None, out_list=None, require_list=None, pre=False): self.var_store = VarStore() self.source_map = source_map or {} self.out_list = ["index", "user-guide"] if out_list is None else out_list self.require_list = require_list or [] self.pre = pre self.page_anchor_map = {} self.link_list = [] def evaluate_if(self, node): from common.eval import eval_expression from common.xml import xml_node_attribute expression = xml_node_attribute(node, "if") return True if expression is None else eval_expression(self.var_store.replace_str(expression)) def source_get(self, key): return _Source(self.source_map[key]) def render_out_get(self, type, key): return _Out() if key in self.out_list else None #################################################################################################################################### class _Source: """Stands in for a document of the documentation.""" def __init__(self, root): self.root = root self.cache_list = None #################################################################################################################################### class _Out: """Stands in for a page of the documentation.""" def __init__(self): self.source = "user-guide" self.file = None self.menu = None #################################################################################################################################### def _doc(content): """Parse a document the way the tool reads one.""" result = xml_parse(content, "test.xml") xml_node_normalize(result) return result #################################################################################################################################### def _render(type, content, manifest=None, exe=False): """Build a renderer for a document, which is where filtering and the section map happen.""" root = _doc(content) manifest = _Manifest() if manifest is None else manifest manifest.source_map["user-guide"] = root return DocRender(type, manifest, exe, "user-guide") #################################################################################################################################### def test_child_list(): """A child that holds nothing but text and says nothing about itself is a property of the node rather than part of it.""" node = _doc( "ls

Text.

Titlet
" ) # The command and the user are properties, and the empty text tags are content however little they hold assert_equal([child.tag for child in child_list(node)], ["p", "title", "host-add"]) # Text is never content of the node holding it, since it is the text of that node rather than a child assert_equal([child.tag for child in child_list(_doc("
t

p

"))], ["p"]) #################################################################################################################################### def test_content_list(): """Text and markup come back in the order they were written, since that is the order they are rendered in.""" node = _doc("

Run pgbackrest as root.

") assert_equal([item if isinstance(item, str) else item.tag for item in content_list(node)], ["Run ", "id", " as ", "b", "."]) # A node that holds nothing holds nothing rather than an empty run of text assert_equal(content_list(_doc("

")), []) #################################################################################################################################### def test_process_text(): """Text is rendered with the markup each output type writes for a tag.""" render = DocRender("html") assert_equal( render.process_text(_doc("

Run pgbackrest as root.

")), 'Run pgbackrest as root.', ) assert_equal(DocRender("markdown").process_text(_doc("

Run x.

")), "Run `x`.") assert_equal(DocRender("text").process_text(_doc("

Run x.

")), "Run x.") # A run of spaces is how the xml is laid out to be read rather than something to render assert_equal(render.process_text(_doc("

a b

")), "a b") # Text that runs over more than one line is layout as well, so it is dropped rather than rendered assert_equal(render.process_text(_doc("

a\n b

")), "b") # A quote has a tag of its own, so a quote in the text would be markup the renderer cannot see with assert_raises(ToolError) as raised: render.process_text(_doc('

say "no"

')) assert_in("unable to process quotes in string", str(raised.exception)) #################################################################################################################################### def test_process_text_type(): """Each output type says what it has to about the text it writes.""" # A page separates paragraphs itself, since the help is rendered to html and then put into a page rather than rendered in one assert_equal(DocRender("html").process_text(_doc("

one

two

")), "one
\n
\ntwo") assert_equal(DocRender("html").process_text(_doc("

one

two
")), "one
\ntwo") # A word that a page would rather not say assert_equal(DocRender("html").process_text(_doc("

master

")), "master") # Console text has no markup to escape into, so what a document escaped is written out assert_equal(DocRender("text").process_text(_doc("

a — b < c ≥ d

")), "a -- b < c >= d") #################################################################################################################################### def test_process_tag(): """A tag renders as what the output type writes for it, and a tag the output type has no markup for is an error.""" render = DocRender("html") # A tag that holds text renders the text, and a tag that holds markup renders the markup assert_equal(render.process_tag(_doc("x")), 'x') assert_equal(render.process_tag(_doc("

x

")), "x") # An admonition says what kind it is before it says anything else assert_equal( render.process_tag(_doc("Careful.")), '
NOTE:
Careful.
', ) assert_equal(DocRender("markdown").process_tag(_doc("Careful.")), "\n> **NOTE: Careful.\n") # A tag that holds markup and no text of its own renders what it holds assert_equal(render.process_tag(_doc("x")), "x") with assert_raises(ToolError) as raised: DocRender("markdown").process_tag(_doc("x")) assert_equal(str(raised.exception), "invalid type markdown or tag user") #################################################################################################################################### def test_tag_set(): """What a renderer writes for a tag can be set, which is how the release notes name the project rather than refer to it.""" render = DocRender("text") assert_equal(render.process_text(_doc("

works.

")), "{[project]} works.") render.tag_set("backrest", "pgBackRest") assert_equal(render.process_text(_doc("

works.

")), "pgBackRest works.") # Setting a tag applies to this renderer rather than to every renderer of the type assert_equal(DocRender("text").process_text(_doc("

works.

")), "{[project]} works.") #################################################################################################################################### def test_variable_replace(): """A renderer with no documentation to draw on writes what it was given, since there is nothing to replace it with.""" assert_equal(DocRender("text").variable_replace("{[project]}"), "{[project]}") assert_is_none(DocRender("text").variable_replace(None)) manifest = _Manifest() manifest.var_store.add("project", "pgBackRest") assert_equal( _render("text", "
T
", manifest).variable_replace("{[project]}"), "pgBackRest", ) #################################################################################################################################### def test_build_section(): """A section knows where it sits, which is what a link to it and the sections that depend on it are written against.""" render = _render( "html", """
One
Two
Three
""", ) assert_equal(sorted(render.section_map), ["/one", "/one/two", "/three"]) # A section depends on the section before it unless it says otherwise, since the documentation is read in order from common.xml import xml_node_attribute assert_equal(xml_node_attribute(render.section_map["/three"], "depend"), "/one") assert_is_none(xml_node_attribute(render.section_map["/one"], "depend")) assert_equal(xml_node_attribute(render.section_map["/one/two"], "path-parent"), "/one") #################################################################################################################################### def test_build_depend(): """A depend is written relative to the section holding it, and one that points nowhere is a mistake in the document.""" render = _render( "html", """
One
A
B
Two
""", ) from common.xml import xml_node_attribute assert_equal(xml_node_attribute(render.section_map["/one/b"], "depend"), "/one/a") assert_equal(xml_node_attribute(render.section_map["/two"], "depend"), "/one") with assert_raises(ToolError) as raised: _render( "html", "
One
" "
Two
", ) assert_equal(str(raised.exception), "section 'two' depend '/missing' is not valid") #################################################################################################################################### def test_build_filter(): """A section whose condition does not hold is not part of this build of the document.""" manifest = _Manifest() manifest.var_store.add("mode", "release") render = _render( "html", """
One
Two
Three
""", manifest, ) assert_equal(sorted(render.section_map), ["/one", "/three"]) #################################################################################################################################### def test_build_source(): """A section that takes its content from another document takes its title too, and renames the sections it takes.""" manifest = _Manifest() manifest.source_map["other"] = _doc( "
Inner" "

See this.

" ) render = _render("html", "
", manifest) assert_equal(sorted(render.section_map), ["/outer", "/outer/inner"]) assert_equal(render.process_text(xml_node_child(render.section_map["/outer"], "title")), "Other") # A link to a section that was taken points at where the section is now rather than where it was written assert_in('href="#outer/inner"', render.process_text(xml_node_child(render.section_map["/outer/inner"], "p"))) # A link that points somewhere else is left alone, since there is no section of it to move manifest = _Manifest() manifest.source_map["other"] = _doc( "
Inner" "

See this.

" ) render = _render("html", "
", manifest) assert_in('href="https://x"', render.process_text(xml_node_child(render.section_map["/outer/inner"], "p"))) with assert_raises(ToolError) as raised: manifest = _Manifest() manifest.source_map["other"] = _doc("") _render("html", "
Mine
", manifest) assert_equal(str(raised.exception), "cannot specify title in section that sources another document") #################################################################################################################################### def test_build_pre(): """A command marked pre is run while the image for the host is built, so it is skipped when the host is up.""" manifest = _Manifest(pre=True) manifest.var_store.add("host-repo", "repo") render = _render( "html", """
One Setup apt-get update ls
""", manifest, exe=True, ) from common.xml import xml_node_attribute, xml_node_field assert_equal([xml_node_field(node, "exe-cmd") for node in render.pre_execute("repo")], ["apt-get update"]) assert_equal(render.pre_execute("missing"), []) # A section that runs commands is worth logging, which keeps the log to the sections that take a while assert_equal(xml_node_attribute(render.section_map["/one"], "log"), "y") #################################################################################################################################### def test_require(): """A build of part of a document renders the sections that were asked for and the sections they need.""" document = """
One
Two
Inner
Three
""" # A section brings the sections it holds and the sections it depends on, which for these is the section before it render = _render("html", document, _Manifest(require_list=["/two"])) assert_true(render.is_required(render.section_map["/two"])) assert_true(render.is_required(render.section_map["/two/inner"])) assert_true(render.is_required(render.section_map["/one"])) assert_false(render.is_required(render.section_map["/three"])) # Everything is required when nothing was asked for assert_true(_render("html", document).is_required(_render("html", document).section_map["/three"])) # A section inside another brings the section holding it for what that section does rather than for what it says render = _render("html", document, _Manifest(require_list=["/two/inner"])) assert_true(render.is_required(render.section_map["/two/inner"])) assert_true(render.is_required(render.section_map["/one"])) assert_false(render.is_required(render.section_map["/two"])) # A section named twice is added once render = _render("html", document, _Manifest(require_list=["/two", "/two/inner"])) assert_true(render.is_required(render.section_map["/two/inner"])) # A section inside the first section of a document has nothing before it to depend on, so only what holds it is needed render = _render( "html", """
One
Inner
Two
""", _Manifest(require_list=["/one/inner"]), ) assert_true(render.is_required(render.section_map["/one/inner"])) assert_false(render.is_required(render.section_map["/one"])) assert_false(render.is_required(render.section_map["/two"])) with assert_raises(ToolError) as raised: _render("html", document, _Manifest(require_list=["two"])) assert_equal(str(raised.exception), "path two must begin with a /") with assert_raises(ToolError) as raised: _render("html", document, _Manifest(require_list=["/missing"])) assert_equal(str(raised.exception), "required section '/missing' does not exist") #################################################################################################################################### def test_anchor_map(): """An anchor is the id of a section prefixed with the anchors of the sections holding it.""" manifest = _Manifest() _render( "html", """
One
Two
Free
""", manifest, ) # A section that says it does not take the anchor of the section holding it keeps its own assert_equal(sorted(manifest.page_anchor_map["user-guide"]), ["free", "one", "one/two"]) #################################################################################################################################### def test_toc(): """A document says whether it has a table of contents.""" assert_true(_render("html", "
A
").toc) assert_false(_render("html", "
A
").toc) #################################################################################################################################### def test_link_url(): """A link that says where it points needs nothing worked out.""" render = _render("html", "
A
") assert_equal(render.process_tag(_doc("X")), 'X') assert_equal(DocRender("markdown").process_tag(_doc("X")), "[X](https://x)") assert_equal(DocRender("text").process_tag(_doc("X")), "X") # What a link says is text and markup, so a link to an option names it the way the rest of the documentation does assert_equal( render.process_tag(_doc("X (--x)")), 'X (--x)', ) #################################################################################################################################### def test_link_page(): """A link to a page points at the page when it is part of this build and at the website when it is not.""" render = _render("html", "
A
") assert_equal(render.process_tag(_doc("Home")), 'Home') # A page that is not part of this build is on the website rather than beside this one assert_in("{[backrest-url-base]}/other.html", render.process_tag(_doc("Other"))) # A link to a section of another page is recorded so it can be checked once every page is rendered assert_equal( render.process_tag(_doc("Setup")), 'Setup' ) assert_equal(render.manifest.link_list, [{"source": "user-guide", "page": "index", "section": "/setup"}]) # A link to a section of the page it is on is a link to the section assert_equal(render.process_tag(_doc("A")), 'A') #################################################################################################################################### def test_link_page_markdown(): """Markdown has no anchor for a section of another page, so that link points at the website.""" manifest = _Manifest() render = _render("markdown", "
A
", manifest) assert_equal(render.process_tag(_doc("Home")), "[Home](index.md)") assert_in("{[backrest-url-base]}/index.html#setup", render.process_tag(_doc("S"))) with assert_raises(ToolError) as raised: _render("text", "
A
").process_tag(_doc("Home")) assert_equal(str(raised.exception), "page links not supported for type text, value 'Home'") #################################################################################################################################### def test_link_section(): """A link to a section of this page is an anchor in html and the title of the section in markdown.""" document = "
Set Up The Repo
" assert_equal(_render("html", document).process_tag(_doc("A")), 'A') assert_equal(_render("markdown", document).process_tag(_doc("A")), "[A](#set-up-the-repo)") # A section link is written rooted so that stripping the leading / gives the anchor with assert_raises(ToolError) as raised: _render("html", document).process_tag(_doc("A")) assert_equal(str(raised.exception), "link section 'a' must begin with '/'") with assert_raises(ToolError) as raised: _render("html", document).process_tag(_doc("A")) assert_equal(str(raised.exception), "section link '/missing' does not exist")