Skip to content
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

Fix JSON Formatter #1580

Merged
merged 11 commits into from
Dec 7, 2021
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ Please visit [cucumber/CONTRIBUTING.md](https://github.com/cucumber/cucumber/blo
- Do not serialize Messages::Hook#tag_expression if it is empty.
([PR#1579](https://github.com/cucumber/cucumber-ruby/pull/1579))

- JSON Formatter uses "pretty" output format
([PR#1580](https://github.com/cucumber/cucumber-ruby/pull/1580))

- Fixed JSON Formatter "end of background" detection.
([PR#1580](https://github.com/cucumber/cucumber-ruby/pull/1580))

- Fixed JSON Formatter expansion of Scenario Outline templates in Doc Strings.
([PR#1580](https://github.com/cucumber/cucumber-ruby/pull/1580))

- Removed usage of `eval` in `Cucumber::Term::ANSIColor` and `Cucumber::Formatter::ANSIColor`.
([PR#1589](https://github.com/cucumber/cucumber-ruby/pull/1589)
[Issue#1583](https://github.com/cucumber/cucumber-ruby/issues/1583))
Expand Down
95 changes: 95 additions & 0 deletions features/docs/formatters/json_formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,101 @@ Feature: JSON output formatter

"""

Scenario: scenario outline with docstring
Given a file named "features/outline_doc_string.feature" with:
"""
Feature: An outline feature with a DocString

Scenario Outline: outline
Then I should fail with
\"\"\"
<status>
\"\"\"

Examples: examples1
| status |
| passes |
| fails |
"""
And a file named "features/step_definitions/steps.rb" with:
"""
Then /I should fail with/ do |s|
raise RuntimeError, s
end
"""
When I run `cucumber --format json features/outline_doc_string.feature`
Then it should fail with JSON:
"""
[
{
"id": "an-outline-feature-with-a-docstring",
"uri": "features/outline_doc_string.feature",
"keyword": "Feature",
"name": "An outline feature with a DocString",
"line": 1,
"description": "",
"elements": [
{
"id": "an-outline-feature-with-a-docstring;outline;examples1;2",
"keyword": "Scenario Outline",
"name": "outline",
"line": 11,
"description": "",
"type": "scenario",
"steps": [
{
"keyword": "Then ",
"name": "I should fail with",
"line": 4,
"doc_string": {
"content_type": "",
"value": "passes",
"line": 5
},
"match": {
"location": "features/step_definitions/steps.rb:1"
},
"result": {
"status": "failed",
"error_message": "passes (RuntimeError)\n./features/step_definitions/steps.rb:2:in `/I should fail with/'\nfeatures/outline_doc_string.feature:11:4:in `I should fail with'",
"duration": 1
}
}
]
},
{
"id": "an-outline-feature-with-a-docstring;outline;examples1;3",
"keyword": "Scenario Outline",
"name": "outline",
"line": 12,
"description": "",
"type": "scenario",
"steps": [
{
"keyword": "Then ",
"name": "I should fail with",
"line": 4,
"doc_string": {
"content_type": "",
"value": "fails",
"line": 5
},
"match": {
"location": "features/step_definitions/steps.rb:1"
},
"result": {
"status": "failed",
"error_message": "fails (RuntimeError)\n./features/step_definitions/steps.rb:2:in `/I should fail with/'\nfeatures/outline_doc_string.feature:12:4:in `I should fail with'",
"duration": 1
}
}
]
}
]
}
]
"""

Scenario: print from step definition
When I run `cucumber --format json features/print_from_step_definition.feature`
Then it should pass with JSON:
Expand Down
4 changes: 3 additions & 1 deletion features/docs/writing_support_code/attachments.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ Feature: Attachments

Scenario: With json formatter, files can be attached given their path
When I run `cucumber --format json features/attaching_screenshot_with_mediatype.feature`
Then the output should contain "embeddings\":[{\"mime_type\":\"image/png\",\"data\":\"Zm9v\"}]"
Then the output should contain "embeddings\":"
And the output should contain "\"mime_type\": \"image/png\","
And the output should contain "\"data\": \"Zm9v\""
10 changes: 5 additions & 5 deletions lib/cucumber/formatter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def on_test_case_finished(event)
end

def on_test_run_finished(_event)
@io.write(JSON.generate(@feature_hashes, pretty: true))
@io.write(JSON.pretty_generate(@feature_hashes))
end

def attach(src, mime_type)
Expand All @@ -106,7 +106,7 @@ def same_feature_as_previous_test_case?(test_case)
end

def first_step_after_background?(test_step)
@in_background && test_step.location.lines.max >= @test_case_hash[:line]
@in_background && test_step.location.file == @feature_hash[:uri] && test_step.location.lines.max >= @test_case_hash[:line]
end

def internal_hook?(test_step)
Expand Down Expand Up @@ -176,15 +176,15 @@ def create_step_hash(test_step)
name: test_step.text,
line: test_step.location.lines.min
}
step_hash[:doc_string] = create_doc_string_hash(step_source.doc_string) unless step_source.doc_string.nil?
step_hash[:doc_string] = create_doc_string_hash(step_source.doc_string, test_step.multiline_arg.content) unless step_source.doc_string.nil?
step_hash[:rows] = create_data_table_value(step_source.data_table) unless step_source.data_table.nil?
step_hash
end

def create_doc_string_hash(doc_string)
def create_doc_string_hash(doc_string, doc_string_content)
content_type = doc_string.media_type || ''
{
value: doc_string.content,
value: doc_string_content,
content_type: content_type,
line: doc_string.location.line
}
Expand Down