rspec_html_formatter 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ gem 'rspec-core', '>= 2.8.0'
4
+ gem 'rouge', '>= 1.6.1'
5
+ gem 'activesupport', '>= 4.1.4'
6
+
7
+ # Add dependencies to develop your gem here.
8
+ # Include everything needed to run rake, tests, features, etc.
9
+ group :development do
10
+ gem "rspec", "~> 2.8.0"
11
+ gem "rdoc", "~> 3.12"
12
+ gem "bundler", "~> 1.0"
13
+ gem "jeweler", "~> 2.0.1"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (4.1.4)
5
+ i18n (~> 0.6, >= 0.6.9)
6
+ json (~> 1.7, >= 1.7.7)
7
+ minitest (~> 5.1)
8
+ thread_safe (~> 0.1)
9
+ tzinfo (~> 1.1)
10
+ addressable (2.3.6)
11
+ builder (3.2.2)
12
+ descendants_tracker (0.0.4)
13
+ thread_safe (~> 0.3, >= 0.3.1)
14
+ diff-lcs (1.1.3)
15
+ faraday (0.9.0)
16
+ multipart-post (>= 1.2, < 3)
17
+ git (1.2.8)
18
+ github_api (0.12.0)
19
+ addressable (~> 2.3)
20
+ descendants_tracker (~> 0.0.4)
21
+ faraday (~> 0.8, < 0.10)
22
+ hashie (>= 3.2)
23
+ multi_json (>= 1.7.5, < 2.0)
24
+ nokogiri (~> 1.6.3)
25
+ oauth2
26
+ hashie (3.2.0)
27
+ highline (1.6.21)
28
+ i18n (0.6.11)
29
+ jeweler (2.0.1)
30
+ builder
31
+ bundler (>= 1.0)
32
+ git (>= 1.2.5)
33
+ github_api
34
+ highline (>= 1.6.15)
35
+ nokogiri (>= 1.5.10)
36
+ rake
37
+ rdoc
38
+ json (1.8.1)
39
+ jwt (1.0.0)
40
+ mini_portile (0.6.0)
41
+ minitest (5.4.0)
42
+ multi_json (1.10.1)
43
+ multi_xml (0.5.5)
44
+ multipart-post (2.0.0)
45
+ nokogiri (1.6.3.1)
46
+ mini_portile (= 0.6.0)
47
+ oauth2 (1.0.0)
48
+ faraday (>= 0.8, < 0.10)
49
+ jwt (~> 1.0)
50
+ multi_json (~> 1.3)
51
+ multi_xml (~> 0.5)
52
+ rack (~> 1.2)
53
+ rack (1.5.2)
54
+ rake (10.3.2)
55
+ rdoc (3.12.2)
56
+ json (~> 1.4)
57
+ rouge (1.6.1)
58
+ rspec (2.8.0)
59
+ rspec-core (~> 2.8.0)
60
+ rspec-expectations (~> 2.8.0)
61
+ rspec-mocks (~> 2.8.0)
62
+ rspec-core (2.8.0)
63
+ rspec-expectations (2.8.0)
64
+ diff-lcs (~> 1.1.2)
65
+ rspec-mocks (2.8.0)
66
+ thread_safe (0.3.4)
67
+ tzinfo (1.2.1)
68
+ thread_safe (~> 0.1)
69
+
70
+ PLATFORMS
71
+ ruby
72
+
73
+ DEPENDENCIES
74
+ activesupport (>= 4.1.4)
75
+ bundler (~> 1.0)
76
+ jeweler (~> 2.0.1)
77
+ rdoc (~> 3.12)
78
+ rouge (>= 1.6.1)
79
+ rspec (~> 2.8.0)
80
+ rspec-core (>= 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Kingsley Hendrickse
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Publish pretty [rspec](http://rspec.info/) reports
2
+
3
+ This is a ruby Rspec custom formatter which generates pretty html reports showing the results of rspec tests. This gem was build to use Rspec 2.8.0. If you want to use it with newer
4
+ versions of Rspec then you need to include the rspec legacy formatters - see below for instructions.
5
+
6
+ ## Install
7
+
8
+ ```
9
+ gem install rspec_reports_formatter
10
+ ```
11
+
12
+ ideally just add it to your bundler Gemfile as follows:
13
+
14
+ ```ruby
15
+ gem 'rspec_reports_formatter'
16
+ ```
17
+
18
+ ## Use
19
+ When running your rspec tests with rspec 2.8.0 just use the custom formatter:
20
+
21
+ This should work:
22
+
23
+ ```
24
+ rspec -f RspecHtmlFormatter spec
25
+ ```
26
+
27
+ If not you can explicitly add in a require as follows:
28
+
29
+ ```
30
+ rspec --require rspec_html_formatter.rb --format RspecHtmlFormatter spec
31
+ ```
32
+
33
+ If you are using a more recent version of Rspec (3.0.0 onwards) then you must include the legacy formatter gem into your project by adding it to your Gemfile:
34
+
35
+ ```ruby
36
+
37
+ # Your gemfile
38
+ group :development do
39
+ gem 'rspec','~> 3.0.0.rc1'
40
+ gem 'bundler', '~> 1.0'
41
+ gem 'rspec-legacy_formatters'
42
+ gem 'rspec_html_formatter'
43
+ end
44
+
45
+ ```
46
+
47
+ Then use it at the command line:
48
+
49
+ ```
50
+ rspec --require rspec/legacy_formatters -f RspecHtmlFormatter spec
51
+ ```
52
+ It will probably throw out some deprecation warnings. Sorry. I will probably make a version of this reporting specifically for rspec 3.0.0 and onwards at some point.
53
+
54
+
55
+ ![example overview report]
56
+ (https://raw.githubusercontent.com/kingsleyh/rspec_reports_formatter/master/.README/rspec_reports_overview.png)
57
+
58
+ ![example report]
59
+ (https://raw.githubusercontent.com/kingsleyh/rspec_reports_formatter/master/.README/rspec_reports_report.png)
60
+
61
+ If you want to provide some generated documentation for the tests you can put comments in the rspec tests like this:
62
+
63
+ ```ruby
64
+ #-> Given I have ordered a vegetarian pizza
65
+ #-> When I eat the pizza
66
+ #-> Then my tummy is full
67
+
68
+ ```
69
+
70
+ The #-> notation is picked up and passed through a Gherkin syntax highlighter. So it was designed to use with Given,When,Then. But in theory you can put other text there too.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "rspec_html_formatter"
18
+ gem.homepage = "http://github.com/kingsleyh/rspec_html_formatter"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Rspec custom formatter to generate pretty html results}
21
+ gem.description = %Q{Rspec custom formatter to generate pretty html results}
22
+ gem.email = "kingsleyhendrickse@me.com"
23
+ gem.authors = ["Kingsley Hendrickse"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ desc "Code coverage detail"
35
+ task :simplecov do
36
+ ENV['COVERAGE'] = "true"
37
+ Rake::Task['spec'].execute
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rdoc/task'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "rspec_html_formatter #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,233 @@
1
+ require 'rspec/core/formatters/base_formatter'
2
+ require 'active_support'
3
+ require 'active_support/core_ext/numeric'
4
+ require 'active_support/inflector'
5
+ require 'fileutils'
6
+ require 'rouge'
7
+ require 'erb'
8
+
9
+ I18n.enforce_available_locales = false
10
+
11
+ class Oopsy
12
+ attr_reader :klass, :message, :backtrace, :highlighted_source, :explanation, :backtrace_message
13
+
14
+ def initialize(exception, file_path)
15
+ @exception = exception
16
+ @file_path = file_path
17
+ unless @exception.nil?
18
+ @klass = @exception.class
19
+ @message = @exception.message.encode('utf-8')
20
+ @backtrace = @exception.backtrace
21
+ @backtrace_message = @backtrace.select { |r| r.match(@file_path) }.join('').encode('utf-8')
22
+ @highlighted_source = process_source
23
+ @explanation = process_message
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def process_source
30
+ data = @backtrace_message.split(':')
31
+ file_path = data.first
32
+ line_number = data[1].to_i
33
+ lines = File.readlines(file_path)
34
+ start_line = line_number-2
35
+ end_line = line_number+3
36
+ source = lines[start_line..end_line].join("").sub(lines[line_number-1].chomp, "--->#{lines[line_number-1].chomp}")
37
+
38
+ formatter = Rouge::Formatters::HTML.new(css_class: 'highlight', line_numbers: true, start_line: start_line+1)
39
+ lexer = Rouge::Lexers::Ruby.new
40
+ formatter.format(lexer.lex(source))
41
+
42
+ end
43
+
44
+ def process_message
45
+ formatter = Rouge::Formatters::HTML.new(css_class: 'highlight')
46
+ lexer = Rouge::Lexers::Ruby.new
47
+ formatter.format(lexer.lex(@message))
48
+ end
49
+
50
+ end
51
+
52
+ class Example
53
+
54
+ attr_reader :description, :full_description, :run_time, :duration, :status, :exception, :file_path, :metadata, :spec
55
+
56
+ def initialize(example)
57
+ @description = example.description
58
+ @full_description = example.full_description
59
+ @execution_result = example.execution_result
60
+ @run_time = (@execution_result[:run_time]).round(5)
61
+ @duration = @execution_result[:run_time].to_s(:rounded, precision: 5)
62
+ @status = @execution_result[:status].to_s
63
+ @metadata = example.metadata
64
+ @file_path = @metadata[:file_path]
65
+ @exception = Oopsy.new(example.exception, @file_path)
66
+ @spec = nil
67
+ end
68
+
69
+ def has_exception?
70
+ !@exception.klass.nil?
71
+ end
72
+
73
+ def has_spec?
74
+ !@spec.nil?
75
+ end
76
+
77
+ def set_spec(spec)
78
+ @spec = spec
79
+ end
80
+
81
+ def klass(prefix='label-')
82
+ class_map = {passed: "#{prefix}success", failed: "#{prefix}danger", pending: "#{prefix}warning"}
83
+ class_map[@status.to_sym]
84
+ end
85
+
86
+ end
87
+
88
+ class Specify
89
+
90
+ def initialize(examples)
91
+ @examples = examples
92
+ end
93
+
94
+ def process
95
+ lines = File.readlines(@examples.first.file_path)
96
+ @examples.each_with_index do |e, i|
97
+ start_line = e.metadata[:line_number]
98
+ end_line = @examples[i+1].nil? ? lines.size : @examples[i+1].metadata[:line_number] - 1
99
+ code_block = lines[start_line..end_line]
100
+ spec = code_block.select { |l| l.match(/#->/) }.join('')
101
+ if !spec.split.empty?
102
+ formatter = Rouge::Formatters::HTML.new(css_class: 'highlight')
103
+ lexer = Rouge::Lexers::Gherkin.new
104
+ formatted_spec = formatter.format(lexer.lex(spec.gsub('#->', '')))
105
+ e.set_spec(formatted_spec)
106
+ end
107
+ end
108
+ @examples
109
+ end
110
+ end
111
+
112
+ class RspecHtmlFormatter < RSpec::Core::Formatters::BaseFormatter
113
+
114
+ REPORT_PATH = './rspec_html_reports'
115
+
116
+ def initialize(io_standard_out)
117
+ create_reports_dir
118
+ create_resources_dir
119
+ copy_resources
120
+ @all_groups = {}
121
+ end
122
+
123
+ def example_group_started(example_group)
124
+ @example_group = {}
125
+ @group_examples = []
126
+ @group_example_count = 0
127
+ @group_example_success_count = 0
128
+ @group_example_failure_count = 0
129
+ @group_example_pending_count = 0
130
+ end
131
+
132
+ def example_started(example)
133
+ @group_example_count += 1
134
+ end
135
+
136
+ def example_passed(example)
137
+ @group_example_success_count += 1
138
+ @group_examples << Example.new(example)
139
+ end
140
+
141
+ def example_failed(example)
142
+ @group_example_failure_count += 1
143
+ @group_examples << Example.new(example)
144
+ end
145
+
146
+ def example_pending(example)
147
+ @group_example_pending_count += 1
148
+ @group_examples << Example.new(example)
149
+ end
150
+
151
+ def example_group_finished(example_group)
152
+ File.open("#{REPORT_PATH}/#{example_group.description.parameterize}.html", 'w') do |f|
153
+
154
+ @passed = @group_example_success_count
155
+ @failed = @group_example_failure_count
156
+ @pending = @group_example_pending_count
157
+
158
+ duration_values = @group_examples.map { |e| e.run_time }
159
+
160
+ duration_keys = duration_values.size.times.to_a
161
+ if duration_values.size < 2
162
+ duration_values.unshift(duration_values.first)
163
+ duration_keys = duration_keys << 1
164
+ end
165
+
166
+ @title = example_group.description
167
+ @durations = duration_keys.zip(duration_values)
168
+ @summary_duration = duration_values.inject(0) { |sum, i| sum + i }.to_s(:rounded, precision: 5)
169
+ @examples = Specify.new(@group_examples).process
170
+
171
+ class_map = {passed: 'success', failed: 'danger', pending: 'warning'}
172
+ statuses = @examples.map { |e| e.status }
173
+ status = statuses.include?('failed') ? 'failed' : (statuses.include?('passed') ? 'passed' : 'pending')
174
+ @all_groups[example_group.description.parameterize] = {
175
+ group: example_group.description,
176
+ examples: @examples.size,
177
+ status: status,
178
+ klass: class_map[status.to_sym],
179
+ passed: statuses.select { |s| s == 'passed' },
180
+ failed: statuses.select { |s| s == 'failed' },
181
+ pending: statuses.select { |s| s == 'pending' },
182
+ duration: @summary_duration
183
+ }
184
+
185
+ template_file = File.read(File.dirname(__FILE__) + '/../templates/report.erb')
186
+
187
+ f.puts ERB.new(template_file).result(binding)
188
+
189
+ end
190
+
191
+ end
192
+
193
+ def close
194
+ File.open("#{REPORT_PATH}/overview.html", 'w') do |f|
195
+ @overview = @all_groups
196
+
197
+ @passed = @overview.values.map { |g| g[:passed].size }.inject(0) { |sum, i| sum + i }
198
+ @failed = @overview.values.map { |g| g[:failed].size }.inject(0) { |sum, i| sum + i }
199
+ @pending = @overview.values.map { |g| g[:pending].size }.inject(0) { |sum, i| sum + i }
200
+
201
+ duration_values = @overview.values.map { |e| e[:duration] }
202
+
203
+ duration_keys = duration_values.size.times.to_a
204
+ if duration_values.size < 2
205
+ duration_values.unshift(duration_values.first)
206
+ duration_keys = duration_keys << 1
207
+ end
208
+
209
+ @durations = duration_keys.zip(duration_values.map{|d| d.to_f.round(5)})
210
+ @summary_duration = duration_values.map{|d| d.to_f.round(5)}.inject(0) { |sum, i| sum + i }.to_s(:rounded, precision: 5)
211
+ @total_examples = @passed + @failed + @pending
212
+ template_file = File.read(File.dirname(__FILE__) + '/../templates/overview.erb')
213
+ f.puts ERB.new(template_file).result(binding)
214
+ end
215
+ end
216
+
217
+
218
+ private
219
+ def create_reports_dir
220
+ FileUtils.rm_rf(REPORT_PATH) if File.exists?(REPORT_PATH)
221
+ FileUtils.mkpath(REPORT_PATH)
222
+ end
223
+
224
+ def create_resources_dir
225
+ file_path = REPORT_PATH + '/resources'
226
+ FileUtils.mkdir_p file_path unless File.exists?(file_path)
227
+ end
228
+
229
+ def copy_resources
230
+ FileUtils.cp_r(File.dirname(__FILE__) + '/../resources', REPORT_PATH)
231
+ end
232
+
233
+ end