Skip to content

Commit 27208bb

Browse files
authored
Feature: Add initial sort column as ini (#692)
1 parent ae54ee6 commit 27208bb

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

src/pytest_html/plugin.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def pytest_addoption(parser):
5151
parser.addini(
5252
"render_collapsed",
5353
type="string",
54-
default="",
55-
help="Open the report with all rows collapsed. Useful for very large reports",
54+
default="passed",
55+
help="row(s) to render collapsed on open.",
5656
)
5757
parser.addini(
5858
"max_asset_filename_length",
@@ -63,9 +63,15 @@ def pytest_addoption(parser):
6363
parser.addini(
6464
"environment_table_redact_list",
6565
type="linelist",
66-
help="A list of regexes corresponding to environment "
66+
help="a list of regexes corresponding to environment "
6767
"table variables whose values should be redacted from the report",
6868
)
69+
parser.addini(
70+
"initial_sort",
71+
type="string",
72+
default="result",
73+
help="column to initially sort on.",
74+
)
6975

7076

7177
def pytest_configure(config):

src/pytest_html/report_data.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,21 @@ def __init__(self, config):
3333
}
3434

3535
collapsed = config.getini("render_collapsed")
36-
if collapsed:
37-
if collapsed.lower() == "true":
38-
warnings.warn(
39-
"'render_collapsed = True' is deprecated and support "
40-
"will be removed in the next major release. "
41-
"Please use 'render_collapsed = all' instead.",
42-
DeprecationWarning,
43-
)
44-
self.set_data(
45-
"renderCollapsed", [outcome.lower() for outcome in collapsed.split(",")]
36+
if collapsed.lower() == "true":
37+
warnings.warn(
38+
"'render_collapsed = True' is deprecated and support "
39+
"will be removed in the next major release. "
40+
"Please use 'render_collapsed = all' instead.",
41+
DeprecationWarning,
4642
)
43+
collapsed = "all"
44+
45+
self.set_data(
46+
"renderCollapsed", [outcome.lower() for outcome in collapsed.split(",")]
47+
)
48+
49+
initial_sort = config.getini("initial_sort")
50+
self.set_data("initialSort", initial_sort)
4751

4852
@property
4953
def title(self):

src/pytest_html/scripts/dom.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ const dom = {
4040

4141
return envRow
4242
},
43-
getListHeader: ({ resultsTableHeader }) => {
43+
getListHeader: ({ initialSort, resultsTableHeader }) => {
4444
const header = listHeader.content.cloneNode(true)
45-
const sortAttr = storageModule.getSort()
45+
const sortAttr = storageModule.getSort(initialSort)
4646
const sortAsc = JSON.parse(storageModule.getSortDirection())
4747

4848
resultsTableHeader.forEach((html) => {

src/pytest_html/scripts/sort.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const durationSort = (list, ascending) => {
4343
}
4444

4545
const doInitSort = () => {
46-
const type = storageModule.getSort()
46+
const type = storageModule.getSort(manager.allData.initialSort)
4747
const ascending = storageModule.getSortDirection()
4848
const list = manager.testSubset
4949
const initialOrder = ['Error', 'Failed', 'Rerun', 'XFailed', 'XPassed', 'Skipped', 'Passed']
@@ -67,7 +67,7 @@ const doInitSort = () => {
6767
}
6868

6969
const doSort = (type) => {
70-
const newSortType = storageModule.getSort() !== type
70+
const newSortType = storageModule.getSort(manager.allData.initialSort) !== type
7171
const currentAsc = storageModule.getSortDirection()
7272
const ascending = newSortType ? true : !currentAsc
7373
storageModule.setSort(type)

src/pytest_html/scripts/storage.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ const setFilter = (currentFilter) => {
4848
history.pushState({}, null, unescape(url.href))
4949
}
5050

51-
const getSort = () => {
51+
const getSort = (initialSort) => {
5252
const url = new URL(window.location.href)
53-
return new URLSearchParams(url.search).get('sort') || 'result'
53+
let sort = new URLSearchParams(url.search).get('sort')
54+
if (!sort) {
55+
sort = initialSort || 'result'
56+
}
57+
return sort
5458
}
5559
const setSort = (type) => {
5660
const url = new URL(window.location.href)

0 commit comments

Comments
 (0)