"""Test Help Parse.
The help is checked against the configuration, so both are written out here. The configuration is the smallest one that has an
option documented in each of the three places help can be: a configuration section, the command line, and a single command."""
####################################################################################################################################
import os
import tempfile
import xml.etree.ElementTree as etree
from harness.test import *
from common.error import *
from common.storage import file_write
from config.parse import bld_cfg_parse
from help.parse import *
# Where the help declaration lives, which the tools tell the parser rather than the parser assuming
PATH_HELP = "doc/xml/reference.xml"
CONFIG = """command:
backup:
command-role:
local: {}
help: {}
restore: {}
version: {}
optionGroup:
pg: {}
option:
buffer-size:
section: global
type: size
allow-list:
- 1MiB
- 32KiB
buffer-size-file:
inherit: buffer-size
force:
type: boolean
stanza:
type: string
set:
type: string
allow-list:
- latest
- oldest
command:
backup: {}
restore: {}
type:
type: string
allow-list:
- full
- diff
command:
restore:
allow-list:
- full
- diff
- incr
process:
type: integer
command-role:
local: {}
command:
backup: {}
"""
# Help for everything the configuration declares. Buffer size is documented in a configuration section, force and stanza on the
# command line, and set and type only by the commands that take them. File buffer size allows what buffer size allows, so it
# inherits the list rather than describing it again, and restore inherits the list backup describes for set the same way, naming
# the command since an option documented by more than one has a list per command. Process has no help at all, since no user can
# give it.
HELP = """Configuration description.
Configuration introduction.
Section introduction.
Buffer size.
Buffer size description.
One MiB.Thirty two KiB.1MiB32KiBFile buffer size.
File buffer size description.
Command description.
Command introduction.
Restore summary.
Restore description.
Backup summary.
Backup description.
Help summary.
Help description.
Version summary.
Version description.
"""
####################################################################################################################################
def _parse(help=HELP, config=CONFIG, detail=True):
"""Parse a help declaration against a configuration."""
with tempfile.TemporaryDirectory() as path:
file_write(os.path.join(path, "build/config.yaml"), config)
file_write(os.path.join(path, PATH_HELP), help)
return bld_hlp_parse(os.path.join(path, PATH_HELP), bld_cfg_parse(path), detail)
####################################################################################################################################
def _error(help=HELP, config=CONFIG):
"""Parse a help declaration that is expected to fail and return the message."""
with assert_raises(ToolError) as error:
_parse(help, config)
return str(error.exception)
####################################################################################################################################
def _find(item_list, name):
"""Find an entry in a list by name."""
return next(item for item in item_list if item.name == name)
####################################################################################################################################
def _render(node):
"""Render a node as the xml text of it."""
return etree.tostring(node, encoding="unicode")
####################################################################################################################################
def test_help_parse():
"""Help is read for every command and option, wherever it is documented."""
bld_hlp = _parse()
assert_equal(bld_hlp.cmd_title, "Command Reference")
assert_equal(bld_hlp.cmd_description, "Command description.")
assert_equal(bld_hlp.opt_title, "Configuration Reference")
assert_equal(bld_hlp.opt_description, "Configuration description.")
# The introductions are left as nodes, since the same nodes are rendered as console text and as documentation
assert_equal(bld_hlp.cmd_introduction.tag, "text")
assert_equal(bld_hlp.opt_introduction.tag, "text")
# Commands are sorted, whatever order they were documented in
assert_equal([cmd.name for cmd in bld_hlp.cmd_list], ["backup", "help", "restore", "version"])
cmd = _find(bld_hlp.cmd_list, "restore")
assert_equal(cmd.title, "Restore")
assert_equal(cmd.summary.tag, "summary")
# An option a command documents differently is under the command rather than in the option list
assert_equal([opt.name for opt in cmd.opt_list], ["set", "type"])
assert_is_none(_find(bld_hlp.cmd_list, "help").opt_list)
# Options are sorted and are found wherever they were documented
assert_equal([opt.name for opt in bld_hlp.opt_list], ["buffer-size", "buffer-size-file", "force", "stanza"])
# An option documented in a configuration section takes the section it is documented in
opt = _find(bld_hlp.opt_list, "buffer-size")
assert_equal(opt.section, "general")
assert_equal(opt.title, "Buffer Size")
assert_equal(opt.example_list, ["1MiB", "32KiB"])
# An option documented on the command line is in no section, unless it says which section it belongs to
assert_is_none(_find(bld_hlp.opt_list, "force").section)
assert_is_none(_find(bld_hlp.opt_list, "force").example_list)
assert_equal(_find(bld_hlp.opt_list, "stanza").section, "stanza")
####################################################################################################################################
def test_help_parse_section():
"""A configuration section is where the options that can be set in the configuration file are documented."""
bld_hlp = _parse()
assert_equal([(section.id, section.name) for section in bld_hlp.sct_list], [("general", "General")])
assert_equal(bld_hlp.sct_list[0].introduction.tag, "text")
####################################################################################################################################
def test_help_parse_detail():
"""The text only the documentation renders is not required when it is not going to be rendered."""
help = HELP.replace("Command description.", "")
help = help.replace(
"""
Command introduction.
""",
"",
)
help = help.replace(
"""
Section introduction.
""",
"",
)
bld_hlp = _parse(help, detail=False)
assert_is_none(bld_hlp.cmd_description)
assert_is_none(bld_hlp.cmd_introduction)
assert_is_none(bld_hlp.sct_list[0].introduction)
# It is required when it is
assert_in("unable to find child 'text' in node 'config-section'", _error(help))
####################################################################################################################################
def test_help_parse_allow_list():
"""The values an option allows are declared in the configuration, so the help describes them and they are checked against it."""
bld_hlp = _parse()
# A list that describes each value is introduced by what it is a list of and rendered as the value then what it means
buffer_size_list = (
"
The following buffer sizes are supported:
"
"1MiB - One MiB."
"32KiB - Thirty two KiB."
)
assert_in(buffer_size_list, _render(_find(bld_hlp.opt_list, "buffer-size").description))
# A list that describes no value is rendered from the declaration, in the sentence that introduces the values. An option a
# command documents is rendered from what the option allows, or from what the command allows when it allows something else.
cmd_hlp = _find(bld_hlp.cmd_list, "restore")
assert_in(
"
Type description. Allowed values are full, diff, and incr.
",
_render(_find(cmd_hlp.opt_list, "type").description),
)
# An option that allows what another option allows renders the list it inherits rather than one of its own
assert_in(buffer_size_list, _render(_find(bld_hlp.opt_list, "buffer-size-file").description))
# An option that more than one command documents renders the list the named command describes, in both places
set_list = (
"
The following sets are supported:
"
"latest - The latest."
"oldest - The oldest."
)
assert_in(set_list, _render(_find(_find(bld_hlp.cmd_list, "backup").opt_list, "set").description))
assert_in(set_list, _render(_find(cmd_hlp.opt_list, "set").description))
# A list inherited from an option that describes none
assert_equal(
_error(HELP.replace('', '')),
"option 'buffer-size-file' inherits the allow list of option 'stanza', which does not describe one",
)
# A list inherited from a command that describes none
assert_equal(
_error(HELP.replace('command="backup"', 'command="help"')),
"option 'set' for command 'restore' inherits the allow list of option 'set' for command 'help', which does not describe"
" one",
)
# A value that is documented but not allowed
assert_equal(
_error(HELP.replace('', '')),
"option 'buffer-size' allow list is documented as '1MiB, 64KiB' but declared as '1MiB, 32KiB'",
)
# A value that is allowed but not documented
assert_equal(
_error(HELP.replace('Thirty two KiB.', "")),
"option 'buffer-size' allow list is documented as '1MiB' but declared as '1MiB, 32KiB'",
)
# A list that describes each value but does not say what it is a list of
assert_equal(
_error(HELP.replace('', "")),
"unable to find attribute 'caption' in node 'allow-list'",
)
# An allow list documented for an option that does not have one
assert_equal(
_error(HELP.replace("
Force description.
", "
Force description.
")),
"option 'force' does not have an allow list",
)
# An allow list that is not documented at all, since a value a user cannot read about is a value they cannot use
assert_equal(
_error(HELP.replace("Allowed values are .", "")),
"option 'type' for command 'restore' must document its allow list",
)
####################################################################################################################################
def test_help_parse_error():
"""Anything the user can reach must have help, since help that is missing is only found by looking for it."""
# A command with no help at all
assert_equal(
_error(HELP.replace('', '')),
"command 'backup' must have help",
)
# An option with no help of its own must be documented by every command that takes it
assert_equal(
_error(HELP.replace('