Skip to content

Commit

Permalink
Feature: Add initial sort column as ini (#692)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil committed Jul 23, 2023
1 parent ae54ee6 commit 27208bb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
12 changes: 9 additions & 3 deletions src/pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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):
Expand Down
24 changes: 14 additions & 10 deletions src/pytest_html/report_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/pytest_html/scripts/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/pytest_html/scripts/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions src/pytest_html/scripts/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 27208bb

Please sign in to comment.