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

JSON image embed renders a broken image in Windows #315

Closed
thienanmac opened this issue Sep 13, 2012 · 3 comments
Closed

JSON image embed renders a broken image in Windows #315

thienanmac opened this issue Sep 13, 2012 · 3 comments
Assignees
Milestone

Comments

@thienanmac
Copy link

Hi,

When we call the embed function on a json report (using --format json -o <report_name>), the base64 encoded string doesn't render the image if we try to embed it in an HTML report. I have done a bit of digging and it seems that the "offending" line is the following:

File.read(filename)

(since json class inherits from gherkin_formatter_adapter)
in lib/cucumber/formatter/gherkin_formatter_adapter.rb

I have proven it using the following test script:

require 'base64'
filename = 'myimage.png'

#generates a proper image in windows
#file_content = File.open(filename, 'rb') { |f| f.read }
#generates a broken image in windows
file_content = File.read(filename)
encoded_data = Base64.encode64(file_content).gsub("\n", '')

File.open('decoded.png', 'wb') do|f|
  f.write(Base64.decode64(encoded_data))
end

html_content =  %Q"<html>
<img src='data:image/png;base64,#{encoded_data}'/>
</html>"
File.open('base64.html', 'w') do|f|
  f.write(html_content)
end

Both 'decoded.png' and the html output will not show the image. However, when i replace the file.read function with file_content = File.open(filename, 'rb') { |f| f.read }, the image and html file will render the image properly.

I think it has to do with windows not being able to interpret the image as a binary by default when it uses the File.read function. I've tested the script under macox, windows (with both ruby 1.8.7 and 1.9.3). Under macosx, either method of reading renders a proper image, but not in windows

@mattwynne
Copy link
Member

I don't quite understand. Do you have a solution here?

@os97673
Copy link
Member

os97673 commented Mar 5, 2013

it looks like we just need to force GherkinFormatterAdapter.embed() read file in binary mode (for 1.9 it would be just adding 'rb' as a second param, but for 1.8.7 we have to use File.open(filename, 'rb') { |f| f.read })

@os97673 os97673 closed this as completed in 2033574 Mar 5, 2013
@ghost ghost assigned os97673 Mar 5, 2013
os97673 added a commit that referenced this issue Mar 10, 2013
It looks like sometimes on Windows images are not recognized as binary files.
Fixes #315
@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants