Skip to content

fix: Inverted tag and grep untagged #28256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions npm/grep/src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,43 @@ function cypressGrepPlugin (config) {

debug('found grep "%s" in %d specs', grep, greppedSpecs.length)
debug('%o', greppedSpecs)
} else if (grepTags) {
const parsedGrep = parseGrep(null, grepTags)

} 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' })

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
Expand Down
46 changes: 36 additions & 10 deletions npm/grep/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
)
}

Expand Down