Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

feat: clearer errors on processing errors #79

Merged
merged 2 commits into from
Sep 10, 2021
Merged
Changes from 1 commit
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
148 changes: 77 additions & 71 deletions src/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,79 +108,85 @@ const getFeatureFromFile = (featureFilename) => {
feature.language = getFeatureFileLanguage(featureFilename, fileLines);
const translations = i18n.getFixedT(feature.language);

fileLines.forEach((nextLine) => {
const line = nextLine.trim();
const newPhase = getNewPhase(translations, line);
if (currentPhase === 'DOC_STRING_STARTED') {
if (newPhase === 'DOC_STRING_STARTED') {
// Reached the end of the doc string
currentPhase = null;
} else {
fileLines.forEach((nextLine, index) => {
try {
const line = nextLine.trim();
const newPhase = getNewPhase(translations, line);
if (currentPhase === 'DOC_STRING_STARTED') {
if (newPhase === 'DOC_STRING_STARTED') {
// Reached the end of the doc string
currentPhase = null;
} else {
const step = scenario.steps[scenario.steps.length - 1];
// Use untrimmed nextLine to preserve whitespace
step.docString += step.docString ? `\n${nextLine}` : nextLine;
}
} else if (newPhase) {
currentPhase = newPhase;
switch (newPhase) {
case 'FEATURE_STARTED':
feature.name = line;
break;
case 'BACKGROUND_STARTED':
scenario = createScenario(line, tags);
feature.background = scenario;
tags = [];
break;
case 'SCENARIO_STARTED':
case 'SCENARIO_OUTLINE_STARTED':
scenario = createScenario(line, tags);
feature.scenarios.push(scenario);
tags = [];
break;
case 'EXAMPLES_STARTED':
scenario.examples = createExamples(line);
break;
default:
}
} else if (line.startsWith('@')) {
tags = line.split(' ');
if (!currentPhase) {
// Feature tags
feature.tags = tags;
}
} else if (line.startsWith('#')) {
// Gherkin comments start with '#' and are required to take an entire line.
// We want to skip any comment lines.
} else if (line.startsWith('|')) {
const step = scenario.steps[scenario.steps.length - 1];
// Use untrimmed nextLine to preserve whitespace
step.docString += step.docString ? `\n${nextLine}` : nextLine;
}
} else if (newPhase) {
currentPhase = newPhase;
switch (newPhase) {
case 'FEATURE_STARTED':
feature.name = line;
break;
case 'BACKGROUND_STARTED':
scenario = createScenario(line, tags);
feature.background = scenario;
tags = [];
break;
case 'SCENARIO_STARTED':
case 'SCENARIO_OUTLINE_STARTED':
scenario = createScenario(line, tags);
feature.scenarios.push(scenario);
tags = [];
break;
case 'EXAMPLES_STARTED':
scenario.examples = createExamples(line);
break;
default:
}
} else if (line.startsWith('@')) {
tags = line.split(' ');
if (!currentPhase) {
// Feature tags
feature.tags = tags;
}
} else if (line.startsWith('#')) {
// Gherkin comments start with '#' and are required to take an entire line.
// We want to skip any comment lines.
} else if (line.startsWith('|')) {
const step = scenario.steps[scenario.steps.length - 1];
const lines = line
.split('|')
.filter((entry) => entry).map((entry) => entry.trim());
switch (currentPhase) {
case 'EXAMPLES_STARTED':
scenario.examples.table.push(lines);
break;
default:
step.table.push(lines);
}
} else if (stepStarting(translations, line)) {
if (scenario) {
scenario.steps.push(createStep(line));
}
} else if (line.length > 0) {
// Nothing new is starting. Must be part of a description
switch (currentPhase) {
case 'FEATURE_STARTED':
feature.description += feature.description ? `\n${line}` : line;
break;
case 'BACKGROUND_STARTED':
feature.background.description += feature.background.description ? `\n${line}` : line;
break;
default:
if (scenario) {
scenario.description += scenario.description ? `\n${line}` : line;
}
const lines = line
.split('|')
.filter((entry) => entry).map((entry) => entry.trim());
switch (currentPhase) {
case 'EXAMPLES_STARTED':
scenario.examples.table.push(lines);
break;
default:
step.table.push(lines);
}
} else if (stepStarting(translations, line)) {
if (scenario) {
scenario.steps.push(createStep(line));
}
} else if (line.length > 0) {
// Nothing new is starting. Must be part of a description
switch (currentPhase) {
case 'FEATURE_STARTED':
feature.description += feature.description ? `\n${line}` : line;
break;
case 'BACKGROUND_STARTED':
feature.background.description += feature.background.description ? `\n${line}` : line;
break;
default:
if (scenario) {
scenario.description += scenario.description ? `\n${line}` : line;
}
}
}
} catch (e) {
if (featureFilename) e.featurefile = featureFilename;
if (typeof index !== 'undefined') e.featureline = index + 1;
throw e;
}
Comment on lines +186 to 190
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the meat of the change, the rest is wrapping the body of the forEach in a try/catch

});
return feature;
Expand Down