Skip to content

Commit 23fef75

Browse files
Duration format (#27)
* adjust tests * build format handler * remove dependency --------- Co-authored-by: Jim Brännlund <jimbrannlund@fastmail.com>
1 parent eff9f0d commit 23fef75

File tree

6 files changed

+38
-22
lines changed

6 files changed

+38
-22
lines changed

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
"build:jsapp": "browserify ./src/pytest_html/scripts/index.js > ./src/pytest_html/resources/app.js",
66
"build": "npm run unit && npm run build:css && npm run build:jsapp"
77
},
8-
"dependencies": {
9-
"dayjs": "^1.11.3"
10-
},
118
"devDependencies": {
129
"browserify": "^17.0.0",
1310
"chai": "^4.3.6",
11+
"eslint": "^8.20.0",
12+
"eslint-config-google": "^0.14.0",
1413
"mocha": "^10.0.0",
1514
"sass": "^1.52.3",
16-
"sinon": "^14.0.0",
17-
"eslint": "^8.20.0",
18-
"eslint-config-google": "^0.14.0"
15+
"sinon": "^14.0.0"
1916
}
2017
}

src/pytest_html/scripts/datamanager.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ class DataManager {
4747
get collectedItems() {
4848
return this.renderData.collectedItems
4949
}
50-
get durationFormat() {
51-
return this.renderData.durationFormat
52-
}
5350
get isFinished() {
5451
return this.data.runningState === 'Finished'
5552
}

src/pytest_html/scripts/dom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const dom = {
7575
resultBody.querySelector('.col-result').classList.add(`${collapsed ? 'expander' : 'collapser'}`)
7676
resultBody.querySelector('.col-result').dataset.id = id
7777
resultBody.querySelector('.col-name').innerText = nodeid
78-
resultBody.querySelector('.col-duration').innerText = `${formatDuration(duration)}s`
78+
resultBody.querySelector('.col-duration').innerText = formatDuration(duration)
7979

8080

8181
if (longreprtext) {

src/pytest_html/scripts/filter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const doFilter = (type, show) => {
2727
}
2828
}
2929

30-
module.exports= {
30+
module.exports = {
3131
doFilter,
3232
doInitFilter,
3333
}

src/pytest_html/scripts/utils.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
const { manager } = require('./datamanager.js')
1+
const formatedNumber = (number) =>
2+
number.toLocaleString('en-US', {
3+
minimumIntegerDigits: 2,
4+
useGrouping: false,
5+
})
26

3-
const dayjs = require('dayjs')
4-
const duration = require('dayjs/plugin/duration')
5-
dayjs.extend(duration)
67

7-
const formatDuration = (dur) => {
8-
const durationFormat = manager.durationFormat
9-
if (durationFormat.length === 0) {
10-
return dur.toFixed(2)
11-
} else {
12-
return dayjs.duration(dur * 1000).format(durationFormat)
8+
const formatDuration = ( ms ) => {
9+
const totalSeconds = ms / 1000
10+
11+
if (totalSeconds < 1) {
12+
return `${ms}ms`
1313
}
14+
const hours = Math.floor(totalSeconds / 3600)
15+
let remainingSeconds = totalSeconds % 3600
16+
const minutes = Math.floor(remainingSeconds / 60)
17+
remainingSeconds = remainingSeconds % 60
18+
const seconds = Math.round(remainingSeconds)
19+
20+
return `${formatedNumber(hours)}:${formatedNumber(minutes)}:${formatedNumber(seconds)}`
1421
}
1522

1623
module.exports = { formatDuration }

testing/unittest.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { expect } = require('chai')
22
const sinon = require('sinon')
33
const { doInitFilter, doFilter } = require('../src/pytest_html/scripts/filter.js')
44
const { doInitSort, doSort } = require('../src/pytest_html/scripts/sort.js')
5+
const { formatDuration } = require('../src/pytest_html/scripts/utils.js')
56
const dataModule = require('../src/pytest_html/scripts/datamanager.js')
67
const storageModule = require('../src/pytest_html/scripts/storage.js')
78

@@ -92,7 +93,7 @@ describe('Sort tests', () => {
9293
let sortDirectionMock
9394
beforeEach(() => dataModule.manager.resetRender())
9495

95-
afterEach(() => [sortMock,sortDirectionMock, managerSpy].forEach((fn) => fn.restore()))
96+
afterEach(() => [sortMock, sortDirectionMock, managerSpy].forEach((fn) => fn.restore()))
9697
it('has no stored sort', () => {
9798
sortMock = sinon.stub(storageModule, 'getSort').returns(null)
9899
sortDirectionMock = sinon.stub(storageModule, 'getSortDirection').returns(null)
@@ -141,3 +142,17 @@ describe('Sort tests', () => {
141142
})
142143
})
143144
})
145+
146+
describe('utils tests', () => {
147+
describe('formatDuration', () => {
148+
it('handles small durations', () => {
149+
expect(formatDuration(123)).to.eql('123ms')
150+
expect(formatDuration(0)).to.eql('0ms')
151+
expect(formatDuration(999)).to.eql('999ms')
152+
})
153+
it('handles larger durations', () => {
154+
expect(formatDuration(1234)).to.eql('00:00:01')
155+
expect(formatDuration(12345678)).to.eql('03:25:46')
156+
})
157+
})
158+
})

0 commit comments

Comments
 (0)