From b06740a04868bd72b83256077b1d7c38b054ea0c Mon Sep 17 00:00:00 2001 From: idstein Date: Wed, 25 Jun 2014 17:21:32 +0200 Subject: [PATCH 1/2] Support of relative image sources If an File-based image is not placed correctly, embedding the image will fail. This sanity check creates a relative path to the HTML IO object. Pre-conditions: - The image to be embedded must be file based - The html IO object must be support an absolute_path method (http://www.ruby-doc.org/core-2.1.2/File.html#method-c-absolute_path) --- lib/cucumber/formatter/html.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/cucumber/formatter/html.rb b/lib/cucumber/formatter/html.rb index a478c1c331..6439e3d006 100644 --- a/lib/cucumber/formatter/html.rb +++ b/lib/cucumber/formatter/html.rb @@ -2,6 +2,7 @@ require 'builder' require 'cucumber/formatter/duration' require 'cucumber/formatter/io' +require 'pathname' module Cucumber module Formatter @@ -43,6 +44,9 @@ 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 @builder.span(:class => 'embed') do |pre| pre << %{#{label}
  } From 4cc2e761347a4b642dfb7952d8144e611ccb38c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rasmusson?= Date: Sun, 28 Dec 2014 17:31:03 +0100 Subject: [PATCH 2/2] Correct the calculation of relative paths in the html reports Add spec for the conversion to relative paths in the html formatter. --- lib/cucumber/formatter/html.rb | 7 ++++--- spec/cucumber/formatter/html_spec.rb | 29 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/cucumber/formatter/html.rb b/lib/cucumber/formatter/html.rb index 6439e3d006..4d7754b759 100644 --- a/lib/cucumber/formatter/html.rb +++ b/lib/cucumber/formatter/html.rb @@ -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 << %{#{label}
  } diff --git a/spec/cucumber/formatter/html_spec.rb b/spec/cucumber/formatter/html_spec.rb index eff3b85fb0..b3e566a516 100644 --- a/spec/cucumber/formatter/html_spec.rb +++ b/spec/cucumber/formatter/html_spec.rb @@ -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