From 27208bba3b9443cf5897a7a33cf5ef27a9e8e8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Sun, 23 Jul 2023 02:37:45 +0200 Subject: [PATCH] Feature: Add initial sort column as ini (#692) --- src/pytest_html/plugin.py | 12 +++++++++--- src/pytest_html/report_data.py | 24 ++++++++++++++---------- src/pytest_html/scripts/dom.js | 4 ++-- src/pytest_html/scripts/sort.js | 4 ++-- src/pytest_html/scripts/storage.js | 8 ++++++-- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/pytest_html/plugin.py b/src/pytest_html/plugin.py index 3bca2fd6..224c2ff4 100644 --- a/src/pytest_html/plugin.py +++ b/src/pytest_html/plugin.py @@ -51,8 +51,8 @@ def pytest_addoption(parser): parser.addini( "render_collapsed", type="string", - default="", - help="Open the report with all rows collapsed. Useful for very large reports", + default="passed", + help="row(s) to render collapsed on open.", ) parser.addini( "max_asset_filename_length", @@ -63,9 +63,15 @@ def pytest_addoption(parser): parser.addini( "environment_table_redact_list", type="linelist", - help="A list of regexes corresponding to environment " + help="a list of regexes corresponding to environment " "table variables whose values should be redacted from the report", ) + parser.addini( + "initial_sort", + type="string", + default="result", + help="column to initially sort on.", + ) def pytest_configure(config): diff --git a/src/pytest_html/report_data.py b/src/pytest_html/report_data.py index 8e6278de..f16ea608 100644 --- a/src/pytest_html/report_data.py +++ b/src/pytest_html/report_data.py @@ -33,17 +33,21 @@ def __init__(self, config): } collapsed = config.getini("render_collapsed") - if collapsed: - if collapsed.lower() == "true": - warnings.warn( - "'render_collapsed = True' is deprecated and support " - "will be removed in the next major release. " - "Please use 'render_collapsed = all' instead.", - DeprecationWarning, - ) - self.set_data( - "renderCollapsed", [outcome.lower() for outcome in collapsed.split(",")] + if collapsed.lower() == "true": + warnings.warn( + "'render_collapsed = True' is deprecated and support " + "will be removed in the next major release. " + "Please use 'render_collapsed = all' instead.", + DeprecationWarning, ) + collapsed = "all" + + self.set_data( + "renderCollapsed", [outcome.lower() for outcome in collapsed.split(",")] + ) + + initial_sort = config.getini("initial_sort") + self.set_data("initialSort", initial_sort) @property def title(self): diff --git a/src/pytest_html/scripts/dom.js b/src/pytest_html/scripts/dom.js index 667b3747..39a6da6c 100644 --- a/src/pytest_html/scripts/dom.js +++ b/src/pytest_html/scripts/dom.js @@ -40,9 +40,9 @@ const dom = { return envRow }, - getListHeader: ({ resultsTableHeader }) => { + getListHeader: ({ initialSort, resultsTableHeader }) => { const header = listHeader.content.cloneNode(true) - const sortAttr = storageModule.getSort() + const sortAttr = storageModule.getSort(initialSort) const sortAsc = JSON.parse(storageModule.getSortDirection()) resultsTableHeader.forEach((html) => { diff --git a/src/pytest_html/scripts/sort.js b/src/pytest_html/scripts/sort.js index 3eaf4bdd..a75fa795 100644 --- a/src/pytest_html/scripts/sort.js +++ b/src/pytest_html/scripts/sort.js @@ -43,7 +43,7 @@ const durationSort = (list, ascending) => { } const doInitSort = () => { - const type = storageModule.getSort() + const type = storageModule.getSort(manager.allData.initialSort) const ascending = storageModule.getSortDirection() const list = manager.testSubset const initialOrder = ['Error', 'Failed', 'Rerun', 'XFailed', 'XPassed', 'Skipped', 'Passed'] @@ -67,7 +67,7 @@ const doInitSort = () => { } const doSort = (type) => { - const newSortType = storageModule.getSort() !== type + const newSortType = storageModule.getSort(manager.allData.initialSort) !== type const currentAsc = storageModule.getSortDirection() const ascending = newSortType ? true : !currentAsc storageModule.setSort(type) diff --git a/src/pytest_html/scripts/storage.js b/src/pytest_html/scripts/storage.js index fd3cb4d1..bcb17cf0 100644 --- a/src/pytest_html/scripts/storage.js +++ b/src/pytest_html/scripts/storage.js @@ -48,9 +48,13 @@ const setFilter = (currentFilter) => { history.pushState({}, null, unescape(url.href)) } -const getSort = () => { +const getSort = (initialSort) => { const url = new URL(window.location.href) - return new URLSearchParams(url.search).get('sort') || 'result' + let sort = new URLSearchParams(url.search).get('sort') + if (!sort) { + sort = initialSort || 'result' + } + return sort } const setSort = (type) => { const url = new URL(window.location.href)