Skip to content

Commit

Permalink
feat: print spec estimates (#139)
Browse files Browse the repository at this point in the history
* feat: print estimated durations for timed specs

* fix: spec results

* get the right function
  • Loading branch information
bahmutov committed Oct 17, 2023
1 parent 4707f42 commit f9471a7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
17 changes: 9 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
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,
printSpecsListWithDurations,
} = require('./utils')
const path = require('path')
const os = require('os')
const fs = require('fs')
Expand Down Expand Up @@ -213,24 +216,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
2 changes: 1 addition & 1 deletion src/timings.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function mergeSplitTimings(timings, debug = noop) {
specResults[item.spec] = item.duration
} else {
// pick the max spec duration
const maxDuration = Math.max(item.duration, specResult[item.spec])
const maxDuration = Math.max(item.duration, specResults[item.spec])
specResults[item.spec] = maxDuration
}
})
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 f9471a7

Please sign in to comment.