Scenario
I'm looking for lines that have contain "screenshot" that are not commented out.
Ideas
- My original idea was to use a negative lookahead but that's not working with my grep command
- My next idea was to look for lines that contain
^[\sa-z0-6\.]+screenshot
which would make sure no instance of//
or/*
exist before theawait page.screenshot
but it seems like brackets aren't working either - currently only
grep -E '^\s+await page.screenshot' test/*.js
is working
Question
How can I make sure brackets work with my regex pattern eg ^[\sa-z0-6\.]+screenshot
?
**Edit**
I'm using grep as part of a jest pre-commit test. Here's is example code from one of the puppeteer tests
test('content group should be closed in 2.12up', async () => {
// const about = await page.waitForSelector(`${pageObjects.primaryNavLink}:nth-child(2)`);
// about.click();
// await page.waitFor(50);
// await page.waitForSelector(`${pageObjects.secondaryFactory} .api-link`);
const contentGroup = await page.waitForSelector(pageObjects.contentGroup);
const boundingBox = await contentGroup.boundingBox();
// const mc = await page.waitForSelector('.main-container');
// console.log(mc);
// await page.screenshot({ path: 'test/screenshots/212post.png' });
expect(boundingBox['width']).toBeLessThanOrEqual(1);
});
So if the grep finds any instances of page.screenshot
that are not commented out, my grep based test needs to fail. Once I figure it out with the screenshots then I'm going to write a test that does the same but checks the source javascript files for any uncommented console.log
instances
Finished Product
for those interested - jest test
import "core-js/stable";
import "regenerator-runtime/runtime";
const cp = require('child_process');
describe('index.html', () => {
// test.todo('make sure there are no uncommented console messages')
test('make sure there are no uncommented console logs', async () => {
const cmd = `grep -P --exclude=serviceWorker.js '^(?:(?!//).)*console.log' src/*.js`;
let grep = '';
try {
grep = cp.execSync(cmd);
// console.log(grep.toString())
} catch (err) {
// console.log(err)
}
expect(grep.toString()).toEqual('');
});
test('make sure no screenshots are uncommented', async () => {
// screenshot refs will cause cicd build to fail
const cmd = `grep -P '^(?:(?!//).)*page.screenshot' test/*.test.js`;
let grep = '';
try {
grep = cp.execSync(cmd);
// console.log(grep.toString())
} catch (err) {
// console.log(err)
}
expect(grep.toString()).toEqual('');
});
});
/*
and//
, so is it C code you're processing?grep
or POSIXgrep
?const thing = "//"; await page.screenshot
?\s
is whitespace, try using[:space:]
instead.