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

Rewrite the Progress, Usage, Stepdefs formatters to the new formatter api #859

Merged
merged 2 commits into from
Jun 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions features/docs/defining_steps/printing_messages.feature
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ Feature: Pretty formatter - Printing messages
Announce

Me
..-UUUUUU
..UUU
Announce with fail
F--
F-
Line: 1: anno1
FFF
F
Line: 2: anno2
...
.
"""

4 changes: 2 additions & 2 deletions features/docs/formatters/usage_formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Feature: Usage formatter
When I run `cucumber -x -f usage --dry-run`
Then it should pass with exactly:
"""
----------
-----------

/A/ # features/step_definitions/steps.rb:1
Given A # features/f.feature:3
Expand All @@ -87,7 +87,7 @@ Feature: Usage formatter
When I run `cucumber -f stepdefs --dry-run`
Then it should pass with exactly:
"""
--------
-----------

/A/ # features/step_definitions/steps.rb:1
/B/ # features/step_definitions/steps.rb:2
Expand Down
3 changes: 1 addition & 2 deletions features/docs/gherkin/outlines.feature
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Feature: Scenario outlines
When I run `cucumber -q --format progress features/outline_sample.feature`
Then it should fail with exactly:
"""
--UU..FF..
U-..F-..

(::) failed steps (::)

Expand All @@ -155,4 +155,3 @@ Feature: Scenario outlines
0m0.012s

