From 6f0de4d83d869d3eaf6bad388317edbb7709b123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rasmusson?= Date: Sat, 21 Jun 2014 16:36:26 +0200 Subject: [PATCH 1/2] Support embedding data directly in gherkin formatters. --- features/json_formatter.feature | 59 +++++++++++++++++++ .../formatter/gherkin_formatter_adapter.rb | 6 +- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/features/json_formatter.feature b/features/json_formatter.feature index e9fbb25d77..3e6464673a 100644 --- a/features/json_formatter.feature +++ b/features/json_formatter.feature @@ -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 = "abc" + embed data, "mime-type" + end """ And a file named "features/embed.feature" with: """ @@ -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 @@ -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 diff --git a/lib/cucumber/formatter/gherkin_formatter_adapter.rb b/lib/cucumber/formatter/gherkin_formatter_adapter.rb index 15b165c7ee..497032757e 100644 --- a/lib/cucumber/formatter/gherkin_formatter_adapter.rb +++ b/lib/cucumber/formatter/gherkin_formatter_adapter.rb @@ -146,7 +146,11 @@ 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 + data = file + end if defined?(JRUBY_VERSION) data = data.to_java_bytes end From ffc692b609c9050953e29533453e7ceb4b7875db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rasmusson?= Date: Sun, 22 Jun 2014 20:26:19 +0200 Subject: [PATCH 2/2] Decode base64 data before sending it to a gherkin formatter The gherkin JSON formatter will encode the data it receives before inserting it into the json file. When supporting embedding images in html files, the data sent to the embed method need to be base64 encoded. Unless decoded before sent to the gherkin JSON formatter, the base64 encoding will be applied twice. --- features/json_formatter.feature | 4 ++-- lib/cucumber/formatter/gherkin_formatter_adapter.rb | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/features/json_formatter.feature b/features/json_formatter.feature index 3e6464673a..63d1ee1b2b 100644 --- a/features/json_formatter.feature +++ b/features/json_formatter.feature @@ -56,8 +56,8 @@ Feature: JSON output formatter end Given /^I embed data directly/ do - data = "abc" - embed data, "mime-type" + data = "YWJj" + embed data, "mime-type;base64" end """ And a file named "features/embed.feature" with: diff --git a/lib/cucumber/formatter/gherkin_formatter_adapter.rb b/lib/cucumber/formatter/gherkin_formatter_adapter.rb index 497032757e..9f0d7843be 100644 --- a/lib/cucumber/formatter/gherkin_formatter_adapter.rb +++ b/lib/cucumber/formatter/gherkin_formatter_adapter.rb @@ -149,7 +149,12 @@ def embed(file, mime_type, label) if File.file?(file) data = File.open(file, 'rb') { |f| f.read } else - data = file + 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