From a9ffe04a01120cb74a325639559faf69b3ba3582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ca=C3=ADque=20Coelho?= Date: Thu, 2 Nov 2023 16:27:09 -0300 Subject: [PATCH 1/3] Fix conditions to run tests without tags and with inverted tags --- npm/grep/src/plugin.js | 63 +++++++++++++++++++++++------------------- npm/grep/src/utils.js | 46 +++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 38 deletions(-) diff --git a/npm/grep/src/plugin.js b/npm/grep/src/plugin.js index 483b3aa85f18..688540079001 100644 --- a/npm/grep/src/plugin.js +++ b/npm/grep/src/plugin.js @@ -109,35 +109,42 @@ function cypressGrepPlugin (config) { debug('found grep "%s" in %d specs', grep, greppedSpecs.length) debug('%o', greppedSpecs) - } else if (grepTags) { - const parsedGrep = parseGrep(null, grepTags) - - debug('parsed grep tags %o', parsedGrep) + } else if (grepTags || grepUntagged) { + const parsedGrep = parseGrep(null, grepTags); + greppedSpecs = specFiles.filter((specFile) => { - const text = fs.readFileSync(specFile, { encoding: 'utf8' }) - - try { - const testInfo = getTestNames(text) - - debug('spec file %s', specFile) - debug('test info: %o', testInfo.tests) - - return testInfo.tests.some((info) => { - const shouldRun = shouldTestRun(parsedGrep, null, info.tags) - - return shouldRun - }) - } catch (err) { - console.error('Could not determine test names in file: %s', specFile) - console.error('Will run it to let the grep filter the tests') - - return true - } - }) - - debug('found grep tags "%s" in %d specs', grepTags, greppedSpecs.length) - debug('%o', greppedSpecs) - } + const text = fs.readFileSync(specFile, { encoding: 'utf8' }); + + try { + const testInfo = getTestNames(text); + + debug('spec file %s', specFile); + debug('test info: %o', testInfo.tests); + + return testInfo.tests.some((info) => { + const shouldRun = shouldTestRun(parsedGrep, null, info.tags); + if(grepUntagged && info.tags.length !== 0) { + // If grepUntagged is true, and the test has tags, it should not run + return false; + } + + return shouldRun; + }); + } catch (err) { + console.error('Could not determine test names in file: %s', specFile); + console.error('Will run it to let the grep filter the tests'); + + return true; + } + }); + + if (grepUntagged) { + debug('filtered for untagged tests in %d specs', greppedSpecs.length); + } else { + debug('found grep tags "%s" in %d specs', grepTags, greppedSpecs.length); + } + debug('%o', greppedSpecs); + } if (greppedSpecs.length) { config.specPattern = greppedSpecs diff --git a/npm/grep/src/utils.js b/npm/grep/src/utils.js index 9ba2dc544cc3..9ff0c78bb01b 100644 --- a/npm/grep/src/utils.js +++ b/npm/grep/src/utils.js @@ -95,10 +95,36 @@ function parseTagsGrep (s) { return ORS_filtered } -function shouldTestRunTags (parsedGrepTags, tags = []) { +function shouldTestRunTags(parsedGrepTags, tags = [], grepUntagged) { + + // Check for presence of negative tags in parsedGrepTags + const negativeTags = parsedGrepTags.filter(tag => tag.startsWith('-')); + + // Check if the test has any of the negative tags (without the '-') + for (const negTag of negativeTags) { + if (tags.includes(negTag.substring(1))) { + return false; // Don't run the test if it has a negated tag + } + } + + // If grepUntagged is false and the test doesn't have any tags, but its parent describe does + if (!grepUntagged && tags.length === 0 && negativeTags.length > 0) { + return false; // Don't run the test + } + if (!parsedGrepTags.length) { // there are no parsed tags to search for, the test should run - return true + return true; + } + + // If there are no tags on the test and we're trying to exclude a specific tag, run the test + if (!tags.length && parsedGrepTags.some(tag => tag.invert)) { + return true; + } + + // If there are no tags on the test and we're NOT trying to exclude a specific tag, run all tests + if (!tags.length && !parsedGrepTags.some(tag => tag.invert)) { + return true; } // now the test has tags and the parsed tags are present @@ -107,18 +133,18 @@ function shouldTestRunTags (parsedGrepTags, tags = []) { const onePartMatched = parsedGrepTags.some((orPart) => { const everyAndPartMatched = orPart.every((p) => { if (p.invert) { - return !tags.includes(p.tag) + return !tags.includes(p.tag); } - return tags.includes(p.tag) - }) - // console.log('every part matched %o?', orPart, everyAndPartMatched) + return tags.includes(p.tag); + }); - return everyAndPartMatched - }) + // console.log('every part matched %o?', orPart, everyAndPartMatched) + return everyAndPartMatched; + }); // console.log('onePartMatched', onePartMatched) - return onePartMatched + return onePartMatched; } function shouldTestRunTitle (parsedGrep, testName) { @@ -165,7 +191,7 @@ function shouldTestRun (parsedGrep, testName, tags = [], grepUntagged = false) { return ( shouldTestRunTitle(parsedGrep.title, testName) && - shouldTestRunTags(parsedGrep.tags, tags) + shouldTestRunTags(parsedGrep.tags, tags, grepUntagged) ) } From cdc4c154ca08063f8009ddd33b35b6767711c753 Mon Sep 17 00:00:00 2001 From: Caique Coelho Date: Mon, 6 Nov 2023 18:22:28 -0300 Subject: [PATCH 2/3] debug message --- npm/grep/src/plugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/npm/grep/src/plugin.js b/npm/grep/src/plugin.js index 688540079001..f0ec7f8e52e1 100644 --- a/npm/grep/src/plugin.js +++ b/npm/grep/src/plugin.js @@ -81,7 +81,7 @@ function cypressGrepPlugin (config) { console.log('@cypress/grep: filtering specs using "%s" in the title', grep) const parsedGrep = parseGrep(grep) - debug('parsed grep %o', parsedGrep) + debug('parsed grep tags %o', parsedGrep) greppedSpecs = specFiles.filter((specFile) => { const text = fs.readFileSync(specFile, { encoding: 'utf8' }) From 488483fe4590c1c724a0bdf5578300ec8f7d06d6 Mon Sep 17 00:00:00 2001 From: Caique Coelho Date: Mon, 6 Nov 2023 18:24:28 -0300 Subject: [PATCH 3/3] debug message --- npm/grep/src/plugin.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/npm/grep/src/plugin.js b/npm/grep/src/plugin.js index f0ec7f8e52e1..d2a19a3f6db2 100644 --- a/npm/grep/src/plugin.js +++ b/npm/grep/src/plugin.js @@ -81,7 +81,7 @@ function cypressGrepPlugin (config) { console.log('@cypress/grep: filtering specs using "%s" in the title', grep) const parsedGrep = parseGrep(grep) - debug('parsed grep tags %o', parsedGrep) + debug('parsed grep %o', parsedGrep) greppedSpecs = specFiles.filter((specFile) => { const text = fs.readFileSync(specFile, { encoding: 'utf8' }) @@ -112,6 +112,7 @@ function cypressGrepPlugin (config) { } else if (grepTags || grepUntagged) { const parsedGrep = parseGrep(null, grepTags); + debug('parsed grep tags %o', parsedGrep) greppedSpecs = specFiles.filter((specFile) => { const text = fs.readFileSync(specFile, { encoding: 'utf8' });