-
-
Notifications
You must be signed in to change notification settings - Fork 742
Fix/bootstrap starting for def #1270
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
Changes from 10 commits
e3c18fe
f300901
7d568ea
653bb5c
d8e0aa5
b602c9a
4dfa720
a45ffbe
de9cc40
a27fd1d
fb29c72
1de8ea7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ module.exports = function (genPath, options) { | |
|
||
const codecept = new Codecept(config, {}); | ||
codecept.init(testsPath, (err) => { | ||
if (err) throw new Error(`Error while running init :${err}`); | ||
|
||
if (!config.gherkin) { | ||
output.error('Gherkin is not enabled in config. Run `codecept gherkin:init` to enable it'); | ||
process.exit(1); | ||
|
@@ -34,77 +36,81 @@ module.exports = function (genPath, options) { | |
process.exit(1); | ||
} | ||
|
||
const files = []; | ||
glob.sync(config.gherkin.features, { cwd: global.codecept_dir }).forEach((file) => { | ||
if (!fsPath.isAbsolute(file)) { | ||
file = fsPath.join(global.codecept_dir, file); | ||
} | ||
files.push(fsPath.resolve(file)); | ||
}); | ||
output.print(`Loaded ${files.length} files`); | ||
codecept.runBootstrap((err) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gherkin:snippets should not execute bootstrap (as it's omitted for def/list commands as well) |
||
if (err) throw new Error(`Error while running bootstrap file :${err}`); | ||
|
||
let newSteps = []; | ||
|
||
const parseSteps = (steps) => { | ||
const newSteps = []; | ||
let currentKeyword = ''; | ||
for (const step of steps) { | ||
if (step.keyword.trim() === 'And') { | ||
if (!currentKeyword) throw new Error(`There is no active keyword for step '${step.text}'`); | ||
step.keyword = currentKeyword; | ||
const files = []; | ||
glob.sync(config.gherkin.features, { cwd: global.codecept_dir }).forEach((file) => { | ||
if (!fsPath.isAbsolute(file)) { | ||
file = fsPath.join(global.codecept_dir, file); | ||
} | ||
currentKeyword = step.keyword; | ||
try { | ||
matchStep(step.text); | ||
} catch (err) { | ||
let stepLine = step.text | ||
.replace(/\"(.*?)\"/g, '{string}') | ||
.replace(/(\d+\.\d+)/, '{float}') | ||
.replace(/ (\d+) /, ' {int} '); | ||
stepLine = Object.assign(stepLine, { type: step.keyword.trim(), location: step.location }); | ||
newSteps.push(stepLine); | ||
files.push(fsPath.resolve(file)); | ||
}); | ||
output.print(`Loaded ${files.length} files`); | ||
|
||
let newSteps = []; | ||
|
||
const parseSteps = (steps) => { | ||
const newSteps = []; | ||
let currentKeyword = ''; | ||
for (const step of steps) { | ||
if (step.keyword.trim() === 'And') { | ||
if (!currentKeyword) throw new Error(`There is no active keyword for step '${step.text}'`); | ||
step.keyword = currentKeyword; | ||
} | ||
currentKeyword = step.keyword; | ||
try { | ||
matchStep(step.text); | ||
} catch (err) { | ||
let stepLine = step.text | ||
.replace(/\"(.*?)\"/g, '{string}') | ||
.replace(/(\d+\.\d+)/, '{float}') | ||
.replace(/ (\d+) /, ' {int} '); | ||
stepLine = Object.assign(stepLine, { type: step.keyword.trim(), location: step.location }); | ||
newSteps.push(stepLine); | ||
} | ||
} | ||
} | ||
return newSteps; | ||
}; | ||
return newSteps; | ||
}; | ||
|
||
const parseFile = (file) => { | ||
const ast = parser.parse(fs.readFileSync(file).toString()); | ||
for (const child of ast.feature.children) { | ||
if (child.type === 'ScenarioOutline') continue; // skip scenario outline | ||
newSteps = newSteps.concat(parseSteps(child.steps).map((step) => { | ||
return Object.assign(step, { file: file.replace(global.codecept_dir, '').slice(1) }); | ||
})); | ||
} | ||
}; | ||
const parseFile = (file) => { | ||
const ast = parser.parse(fs.readFileSync(file).toString()); | ||
for (const child of ast.feature.children) { | ||
if (child.type === 'ScenarioOutline') continue; // skip scenario outline | ||
newSteps = newSteps.concat(parseSteps(child.steps).map((step) => { | ||
return Object.assign(step, { file: file.replace(global.codecept_dir, '').slice(1) }); | ||
})); | ||
} | ||
}; | ||
|
||
files.forEach(file => parseFile(file)); | ||
files.forEach(file => parseFile(file)); | ||
|
||
let stepFile = config.gherkin.steps[0]; | ||
if (!fsPath.isAbsolute(stepFile)) { | ||
stepFile = fsPath.join(global.codecept_dir, stepFile); | ||
} | ||
let stepFile = config.gherkin.steps[0]; | ||
if (!fsPath.isAbsolute(stepFile)) { | ||
stepFile = fsPath.join(global.codecept_dir, stepFile); | ||
} | ||
|
||
const snippets = newSteps | ||
.filter((value, index, self) => self.indexOf(value) === index) | ||
.map((step) => { | ||
return ` | ||
const snippets = newSteps | ||
.filter((value, index, self) => self.indexOf(value) === index) | ||
.map((step) => { | ||
return ` | ||
${step.type}('${step}', () => { | ||
// From "${step.file}" ${JSON.stringify(step.location)} | ||
throw new Error('Not implemented yet'); | ||
});`; | ||
}); | ||
}); | ||
|
||
if (!snippets.length) { | ||
output.print('No new snippets found'); | ||
return; | ||
} | ||
output.success(`Snippets generated: ${snippets.length}`); | ||
output.print(snippets.join('\n')); | ||
if (!snippets.length) { | ||
output.print('No new snippets found'); | ||
return; | ||
} | ||
output.success(`Snippets generated: ${snippets.length}`); | ||
output.print(snippets.join('\n')); | ||
|
||
if (!options.dryRun) { | ||
output.success(`Snippets added to ${output.colors.bold(stepFile)}`); | ||
fs.writeFileSync(stepFile, fs.readFileSync(stepFile).toString() + snippets.join('\n') + '\n'); // eslint-disable-line | ||
} | ||
if (!options.dryRun) { | ||
output.success(`Snippets added to ${output.colors.bold(stepFile)}`); | ||
fs.writeFileSync(stepFile, fs.readFileSync(stepFile).toString() + snippets.join('\n') + '\n'); // eslint-disable-line | ||
} | ||
}); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,16 +13,22 @@ module.exports = function (genPath, options) { | |
|
||
const codecept = new Codecept(config, {}); | ||
codecept.init(testsPath, (err) => { | ||
output.print('Gherkin Step definitions:'); | ||
output.print(); | ||
const steps = getSteps(); | ||
for (const step of Object.keys(steps)) { | ||
output.print(` ${output.colors.bold(step)} \n => ${output.colors.green(steps[step].line || '')}`); | ||
} | ||
output.print(); | ||
if (!Object.keys(steps).length) { | ||
output.error('No Gherkin steps defined'); | ||
} | ||
if (err) throw new Error(`Error while running init :${err}`); | ||
|
||
codecept.runBootstrap((err) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bootstrap should not be run for listing steps definition (as it's not executed for def) |
||
if (err) throw new Error(`Error while running bootstrap file :${err}`); | ||
|
||
output.print('Gherkin Step definitions:'); | ||
output.print(); | ||
const steps = getSteps(); | ||
for (const step of Object.keys(steps)) { | ||
output.print(` ${output.colors.bold(step)} \n => ${output.colors.green(steps[step].line || '')}`); | ||
} | ||
output.print(); | ||
if (!Object.keys(steps).length) { | ||
output.error('No Gherkin steps defined'); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like
runHooks
are synchronous now, sodone
is not needed and callback functions.Also I'm going to deprecate custom hooks in config so this code should be 100% synchronous