From 9b5d04045f700c14b0378458f9ebcfe2a25e7d49 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 12 Oct 2023 09:05:24 -0400 Subject: [PATCH 1/6] fix: use absolute paths when finding results --- src/index.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 3832369..e5cce80 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +/// // @ts-check const debug = require('debug')('cypress-split') @@ -15,11 +16,17 @@ const label = 'cypress-split:' const isDefined = (x) => typeof x !== 'undefined' +/** + * Initialize Cypress split plugin using Cypress "on" and "config" arguments. + * @param {Cypress.PluginEvents} on Cypress "on" event registration + * @param {Cypress.Config} config Cypress config object + */ function cypressSplit(on, config) { // maybe the user called this function with a single argument // then we assume it is the config object if (arguments.length === 1) { debug('single argument, assuming it is the config object') + // @ts-ignore config = on } @@ -33,13 +40,22 @@ function cypressSplit(on, config) { // or Cypress env variables debug('Cypress config env') debug(config.env) + debug('current working directory %s', process.cwd()) // collect the test results to generate a better report const specResults = {} on('after:spec', (spec, results) => { // console.log(results, results) debug('after:spec for %s %o', spec.relative, results.stats) - specResults[spec.relative] = results + // make sure there are no duplicate specs for some reason + if (specResults[spec.absolute]) { + console.error( + 'Warning: cypress-split found duplicate test results for %s', + spec.absolute, + ) + } + + specResults[spec.absolute] = results }) let SPLIT = process.env.SPLIT || config.env.split || config.env.SPLIT @@ -111,11 +127,14 @@ function cypressSplit(on, config) { }) console.log(cTable.getTable(['k', 'spec'], specRows)) + const cwd = process.cwd() const addSpecResults = () => { specRows.forEach((specRow) => { - const specName = specRow[1] - const specResult = specResults[specName] + const specAbsolutePath = specRow[1] + const specResult = specResults[specAbsolutePath] if (specResult) { + // shorted to relative filename + const specName = path.relative(cwd, specAbsolutePath) debug('spec results for %s', specName) debug(specResult.stats) // have to convert numbers to strings @@ -125,7 +144,7 @@ function cypressSplit(on, config) { specRow.push(String(specResult.stats.skipped)) specRow.push(humanizeDuration(specResult.stats.wallClockDuration)) } else { - console.error('Could not find spec results for %s', specName) + console.error('Could not find spec results for %s', specAbsolutePath) } }) } @@ -172,9 +191,13 @@ function cypressSplit(on, config) { if (splitSpecs.length) { debug('setting the spec pattern to') debug(splitSpecs) + // if working with Cypress v9, there is integration folder + // @ts-ignore if (config.integrationFolder) { debug('setting test files') + // @ts-ignore config.testFiles = splitSpecs.map((name) => + // @ts-ignore path.relative(config.integrationFolder, name), ) } else { From 852139e75284b71eac1dd20bf0663c90dd4dd257 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 12 Oct 2023 09:22:20 -0400 Subject: [PATCH 2/6] remember relative to absolute paths --- src/index.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index e5cce80..242ea09 100644 --- a/src/index.js +++ b/src/index.js @@ -44,6 +44,9 @@ function cypressSplit(on, config) { // collect the test results to generate a better report const specResults = {} + // map from absolute to relative spec names as reported by Cypress + const specAbsoluteToRelative = {} + on('after:spec', (spec, results) => { // console.log(results, results) debug('after:spec for %s %o', spec.relative, results.stats) @@ -56,6 +59,7 @@ function cypressSplit(on, config) { } specResults[spec.absolute] = results + specAbsoluteToRelative[spec.absolute] = spec.relative }) let SPLIT = process.env.SPLIT || config.env.split || config.env.SPLIT @@ -119,23 +123,32 @@ function cypressSplit(on, config) { console.log('%s chunk %d of %d', label, splitIndex + 1, splitN) debug('get chunk %o', { specs, splitN, splitIndex }) + /** @type {string[]} absolute spec filenames */ const splitSpecs = getChunk(specs, splitN, splitIndex) - const specRows = splitSpecs.map((specName, k) => { - const specRow = [String(k + 1), specName] - return specRow + const cwd = process.cwd() + console.log('spec from the current directory %s', cwd) + const nameRows = splitSpecs.map((specName, k) => { + const row = [String(k + 1), path.relative(cwd, specName)] + return row }) - console.log(cTable.getTable(['k', 'spec'], specRows)) + console.log(cTable.getTable(['k', 'spec'], nameRows)) - const cwd = process.cwd() const addSpecResults = () => { + // at this point, the specAbsoluteToRelative object should be filled + const specRows = splitSpecs.map((absoluteSpecPath, k) => { + const relativeName = specAbsoluteToRelative[absoluteSpecPath] + const specRow = [String(k + 1), relativeName] + return specRow + }) + specRows.forEach((specRow) => { const specAbsolutePath = specRow[1] const specResult = specResults[specAbsolutePath] if (specResult) { // shorted to relative filename - const specName = path.relative(cwd, specAbsolutePath) - debug('spec results for %s', specName) + const relativeName = specAbsoluteToRelative[specAbsolutePath] + debug('spec results for %s', relativeName) debug(specResult.stats) // have to convert numbers to strings specRow.push(String(specResult.stats.passes)) From 4244a16ef746ca7531ba8d2cb6d50e3cda672a98 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 12 Oct 2023 09:24:10 -0400 Subject: [PATCH 3/6] return the array --- src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 242ea09..09a8837 100644 --- a/src/index.js +++ b/src/index.js @@ -160,6 +160,8 @@ function cypressSplit(on, config) { console.error('Could not find spec results for %s', specAbsolutePath) } }) + + return specRows } const shouldWriteSummary = getEnvironmentFlag('SPLIT_SUMMARY', true) @@ -171,7 +173,7 @@ function cypressSplit(on, config) { // because GH does not show the summary before the job finishes // so we might as well wait for all spec results to come in on('after:run', () => { - addSpecResults() + const specRows = addSpecResults() // https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/ ghCore.summary From 5c0627841f57bc868597d1ccdba1cc3f4dde4741 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 12 Oct 2023 09:27:38 -0400 Subject: [PATCH 4/6] simplify code --- src/index.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 09a8837..5f9d208 100644 --- a/src/index.js +++ b/src/index.js @@ -139,15 +139,10 @@ function cypressSplit(on, config) { const specRows = splitSpecs.map((absoluteSpecPath, k) => { const relativeName = specAbsoluteToRelative[absoluteSpecPath] const specRow = [String(k + 1), relativeName] - return specRow - }) - specRows.forEach((specRow) => { - const specAbsolutePath = specRow[1] - const specResult = specResults[specAbsolutePath] + const specResult = specResults[absoluteSpecPath] if (specResult) { // shorted to relative filename - const relativeName = specAbsoluteToRelative[specAbsolutePath] debug('spec results for %s', relativeName) debug(specResult.stats) // have to convert numbers to strings @@ -157,8 +152,10 @@ function cypressSplit(on, config) { specRow.push(String(specResult.stats.skipped)) specRow.push(humanizeDuration(specResult.stats.wallClockDuration)) } else { - console.error('Could not find spec results for %s', specAbsolutePath) + console.error('Could not find spec results for %s', absoluteSpecPath) } + + return specRow }) return specRows From e5f002633219957028ab0a14e0b7bd3b52f29e8e Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 12 Oct 2023 09:31:13 -0400 Subject: [PATCH 5/6] hmm why the chunk is not absolute --- src/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.js b/src/index.js index 5f9d208..55a0d62 100644 --- a/src/index.js +++ b/src/index.js @@ -125,6 +125,8 @@ function cypressSplit(on, config) { debug('get chunk %o', { specs, splitN, splitIndex }) /** @type {string[]} absolute spec filenames */ const splitSpecs = getChunk(specs, splitN, splitIndex) + debug('split specs') + debug(splitSpecs) const cwd = process.cwd() console.log('spec from the current directory %s', cwd) From 285240194056cdcac2bb40df630e44046b823af4 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 12 Oct 2023 09:34:10 -0400 Subject: [PATCH 6/6] make user passed specs absolute --- src/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/index.js b/src/index.js index 55a0d62..9dd314c 100644 --- a/src/index.js +++ b/src/index.js @@ -71,6 +71,10 @@ function cypressSplit(on, config) { specs = SPEC.split(',') .map((s) => s.trim()) .filter(Boolean) + .map((specFilename) => { + // make sure every spec filename is absolute + return path.resolve(specFilename) + }) console.log( '%s have explicit %d spec %s', label,