Skip to content

Commit

Permalink
ReST export plugin should accept --template option
Browse files Browse the repository at this point in the history
The option was accepted by `template` plugin only, but it absolutely
makes sense for `-h rst` to accept it as well. There is a default story
template, and no other template can be used...
  • Loading branch information
happz authored Jul 28, 2023
1 parent 4474649 commit 8f92f8a
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tests/plan/export/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ rlJournalStart
# different wording & quotes.
rlAssertgrep "Error: Invalid value for \"-h\" / \"--how\": invalid choice: weird. (choose from dict, json, yaml)" $rlRun_LOG
else
rlAssertGrep "Error: Invalid value for '-h' / '--how': 'weird' is not one of 'dict', 'json', 'yaml'." $rlRun_LOG
rlAssertGrep "Error: Invalid value for '-h' / '--how': 'weird' is not one of 'dict', 'json', 'template', 'yaml'." $rlRun_LOG
fi
rlPhaseEnd

Expand Down
2 changes: 1 addition & 1 deletion tests/test/export/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ rlJournalStart
# different wording & quotes.
rlAssertgrep "Error: Invalid value for \"-h\" / \"--how\": invalid choice: weird. (choose from dict, json, nitrate, polarion, yaml)" $rlRun_LOG
else
rlAssertGrep "Error: Invalid value for '-h' / '--how': 'weird' is not one of 'dict', 'json', 'nitrate', 'polarion', 'yaml'." $rlRun_LOG
rlAssertGrep "Error: Invalid value for '-h' / '--how': 'weird' is not one of 'dict', 'json', 'nitrate', 'polarion', 'template', 'yaml'." $rlRun_LOG
fi
rlPhaseEnd

Expand Down
2 changes: 1 addition & 1 deletion tmt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ def headfoot(text: str) -> None:
# TODO: move to `template` export plugin options
@option(
'--template', metavar='PATH',
help="Path to a template to use for rendering the export. Used with '--how=template' only."
help="Path to a template to use for rendering the export. Used with '--how=rst|template' only."
)
def stories_export(
context: Context,
Expand Down
9 changes: 7 additions & 2 deletions tmt/export/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tmt.export
import tmt.export.template
import tmt.utils
from tmt.utils import Path


@tmt.base.Story.provides_export('rst')
Expand All @@ -12,8 +13,10 @@ class RestructuredExporter(tmt.export.ExportPlugin):
def export_story(cls,
story: tmt.base.Story,
keys: Optional[List[str]] = None,
template: Optional[Path] = None,
include_title: bool = True) -> str:
return tmt.export.template.TemplateExporter.render_template(
template_filepath=template,
default_template_filename='default-story.rst.j2',
keys=keys,
STORY=story,
Expand All @@ -23,7 +26,9 @@ def export_story(cls,
def export_story_collection(cls,
stories: List[tmt.base.Story],
keys: Optional[List[str]] = None,
template: Optional[Path] = None,
include_title: bool = True,
**kwargs: Any) -> str:
return '\n\n'.join([cls.export_story(story, keys=keys, include_title=include_title)
for story in stories])
return '\n\n'.join([
cls.export_story(story, keys=keys, template=template, include_title=include_title)
for story in stories])
3 changes: 3 additions & 0 deletions tmt/export/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from tmt.utils import Path


@tmt.base.FmfId.provides_export('template')
@tmt.base.Test.provides_export('template')
@tmt.base.Plan.provides_export('template')
@tmt.base.Story.provides_export('template')
class TemplateExporter(tmt.export.ExportPlugin):
@classmethod
Expand Down
4 changes: 4 additions & 0 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5142,6 +5142,10 @@ def render_template_file(

return template.render(**variables).strip()

except FileNotFoundError as exc:
raise GeneralError(
f"Could not open template '{template_filepath}'.") from exc

except jinja2.exceptions.TemplateSyntaxError as exc:
raise GeneralError(
f"Could not parse template '{template_filepath}' at line {exc.lineno}.") from exc
Expand Down

0 comments on commit 8f92f8a

Please sign in to comment.