Skip to content

Commit 821860d

Browse files
committed
separate protocol check in custom json formatter
1 parent 060124b commit 821860d

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

features/environment.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,9 @@ def before_feature(context, feature):
4242
}
4343
protocol_errors = protocol.enforce(convention_attrs)
4444
for error in protocol_errors:
45-
validation_outcome = ValidationOutcome(
46-
outcome_code=ValidationOutcomeCode.EXECUTED,
47-
observed=error,
48-
expected=error,
49-
feature=context.feature.name,
50-
feature_version=1,
51-
severity=OutcomeSeverity.ERROR,
52-
)
53-
context.protocol_errors.append(validation_outcome)
54-
55-
context.gherkin_outcomes = context.protocol_errors
45+
context.protocol_errors.append(error)
46+
47+
context.gherkin_outcomes = []
5648

5749

5850
def before_scenario(context, scenario):
@@ -130,4 +122,8 @@ def get_or_create_instance_when_set(spf_id):
130122
outcomes_json_str = json.dumps(outcomes) #ncodes to utf-8
131123
outcomes_bytes = outcomes_json_str.encode("utf-8")
132124
for formatter in filter(lambda f: hasattr(f, "embedding"), context._runner.formatters):
133-
formatter.embedding(mime_type="application/json", data=outcomes_bytes, target='feature', attribute_name='validation_outcomes')
125+
formatter.embedding(mime_type="application/json", data=outcomes_bytes, target='feature', attribute_name='validation_outcomes')
126+
127+
# embed protocol errors
128+
protocol_errors_bytes = json.dumps(context.protocol_errors).encode("utf-8")
129+
formatter.embedding(mime_type="application/json", data=protocol_errors_bytes, target='feature', attribute_name='protocol_errors')

main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ def run(filename, rule_type=RuleType.ALL, with_console_output=False, execution_m
128128
except KeyError:
129129
el_list = []
130130
for el in el_list:
131+
protocol_errors = json.loads(base64.b64decode(el.get('protocol_errors', [{}])[0].get('data', '')).decode('utf-8')) if el.get('protocol_errors') else []
132+
if protocol_errors:
133+
yield {
134+
'protocol_errors': protocol_errors,
135+
}
131136
scenario_validation_outcomes = json.loads(base64.b64decode(el.get('validation_outcomes', [{}])[0].get('data', '')).decode('utf-8')) if el.get('validation_outcomes') else []
132137
scenario_info = {
133138
'scenario_name': el['name'],

test/test_main.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def test_invocation(filename):
5050
print('The Gherkin tests did not run for the specified test file, and the JSON report is empty. Please review the test file for any errors.')
5151

5252
rule_is_disabled = feature_info['rule_is_disabled']
53-
validation_outcomes = gherkin_results[1:]
53+
protocol_errors = next((d for d in gherkin_results if 'protocol_errors' in d), None)
54+
55+
validation_outcomes = [d for d in gherkin_results[1:] if 'protocol_errors' not in d]
5456

5557
error_outcomes = [outcome for outcome in validation_outcomes if outcome['severity'] in ['Error', 'Warning']]
5658
activating_outcomes = [outcome for outcome in validation_outcomes if outcome['severity'] == 'Executed']
@@ -75,6 +77,16 @@ def test_invocation(filename):
7577
# state originating from given steps. Therefore when results without disabled messages
7678
# is empty, it means that the rule has not been activated. I.e given statements
7779
# did not result in an actionable set of instances at the time of the first then step.
80+
81+
#first, check if there are no protocol errors
82+
if protocol_errors:
83+
red_text = "\033[91m"
84+
reset_text = "\033[0m"
85+
print(f'{red_text}\n\nWARNING: The following protocol errors have been found:{reset_text}')
86+
print(tabulate.tabulate([[error] for error in protocol_errors['protocol_errors']], headers=['Details'], tablefmt='fancy_grid'))
87+
assert False # table should be printed before the assertion
88+
89+
7890
if base.startswith('fail'):
7991
assert len(error_outcomes) > 0
8092
elif base.startswith('pass'):

0 commit comments

Comments
 (0)