/
cuke-step-bm.rb
113 lines (99 loc) · 3.53 KB
/
cuke-step-bm.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# encoding: utf-8
require "cuke-step-bm/cli"
module CukeStepBm
# cuke_step_bm bm-cuke-step cuke-step-bm.rb
# :bm => BenchMark(only for display)
# :std => SimpleTimeDiff
# :std_with_log => SimpleTimeDiff log
# :off => Cucumber default do nothing
BM_SUPPORTED_OUTPUT_MODES = [:bm, :std, :std_with_log, :off]
class << self
attr_accessor :root, :log_file, :output_mode, :delimiter
def configure
yield self
end
def suite!
use_defaults!
paths = [
File.expand_path("config/cuke_step_bm.rb", root),
File.expand_path(".cuke_step_bm", root),
"#{ENV["HOME"]}/.cuke_step_bm"
]
paths.each { |path| load(path) if File.exist?(path) }
self.output_mode = :std unless BM_SUPPORTED_OUTPUT_MODES.include? output_mode
end
def use_defaults!
configure do |config|
config.root = File.expand_path('.', Dir.pwd)
config.output_mode = :std
config.log_file = File.join(config.root, 'features', 'steps_consuming.bms')
config.delimiter = "-#{[20879].pack("U*")}-"
end
end
def write_to_log(msg)
File.open(log_file, "a+") { |f| f.write msg }
rescue
nil
end
#remove log file before cuke working with :std_with_log
def remove_log!
File.delete log_file if File.exist?(log_file) && (output_mode == :std_with_log)
rescue Exception => e
warn e.message
end
end # class << self
end # CucumberStepsBm
CukeStepBm.suite!
if defined?(Cucumber) && defined?(Cucumber::VERSION) && (Cucumber::VERSION >= "1.1.1") && (CukeStepBm.output_mode != :off)
CukeStepBm.remove_log!
module Cucumber
module Ast
class StepInvocation #:nodoc:
def invoke(step_mother, configuration)
exec_proc = Proc.new {
find_step_match!(step_mother, configuration)
unless @skip_invoke || configuration.dry_run? || @exception || @step_collection.exception
@skip_invoke = true
begin
@step_match.invoke(@multiline_arg)
step_mother.after_step
status!(:passed)
rescue Pending => e
failed(configuration, e, false)
status!(:pending)
rescue Undefined => e
failed(configuration, e, false)
status!(:undefined)
rescue Cucumber::Ast::Table::Different => e
@different_table = e.table
failed(configuration, e, false)
status!(:failed)
rescue Exception => e
failed(configuration, e, false)
status!(:failed)
end
end
}
if (CukeStepBm.output_mode == :bm) && defined?(Benchmark)
Benchmark.bm do |reporter|
reporter.report("Time consuming:") { exec_proc.call }
end
else
time_begin = Time.now
exec_proc.call
time_end = Time.now
time_for_output = "Step consume %s seconds.\n" % ["#{time_end - time_begin}"]
time_consuming_message = ["#{time_end - time_begin}", @name, file_colon_line].join(CukeStepBm.delimiter)
time_consuming_message << "\n"
if CukeStepBm.output_mode == :std_with_log
puts time_for_output
CukeStepBm.write_to_log time_consuming_message
else
puts time_for_output
end
end
end # invoke
end # StepInvocation
end # Ast
end # Cucumber
end