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

Allow attaching path #1514

Merged
merged 13 commits into from
Apr 2, 2021
Merged
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Please visit [cucumber/CONTRIBUTING.md](https://github.com/cucumber/cucumber/blo

### Fixed

* It is now possible to attach a file passing its path
([#1506](https://github.com/cucumber/cucumber-ruby/issues/1506)
[#1514](https://github.com/cucumber/cucumber-ruby/pull/1514)
[aurelien-reeves](https://github.com/aurelien-reeves))

### Changed

* Upgraded to `cucumber-create-meta` 3.0.0 ([aurelien-reeves](https://github.com/aurelien-reeves), [TimotheeVille](https://github.com/TimotheeVille))
Expand Down
1 change: 1 addition & 0 deletions cucumber.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |s|
s.add_dependency 'cucumber-messages', '~> 14.0', '>= 14.0.1'
s.add_dependency 'cucumber-wire', '~> 4.0', '>= 4.0.1'
s.add_dependency 'diff-lcs', '~> 1.4', '>= 1.4.4'
s.add_dependency 'mime-types', '~> 3.3', '>= 3.3.1'
s.add_dependency 'multi_test', '~> 0.1', '>= 0.1.2'
s.add_dependency 'sys-uname', '~> 1.2', '>= 1.2.1'

Expand Down
4 changes: 2 additions & 2 deletions features/docs/raketask.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Feature: Raketask
In order to use cucumber's rake task
As a Cuker
I do not want to see rake's backtraces when it fails
Also I want to get zero exit status code on failures
And non-zero exit status code when it pases
Also I want to get zero exit status code when it passes
And non-zero exit status code when it fails

Background:
Given the standard step definitions
Expand Down
54 changes: 54 additions & 0 deletions features/docs/writing_support_code/attachments.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Feature: Attachments
It is sometimes useful to take a screenshot while a scenario runs.
Or capture some logs.

Cucumber lets you `attach` arbitrary files during execution, and you can
specify a media type for the contents.

Formatters can then render these attachments in reports.

Background:
Given a file named "features/attaching_screenshot_with_mediatype.feature" with:
"""
Feature: A screenshot feature
Scenario:
Given I attach a screenshot with media type
"""
Given a file named "features/attaching_screenshot_without_mediatype.feature" with:
"""
Feature: A file feature
Scenario:
Given I attach a screenshot without media type
"""
And a file named "features/screenshot.png" with:
"""
foo
"""
And a file named "features/step_definitions/attaching_screenshot_steps.rb" with:
"""
Given /^I attach a screenshot with media type/ do
attach "features/screenshot.png", "image/png"
end

Given /^I attach a screenshot without media type/ do
attach "features/screenshot.png"
end
"""

Scenario: Files can be attached given their path
When I run `cucumber --format message features/attaching_screenshot_with_mediatype.feature`
Then output should be valid NDJSON
And the output should contain NDJSON with key "attachment"
And the output should contain NDJSON with key "body" and value "Zm9v"
And the output should contain NDJSON with key "mediaType" and value "image/png"

Scenario: Media type is inferred from the given file
When I run `cucumber --format message features/attaching_screenshot_without_mediatype.feature`
Then output should be valid NDJSON
And the output should contain NDJSON with key "attachment"
And the output should contain NDJSON with key "body" and value "Zm9v"
And the output should contain NDJSON with key "mediaType" and value "image/png"

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\"}]"
4 changes: 4 additions & 0 deletions features/lib/step_definitions/message_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
end
end

Then('the output should contain NDJSON with key {string}') do |key|
expect(command_line.stdout).to match(/"#{key}":/)
end

Then('the output should contain NDJSON with key {string} and value {string}') do |key, value|
expect(command_line.stdout).to match(/"#{key}": ?"#{value}"/)
end
5 changes: 1 addition & 4 deletions lib/cucumber/formatter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ def attach(src, mime_type)
test_step_output << src
return
end
if File.file?(src)
content = File.open(src, 'rb', &:read)
data = encode64(content)
elsif mime_type =~ /;base64$/
if mime_type =~ /;base64$/
mime_type = mime_type[0..-8]
data = src
else
Expand Down
10 changes: 8 additions & 2 deletions lib/cucumber/glue/proto_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'cucumber/gherkin/formatter/ansi_escapes'
require 'cucumber/core/test/data_table'
require 'cucumber/deprecate'
require 'mime/types'

module Cucumber
module Glue
Expand Down Expand Up @@ -111,8 +112,13 @@ def log(*messages)
messages.each { |message| attach(message.to_s.dup, 'text/x.cucumber.log+plain') }
end

def attach(file, media_type)
super
def attach(file, media_type = nil)
return super unless File.file?(file)

content = File.open(file, 'rb', &:read)
olleolleolle marked this conversation as resolved.
Show resolved Hide resolved
media_type = MIME::Types.type_for(file).first if media_type.nil?

super(content, media_type.to_s)
end

# Mark the matched step as pending.
Expand Down