|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { Parser } = require("gherkin"); |
| 4 | +const glob = require("glob"); |
| 5 | +const fs = require("fs"); |
| 6 | +const { execFileSync } = require("child_process"); |
| 7 | + |
| 8 | +const { shouldProceedCurrentStep } = require("./lib/tagsHelper"); |
| 9 | + |
| 10 | +// TODO currently we only work with feature files in cypress/integration folder. |
| 11 | +// It should be easy to base this on the cypress.json configuration - we are happy to take a PR |
| 12 | +// here if you need this functionality! |
| 13 | + |
| 14 | +const paths = glob.sync("cypress/integration/**/*.feature"); |
| 15 | + |
| 16 | +const featuresToRun = []; |
| 17 | + |
| 18 | +const debug = (message, ...rest) => |
| 19 | + process.env.DEBUG |
| 20 | + ? console.log(`DEBUG: ${message}`, rest.length ? rest : "") |
| 21 | + : null; |
| 22 | + |
| 23 | +const found = process.argv.slice(2).find(arg => arg.indexOf("TAGS=") === 0); |
| 24 | + |
| 25 | +const envTags = found.replace(/.*=/, ""); |
| 26 | +debug("Found tag expression", envTags); |
| 27 | + |
| 28 | +paths.forEach(featurePath => { |
| 29 | + const spec = `${fs.readFileSync(featurePath)}`; |
| 30 | + const parsedFeature = new Parser().parse(spec); |
| 31 | + |
| 32 | + const featureTags = parsedFeature.feature.tags; |
| 33 | + const featureShouldRun = shouldProceedCurrentStep(featureTags, envTags); |
| 34 | + const taggedScenarioShouldRun = parsedFeature.feature.children.some( |
| 35 | + section => |
| 36 | + section.tags && |
| 37 | + section.tags.length && |
| 38 | + shouldProceedCurrentStep(section.tags.concat(featureTags), envTags) |
| 39 | + ); |
| 40 | + debug( |
| 41 | + `Feature: ${featurePath}, featureShouldRun: ${featureShouldRun}, taggedScenarioShouldRun: ${taggedScenarioShouldRun}` |
| 42 | + ); |
| 43 | + if (featureShouldRun || taggedScenarioShouldRun) { |
| 44 | + featuresToRun.push(featurePath); |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +try { |
| 49 | + execFileSync( |
| 50 | + `${__dirname}/node_modules/.bin/cypress`, |
| 51 | + [...process.argv.slice(2), "--spec", featuresToRun.join(",")], |
| 52 | + { |
| 53 | + stdio: [process.stdin, process.stdout, process.stderr] |
| 54 | + } |
| 55 | + ); |
| 56 | +} catch (e) { |
| 57 | + debug("Error while running cypress (or just a test failure)", e); |
| 58 | + process.exit(1); |
| 59 | +} |
0 commit comments