"""

4 changes: 2 additions & 2 deletions lib/cucumber/formatter/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def print_elements(elements, status, kind)
end
end

def print_stats(features, options)
def print_stats(duration, options)
failures = collect_failing_scenarios(runtime)
if !failures.empty?
print_failing_scenarios(failures, options.custom_profiles, options[:source])
Expand All @@ -85,7 +85,7 @@ def print_stats(features, options)
@io.puts scenario_summary(runtime) {|status_count, status| format_string(status_count, status)}
@io.puts step_summary(runtime) {|status_count, status| format_string(status_count, status)}

@io.puts(format_duration(features.duration)) if features && features.duration
@io.puts(format_duration(duration)) if duration

if runtime.configuration.randomize?
@io.puts
Expand Down
28 changes: 28 additions & 0 deletions lib/cucumber/formatter/duration_extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Cucumber
module Formatter

class DurationExtractor
attr_reader :result_duration
def initialize(result)
@result_duration = 0
result.describe_to(self)
end

def passed(*) end

def failed(*) end

def undefined(*) end

def skipped(*) end

def pending(*) end

def exception(*) end

def duration(duration, *)
duration.tap { |duration| @result_duration = duration.nanoseconds / 10**9.0 }
end
end
end
end
4 changes: 4 additions & 0 deletions lib/cucumber/formatter/legacy_api/runtime_facade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def scenarios(status = nil)
def steps(status = nil)
results.steps(status)
end

def step_match(step_name, name_to_report=nil)
support_code.step_match(step_name, name_to_report)
end
end

end
Expand Down
3 changes: 2 additions & 1 deletion lib/cucumber/formatter/pretty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ def cell_prefix(status)
end

def print_summary(features)
print_stats(features, @options)
duration = features ? features.duration : nil
print_stats(duration, @options)
print_snippets(@options)
print_passing_wip(@options)
end
Expand Down
74 changes: 22 additions & 52 deletions lib/cucumber/formatter/progress.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'cucumber/formatter/console'
require 'cucumber/formatter/io'
require 'cucumber/formatter/duration_extractor'

module Cucumber
module Formatter
Expand All @@ -13,73 +14,42 @@ def initialize(runtime, path_or_io, options)
@runtime, @io, @options = runtime, ensure_io(path_or_io, "progress"), options
@previous_step_keyword = nil
@snippets_input = []
@total_duration = 0
end

def before_features(features)
print_profile_information
end

def after_features(features)
@io.puts
@io.puts
print_summary(features)
end

def before_feature_element(*args)
@exception_raised = false
end

def after_feature_element(*args)
progress(:failed) if (defined? @exception_raised) and (@exception_raised)
@exception_raised = false
end

def before_steps(*args)
progress(:failed) if (defined? @exception_raised) and (@exception_raised)
@exception_raised = false
end

def after_steps(*args)
@exception_raised = false
end

def after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line)
progress(status)
@status = status
end

def before_outline_table(outline_table)
@outline_table = outline_table
def before_test_case(_test_case)
unless @profile_information_printed
print_profile_information
@profile_information_printed = true
end
@previous_step_keyword = nil
end

def after_outline_table(outline_table)
@outline_table = nil
def after_test_step(test_step, result)
progress(result.to_sym) if !hook?(test_step.source.last) || result.failed?
collect_snippet_data(test_step, result) unless hook?(test_step.source.last)
end

def table_cell_value(value, status)
return unless @outline_table
status ||= @status
progress(status) unless table_header_cell?(status)
def after_test_case(_test_case, result)
@total_duration += DurationExtractor.new(result).result_duration
end

def exception(*args)
@exception_raised = true
def done
@io.puts
@io.puts
print_summary
end

def before_test_case(test_case)
@previous_step_keyword = nil
end
private

def after_test_step(test_step, result)
collect_snippet_data(test_step, result)
def hook?(step)
['Before hook', 'After hook', 'AfterStep hook'].include? step.name
end

private

def print_summary(features)
def print_summary
print_steps(:pending)
print_steps(:failed)
print_stats(features, @options)
print_stats(@total_duration, @options)
print_snippets(@options)
print_passing_wip(@options)
end
Expand Down
38 changes: 16 additions & 22 deletions lib/cucumber/formatter/usage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,33 @@ def initialize(runtime, path_or_io, options)
@runtime = runtime
@io = ensure_io(path_or_io, "usage")
@options = options
@stepdef_to_match = Hash.new{|h,stepdef_key| h[stepdef_key] = []}
@stepdef_to_match = Hash.new { |h, stepdef_key| h[stepdef_key] = [] }
@total_duration = 0
end

def before_features(features)
print_profile_information
end

def before_step(step)
@step = step
@start_time = Time.now
end
def after_test_step(test_step, result)
return if hook?(test_step.source.last)

def before_step_result(*args)
@duration = Time.now - @start_time
end

def after_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background, file_colon_line)
step_match = @runtime.step_match(test_step.source.last.name)
step_definition = step_match.step_definition
unless step_definition.nil? # nil if it's from a scenario outline
stepdef_key = StepDefKey.new(step_definition.regexp_source, step_definition.file_colon_line)
stepdef_key = StepDefKey.new(step_definition.regexp_source, step_definition.file_colon_line)
unless @stepdef_to_match[stepdef_key].map { |key| key[:file_colon_line] }.include? test_step.location
duration = DurationExtractor.new(result).result_duration

@stepdef_to_match[stepdef_key] << {
:keyword => keyword,
:step_match => step_match,
:status => status,
:file_colon_line => @step.file_colon_line,
:duration => @duration
keyword: test_step.source.last.keyword,
step_match: step_match,
status: result.to_sym,
file_colon_line: test_step.location,
duration: duration
}
end
super
end

def print_summary(features)
private

def print_summary
add_unused_stepdefs
aggregate_info

Expand Down
86 changes: 85 additions & 1 deletion spec/cucumber/formatter/progress_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Formatter
before(:each) do
Cucumber::Term::ANSIColor.coloring = false
@out = StringIO.new
@formatter = Progress.new(runtime, @out, {})
@formatter = Progress.new(runtime, @out, Cucumber::Cli::Options.new)
end

describe "given a single feature" do
Expand Down Expand Up @@ -79,6 +79,90 @@ module Formatter
end

end

describe "with hooks" do

describe "all hook passes" do
define_feature <<-FEATURE
Feature:
Scenario:
Given this step passes
FEATURE

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

it "only steps generate output" do
lines = <<-OUTPUT
.
1 scenario (1 passed)
1 step (1 passed)
OUTPUT
lines.split("\n").each do |line|
expect(@out.string).to include line.strip
end
end
end

describe "with a failing before hook" do
define_feature <<-FEATURE
Feature:
Scenario:
Given this step passes
FEATURE

define_steps do
Before do
fail "hook failed"
end
Given(/^this step passes$/) {}
end

it "the failing hook generate output" do
lines = <<-OUTPUT
F-
1 scenario (1 failed)
1 step (1 skipped)
OUTPUT
lines.split("\n").each do |line|
expect(@out.string).to include line.strip
end
end
end

describe "with a failing after hook" do
define_feature <<-FEATURE
Feature:
Scenario:
Given this step passes
FEATURE

define_steps do
After do
fail "hook failed"
end
Given(/^this step passes$/) {}
end

it "the failing hook generate output" do
lines = <<-OUTPUT
.F
1 scenario (1 failed)
1 step (1 passed)
OUTPUT
lines.split("\n").each do |line|
expect(@out.string).to include line.strip
end
end
end
end
end
end
end
Expand Down