Skip to content

Commit

Permalink
#213 set archives maximum build count to display
Browse files Browse the repository at this point in the history
  • Loading branch information
prashanth-sams committed Apr 8, 2022
1 parent 0c7f0c0 commit d505356
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.rst
Expand Up @@ -72,6 +72,11 @@ Add ``--title`` tag followed by the report title::

$ pytest tests/ --html-report=./report --title='PYTEST REPORT'

Add ``--archive-count`` tag followed by an integer to limit showing the number of builds in the Archives section::

$ pytest tests/ --archive-count 7
$ pytest tests/ --html-report=./report --archive-count 7

..
pytest.ini
Expand Down
13 changes: 12 additions & 1 deletion pytest_html_reporter/plugin.py
@@ -1,5 +1,5 @@
from pytest_html_reporter.html_reporter import HTMLReporter
from pytest_html_reporter.util import clean_screenshots, custom_title
from pytest_html_reporter.util import clean_screenshots, custom_title, remove_old_archives


def pytest_addoption(parser):
Expand All @@ -21,13 +21,24 @@ def pytest_addoption(parser):
help="customize report title",
)

group.addoption(
"--archive-count",
action="store",
dest="archive_count",
default="",
help="set archives maximum build count",
)


def pytest_configure(config):
path = config.getoption("path")
clean_screenshots(path)

title = config.getoption("title")
custom_title(title)

archive_count = config.getoption("archive_count")
remove_old_archives(path, archive_count)

config._html = HTMLReporter(path, config)
config.pluginmanager.register(config._html)
Expand Down
14 changes: 13 additions & 1 deletion pytest_html_reporter/util.py
Expand Up @@ -78,4 +78,16 @@ def clean_screenshots(path):


def custom_title(title):
ConfigVars._title = title[:26] + '...' if title.__len__() > 29 else title
ConfigVars._title = title[:26] + '...' if title.__len__() > 29 else title


def remove_old_archives(path, archive_count):
if archive_count is not '':
if int(archive_count) is 0: return
archive_count = int(archive_count) - 1
archive_dir = os.path.abspath(os.path.expanduser(os.path.expandvars(path))) + '/archive'
if os.path.isdir(archive_dir):
archives = os.listdir(archive_dir)
archives.sort(key=lambda f: os.path.getmtime(os.path.join(archive_dir, f)))
for i in range(0, len(archives) - archive_count):
os.remove(os.path.join(archive_dir, archives[i]))

0 comments on commit d505356

Please sign in to comment.