Skip to content

Instantly share code, notes, and snippets.

@patricksevat
Created November 20, 2019 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patricksevat/fe32c37ed0829079e181f8bc722108ad to your computer and use it in GitHub Desktop.
Save patricksevat/fe32c37ed0829079e181f8bc722108ad to your computer and use it in GitHub Desktop.
Regex / grep generator
// getSpecFiles.js
const glob = require('glob');
const { readFileSync } = require('fs-extra');
function getFilteredSpecFiles(specs, opts = {}) {
const { glob, grep } = getRegularExpression(opts);
const fileNames = getGlobMatches(specs);
const includedSpecFiles = [];
for (const path of fileNames) {
const file = readFileSync(path, 'utf-8');
if (file.match(glob)) {
includedSpecFiles.push(path);
}
}
return includedSpecFiles;
}
function getGlobMatches(specs) {
return specs.reduce((accumulator, spec) => {
return [...accumulator, ...glob.sync(spec)];
}, []);
}
function getRegularExpression({ smoke, target }) {
if (!smoke && target === 'desktop') {
// ❌ #smoke and ❌ #mobile
return {
glob: /^.*(describe|it)\((?!.*(#smoke|#mobile|#perfecto|#handheld)).*/gm,
grep: /^(?!.*(#smoke|#mobile|#perfecto|#handheld)).*/,
};
} else if (smoke && target === 'desktop') {
// ✅ #smoke, ❌ #mobile
return {
glob: /^.*(describe|it)\((?!.*(#mobile|#perfecto|#handheld)).*(#smoke).*/gm,
grep: /^(?!.*(#mobile|#perfecto|#handheld)).*(#smoke).*/,
};
} else if (!smoke && target === 'mobile') {
// ❌ #smoke, ❌ #desktop, ✅ #mobile
return {
glob: /^.*(describe|it)\((?!.*(#smoke|#desktop)).*(#mobile|#handheld).*/gm,
grep: /^(?!.*(#smoke|#desktop)).*(#mobile|#handheld).*/
};
} else if (smoke && target === 'mobile') {
// ✅ #smoke, ❌ #desktop, ✅ #mobile
return {
glob: /^.*(describe|it)\((?!.*(#desktop)).*(#smoke|#perfecto).*/gm,
grep: /^(?!.*(#desktop)).*(#smoke|#perfecto).*/
};
} else {
throw new Error('unforeseen case');
}
}
exports.getFilteredSpecFiles = getFilteredSpecFiles;
exports.getRegularExpression = getRegularExpression;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment