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

Support embedding data directly in gherkin formatters #695

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
59 changes: 59 additions & 0 deletions features/json_formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ Feature: JSON output formatter
Given /^I print from step definition/ do
puts "from step definition"
end

Given /^I embed data directly/ do
data = "YWJj"
embed data, "mime-type;base64"
end
"""
And a file named "features/embed.feature" with:
"""
Expand Down Expand Up @@ -89,6 +94,14 @@ Feature: JSON output formatter
| passing |

"""
And a file named "features/embed_data_directly.feature" with:
"""
Feature: An embed data directly feature

Scenario:
Given I embed data directly

"""

# Need to investigate why this won't pass in-process. error_message doesn't get det?
@spawn
Expand Down Expand Up @@ -422,6 +435,52 @@ Feature: JSON output formatter
}
]

"""
Scenario: embedding data directly
When I run `cucumber -b --format json features/embed_data_directly.feature`
Then it should pass with JSON:
"""
[
{
"uri": "features/embed_data_directly.feature",
"id": "an-embed-data-directly-feature",
"keyword": "Feature",
"name": "An embed data directly feature",
"line": 1,
"description": "",
"elements": [
{
"id": "an-embed-data-directly-feature;",
"keyword": "Scenario",
"name": "",
"line": 3,
"description": "",
"type": "scenario",
"steps": [
{
"keyword": "Given ",
"name": "I embed data directly",
"line": 4,
"embeddings": [
{
"mime_type": "mime-type",
"data": "YWJj"
}
],
"match": {
"location": "features/step_definitions/steps.rb:38"
},
"result": {
"status": "passed",
"duration": 1
}
}
]
}
]
}
]

"""
@spawn
Scenario: scenario outline
Expand Down
11 changes: 10 additions & 1 deletion lib/cucumber/formatter/gherkin_formatter_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,16 @@ def after_features(features)
end

def embed(file, mime_type, label)
data = File.open(file, 'rb') { |f| f.read }
if File.file?(file)
data = File.open(file, 'rb') { |f| f.read }
else
if mime_type =~ /;base64$/
mime_type = mime_type[0..-8]
data = Base64.decode64(file)
else
data = file
end
end
if defined?(JRUBY_VERSION)
data = data.to_java_bytes
end
Expand Down