Skip to content

Commit

Permalink
Updated --template name and help. (#266)
Browse files Browse the repository at this point in the history
Added diagnostics for missing HTML templates.

Co-authored-by: Steve Bate <svc-atlassian@stevebate.net>
  • Loading branch information
steve-bate and Steve Bate authored Jul 24, 2024
1 parent 14112f3 commit 07c042b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/feditest/cli/commands/convert_transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from feditest.utils import FEDITEST_VERSION

DEFAULT_TEMPLATE = 'default'
DEFAULT_TEMPLATE_PATH = "default"

def run(parser: ArgumentParser, args: Namespace, remaining: list[str]) -> int:
"""
Expand All @@ -32,7 +32,7 @@ def run(parser: ArgumentParser, args: Namespace, remaining: list[str]) -> int:
serializer.write(args.tap)

if isinstance(args.html, str) or args.html:
multifile_serializer = MultifileRunTranscriptSerializer(args.html, args.template)
multifile_serializer = MultifileRunTranscriptSerializer(args.html, args.template_path)
multifile_serializer.write(transcript)

if isinstance(args.json, str) or args.json:
Expand All @@ -59,8 +59,8 @@ def add_sub_parser(parent_parser: _SubParsersAction, cmd_name: str) -> None:
html_group = parser.add_argument_group('html', 'HTML options')
html_group.add_argument('--html',
help="Write results in HTML format to the provided file.")
html_group.add_argument('--template', default=DEFAULT_TEMPLATE,
help=f"When specifying --html, use this template (defaults to '{ DEFAULT_TEMPLATE }').")
html_group.add_argument('--template-path', default=DEFAULT_TEMPLATE_PATH,
help="When specifying --html, use this template path override (comma separated directory names)")
parser.add_argument('--json', nargs="?", const=True, default=False,
help="Write results in JSON format to stdout, or to the provided file (if given).")
parser.add_argument('--summary', nargs="?", const=True, default=False,
Expand Down
41 changes: 25 additions & 16 deletions src/feditest/testruntranscript.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from abc import ABC, abstractmethod
from datetime import datetime
import contextlib
import json
import os
import os.path
import re
import shutil
import sys
import traceback
from abc import ABC, abstractmethod
from datetime import datetime
from typing import IO, Iterator, Optional

import jinja2
Expand Down Expand Up @@ -543,20 +544,28 @@ def session_file_path(plan_session):
len=len
)

with open(self.matrix_file, "w") as fp:
matrix_template = jinja2_env.get_template("test_matrix.jinja2")
fp.write(matrix_template.render(**context))

session_template = jinja2_env.get_template("test_session.jinja2")
for run_session in transcript.sessions:
session_context = dict(context)
session_context.update(
run_session=run_session,
summary=run_session.build_summary(),
)
plan_session = transcript.plan.sessions[run_session.plan_session_index]
with open(os.path.join(dir, session_file_path(plan_session)), "w" ) as fp:
fp.write(session_template.render(**session_context))
try:
with open(self.matrix_file, "w") as fp:
matrix_template = jinja2_env.get_template("test_matrix.jinja2")
fp.write(matrix_template.render(**context))

session_template = jinja2_env.get_template("test_session.jinja2")
for run_session in transcript.sessions:
session_context = dict(context)
session_context.update(
run_session=run_session,
summary=run_session.build_summary(),
)
plan_session = transcript.plan.sessions[run_session.plan_session_index]
with open(os.path.join(dir, session_file_path(plan_session)), "w" ) as fp:
fp.write(session_template.render(**session_context))
except jinja2.exceptions.TemplateNotFound as ex:
with contextlib.redirect_stdout(sys.stderr):
print(f"ERROR: template '{ex}' not found")
print("Searched in the following directories:")
for entry in self.template_path:
print(f" {entry}")
sys.exit(1)


def _init_jinja2_env(self) -> jinja2.Environment:
Expand Down

0 comments on commit 07c042b

Please sign in to comment.