Skip to content

Commit

Permalink
Merge #738 Handle hook output appropriately in the Pretty formatter
Browse files Browse the repository at this point in the history
Also update History.md.
  • Loading branch information
brasmusson committed Sep 14, 2014
2 parents 80b95d9 + 66c9c82 commit f7a9343
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 20 deletions.
14 changes: 11 additions & 3 deletions History.md
@@ -1,4 +1,12 @@
## [In Git](https://github.com/cucumber/cucumber/compare/v.2.0.0.beta.2...master)
## [In Git](https://github.com/cucumber/cucumber/compare/v2.0.0.beta.3...master)

### New Features

### Bugfixes

* Handle hook output appropriately in the Pretty formatter ([738](https://github.com/cucumber/cucumber/pull/738) @brasmusson)

## [v2.0.0.beta.3](https://github.com/cucumber/cucumber/compare/v2.0.0.beta.2...v2.0.0.beta.3)

### Removed Features

Expand All @@ -18,14 +26,14 @@
* Add back support for the DataTable API ([729](https://github.com/cucumber/cucumber/pull/729) @mattwynne and @tooky)
* Fix Windows support loading files properly ([739](https://github.com/cucumber/cucumber/issues/739) @os97673)

## [v2.0.0.beta.2](https://github.com/cucumber/cucumber/compare/v.2.0.0.beta.1...v.2.0.0.beta.2)
## [v2.0.0.beta.2](https://github.com/cucumber/cucumber/compare/v2.0.0.beta.1...v2.0.0.beta.2)

### Bugfixes

* Better reporting of exceptions in Before / After hooks ([723](https://github.com/cucumber/cucumber/pull/723) @mattwynne)
* Add `#source_tag_names` method to `TestCase` object passed to hooks (@mattwynne)

## [v2.0.0.beta.1 ](https://github.com/cucumber/cucumber/compare/v1.3.8...v.2.0.0.beta.1)
## [v2.0.0.beta.1 ](https://github.com/cucumber/cucumber/compare/v1.3.8...v2.0.0.beta.1)

Version 2.0 contains a major internal redesign, extracting the core logic of
parsing and executing tests into a [separate gem](https://github.com/cucumber/cucumber-ruby-core).
Expand Down
42 changes: 42 additions & 0 deletions features/docs/formatters/pretty_formatter.feature
Expand Up @@ -28,4 +28,46 @@ Feature: Pretty output formatter
"""
Using the default profile...
"""
Scenario: Hook output should be printed before hook exception
Given the standard step definitions
And a file named "features/test.feature" with:
"""
Feature:
Scenario:
Given this step passes
"""
And a file named "features/step_definitions/output_steps.rb" with:
"""
Before do
puts "Before hook"
end
AfterStep do
puts "AfterStep hook"
end
After do
puts "After hook"
raise "error"
end
"""
When I run `cucumber -q -f pretty features/test.feature`
Then the stderr should not contain anything
Then it should fail with:
"""
Feature:
Scenario:
Before hook
Given this step passes
AfterStep hook
After hook
error (RuntimeError)
./features/step_definitions/output_steps.rb:11:in `After'
Failing Scenarios:
cucumber features/test.feature:2
1 scenario (1 failed)
1 step (1 passed)
"""
16 changes: 8 additions & 8 deletions features/docs/output_from_hooks.feature
Expand Up @@ -110,15 +110,15 @@ Feature: Hook output feature
before_table_cell
table_cell_value
after_table_cell
puts
embed
puts
embed
puts
embed
puts
embed
after_table_row
puts
embed
puts
embed
puts
embed
puts
embed
after_outline_table
after_examples
after_examples_array
Expand Down
3 changes: 3 additions & 0 deletions lib/cucumber/formatter/pretty.rb
Expand Up @@ -72,6 +72,7 @@ def before_feature_element(feature_element)
end

def after_feature_element(feature_element)
print_messages
@io.puts
@io.flush
end
Expand Down Expand Up @@ -125,6 +126,7 @@ def scenario_name(keyword, name, file_colon_line, source_indent)
def before_step(step)
@current_step = step
@indent = 6
print_messages
end

def before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line)
Expand Down Expand Up @@ -161,6 +163,7 @@ def doc_string(string)

def exception(exception, status)
return if @hide_this_step
print_messages
print_exception(exception, status, @indent)
@io.flush
end
Expand Down
40 changes: 31 additions & 9 deletions lib/cucumber/reports/legacy_formatter.rb
Expand Up @@ -205,7 +205,7 @@ def before
attr_reader :current_test_step_source

def before_test_case(test_case)
@before_hook_results = Legacy::Ast::NodeCollection.new
@before_hook_results = Legacy::Ast::HookResultCollection.new
end

def before_test_step(test_step)
Expand Down Expand Up @@ -401,7 +401,7 @@ def indented(nasty_old_conflation_of_name_and_description)

module PrintsAfterHooks
def after_hook_results
@after_hook_results ||= Legacy::Ast::NodeCollection.new
@after_hook_results ||= Legacy::Ast::HookResultCollection.new
end

def after_hook(result)
Expand Down Expand Up @@ -739,8 +739,8 @@ class TableRowPrinterBase < Struct.new(:formatter, :node, :before_hook_results)
include PrintsAfterHooks

def after_step_hook(result)
@after_step_hook_result ||= []
@after_step_hook_result.push result
@after_step_hook_result ||= Legacy::Ast::HookResultCollection.new
@after_step_hook_result << result
end

def after_test_case(*args)
Expand Down Expand Up @@ -804,9 +804,11 @@ def after
formatter.table_cell_value(value, @status || :skipped)
formatter.after_table_cell(value)
end
@after_step_hook_result.send_output_to(formatter) if @after_step_hook_result
after_hook_results.send_output_to(formatter)
formatter.after_table_row(legacy_table_row)
@after_step_hook_result.each { |result| result.accept formatter } if @after_step_hook_result
after_hook_results.accept(formatter)
@after_step_hook_result.describe_exception_to(formatter) if @after_step_hook_result
after_hook_results.describe_exception_to(formatter)
@done = true
self
end
Expand Down Expand Up @@ -840,7 +842,7 @@ def step_invocation(step_invocation, source)
def after
return if @done
@child.after if @child
@after_step_hook_result.each { |result| result.accept formatter } if @after_step_hook_result
@after_step_hook_result.accept(formatter) if @after_step_hook_result
after_hook_results.accept(formatter)
@done = true
self
Expand Down Expand Up @@ -1046,7 +1048,7 @@ def accept(formatter)
private :node
end

class NodeCollection
class HookResultCollection
def initialize
@children = []
end
Expand All @@ -1055,6 +1057,14 @@ def accept(formatter)
@children.each { |child| child.accept(formatter) }
end

def send_output_to(formatter)
@children.each { |child| child.send_output_to(formatter) }
end

def describe_exception_to(formatter)
@children.each { |child| child.describe_exception_to(formatter) }
end

def <<(child)
@children << child
end
Expand All @@ -1077,13 +1087,25 @@ def initialize(result, messages, embeddings)
end

def accept(formatter)
unless @already_accepted
send_output_to(formatter)
describe_exception_to(formatter)
end
self
end

def send_output_to(formatter)
unless @already_accepted
@messages.each { |message| formatter.puts(message) }
@embeddings.each { |embedding| embedding.send_to_formatter(formatter) }
end
end

def describe_exception_to(formatter)
unless @already_accepted
@result.describe_exception_to(formatter)
@already_accepted = true
end
self
end
end

Expand Down
48 changes: 48 additions & 0 deletions spec/cucumber/formatter/pretty_spec.rb
Expand Up @@ -263,6 +263,54 @@ module Formatter
"""
bar
"""
OUTPUT
end
end

describe "with a output from hooks" do
define_feature <<-FEATURE
Feature:
Scenario:
Given this step passes
Scenario Outline:
Given this step <status>
Examples:
| status |
| passes |
FEATURE

define_steps do
Before do
puts "Before hook"
end
AfterStep do
puts "AfterStep hook"
end
After do
puts "After hook"
end
Given(/^this step passes$/) {}
end

it "displays hook output appropriately " do
expect( @out.string ).to include <<OUTPUT
Feature:
Scenario:
Before hook
Given this step passes
AfterStep hook
After hook
Scenario Outline:
Given this step <status>
Examples:
| status |
| passes | Before hook, AfterStep hook, After hook
2 scenarios (2 passed)
2 steps (2 passed)
OUTPUT
end
end
Expand Down

0 comments on commit f7a9343

Please sign in to comment.