Skip to content

Commit

Permalink
feat: print estimated durations for timed specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 17, 2023
1 parent 4707f42 commit 1705841
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
13 changes: 5 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
const debug = require('debug')('cypress-split')
const { getSpecs } = require('find-cypress-specs')
const ghCore = require('@actions/core')
const cTable = require('console.table')
const { getChunk } = require('./chunk')
const {
splitByDuration,
hasTimeDifferences,
mergeTimings,
} = require('./timings')
const { getEnvironmentFlag } = require('./utils')
const { getEnvironmentFlag, printSpecsList } = require('./utils')
const path = require('path')
const os = require('os')
const fs = require('fs')
Expand Down Expand Up @@ -213,24 +212,22 @@ function cypressSplit(on, config) {
label,
humanizeDuration(sums[splitIndex]),
)

printSpecsListWithDurations(chunks[splitIndex])
} catch (err) {
console.error('%s Could not split specs by duration', label)
console.error(err.message)
console.error('%s splitting as is by name', label)
splitSpecs = getChunk(specs, splitN, splitIndex)
printSpecsList(splitSpecs)
}
} else {
splitSpecs = getChunk(specs, splitN, splitIndex)
printSpecsList(splitSpecs)
}
debug('split specs')
debug(splitSpecs)

const nameRows = splitSpecs.map((specName, k) => {
const row = [String(k + 1), path.relative(cwd, specName)]
return row
})
console.log(cTable.getTable(['k', 'spec'], nameRows))

const addSpecResults = () => {
// at this point, the specAbsoluteToRelative object should be filled
const specRows = splitSpecs.map((absoluteSpecPath, k) => {
Expand Down
41 changes: 40 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// @ts-check

const cTable = require('console.table')
const path = require('path')
const humanizeDuration = require('humanize-duration')

function getEnvironmentFlag(osVariableName, defaultValue = true) {
if (!(osVariableName in process.env)) {
return defaultValue
Expand All @@ -6,4 +12,37 @@ function getEnvironmentFlag(osVariableName, defaultValue = true) {
return value === '1' || value === 'true'
}

module.exports = { getEnvironmentFlag }
/**
* Console table with the list of specs names
*/
function printSpecsList(splitSpecs) {
const cwd = process.cwd()
const nameRows = splitSpecs.map((specName, k) => {
const row = [String(k + 1), path.relative(cwd, specName)]
return row
})

const headers = ['k', 'spec']
console.log(cTable.getTable(headers, nameRows))
}

/**
* Prints a table with spec names and durations
*/
function printSpecsListWithDurations(chunk) {
const cwd = process.cwd()
const nameRows = chunk.map((item, k) => {
const relative = path.relative(cwd, item.specName)
const duration = humanizeDuration(item.duration)
const row = [String(k + 1), relative, duration]
return row
})
const headers = ['k', 'spec', 'duration (estimate)']
console.log(cTable.getTable(headers, nameRows))
}

module.exports = {
getEnvironmentFlag,
printSpecsList,
printSpecsListWithDurations,
}

0 comments on commit 1705841

Please sign in to comment.