Skip to content

Commit

Permalink
Correct the calculation of relative paths in the html reports
Browse files Browse the repository at this point in the history
Add spec for the conversion to relative paths in the html formatter.
  • Loading branch information
brasmusson committed Dec 28, 2014
1 parent b06740a commit 4cc2e76
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/cucumber/formatter/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ def embed(src, mime_type, label)
def embed_image(src, label)
id = "img_#{@img_id}"
@img_id += 1
if @io.respond_to?(:absolute_path) and File.exist?(src)
src = Pathname.new(File.absolute_path(src)).relative_path_from(@io.absolute_path)
end
if @io.respond_to?(:path) and File.file?(src)
out_dir = Pathname.new(File.dirname(File.absolute_path(@io.path)))
src = Pathname.new(File.absolute_path(src)).relative_path_from(out_dir)
end
@builder.span(:class => 'embed') do |pre|
pre << %{<a href="" onclick="img=document.getElementById('#{id}'); img.style.display = (img.style.display == 'none' ? 'block' : 'none');return false">#{label}</a><br>&nbsp;
<img id="#{id}" style="display: none" src="#{src}"/>}
Expand Down
29 changes: 29 additions & 0 deletions spec/cucumber/formatter/html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ module Formatter
}).not_to raise_error
end

describe "when writing the report to a file" do
before(:each) do
allow(@out).to receive(:respond_to?).with(:path, false).and_return(true)
expect(@out).to receive(:respond_to?).with(:path).and_return(true)
expect(@out).to receive(:path).and_return('out/file.html')
run_defined_feature
@doc = Nokogiri.HTML(@out.string)
end

describe "with a step that embeds a snapshot" do
define_steps do
Given(/snap/) {
RSpec::Mocks.allow_message(File, :file?) { true }
embed('out/snapshot.jpeg', 'image/jpeg')
}
end

define_feature(<<-FEATURE)
Feature:
Scenario:
Given snap
FEATURE

it "converts the snapshot path to a relative path" do
expect(@doc.css('.embed img').first.attributes['src'].to_s).to eq "snapshot.jpeg"
end
end
end

describe "given a single feature" do
before(:each) do
run_defined_feature
Expand Down

0 comments on commit 4cc2e76

Please sign in to comment.