rspec_pretty_html_reporter 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db940330b5e7aa94854290881ddda08c64ca4c6b3aabe8ac34ceb8d974904c05
4
- data.tar.gz: d3c7e3706e03b04eda6ca066340acc2aaef079f157fdaea74e8751f5c0c716a0
3
+ metadata.gz: 989a30971d82398d7880345a90ff3fbea0a3a790ea0e381b90e2a15714d53b6b
4
+ data.tar.gz: 6c31be855be029651381cadd72492c999a4cbb311bcc91ea058ae5fe9ed9f294
5
5
  SHA512:
6
- metadata.gz: e35c17c46ae6066e216096fef5bea93a3bf0093dbd807c8f24d527218e16bd555cc808464a822d761b4ac4954edf1e533d433510cccf27d8d49de48ac9af627f
7
- data.tar.gz: 6719d78b509340e5d892f6c4994ca6f1f314c9b4f72f09fce1202db57848fdee0f74ed635ddc3694f11d17eb656b82296912890c0122eafc06a2cd825abef4d1
6
+ metadata.gz: 99859592e5abba752b44110a13f6abbc707e6557bdf1984903576beed48bf32f84f8a8a715d577d5bda500828e1aa128abdbd648445c27b2a6a61099d4f9edfc
7
+ data.tar.gz: 1f5169670f431cef993d5b56b49401353f11a198a9dafdc160e7c2c8de19ebbee0837af928a82a92a8d712b2267720c23e0c9cb161b706fbf50107e44e236d8b
data/README.md CHANGED
@@ -36,6 +36,21 @@ REPORT_PATH=reports/$(date +%s) bundle exec rspec --format RspecPrettyHtmlReport
36
36
 
37
37
  This will create the reports in the `reports` directory.
38
38
 
39
+ ## Parallel tests
40
+
41
+ The reporter supports [parallel_tests](https://github.com/grosser/parallel_tests). Give every worker the same
42
+ `REPORT_PATH`, because the default path holds the start time of each process:
43
+
44
+ ```bash
45
+ REPORT_PATH=reports/latest bundle exec parallel_rspec -- --format RspecPrettyHtmlReporter --
46
+ ```
47
+
48
+ The first worker of a run empties the report directory. Every worker adds the reports of its spec groups, and the
49
+ last worker to finish writes the overview of all the groups. The workers use a `<REPORT_PATH>.lock` file next to the
50
+ report directory.
51
+
52
+ A worker saves its groups when it finishes. If a worker crashes, the overview does not list its groups.
53
+
39
54
  ## Usage
40
55
 
41
56
  Images and videos can be embed by adding their path into example's metadata. For an example of how to do this, please
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'json'
5
+
6
+ ##
7
+ # Lets the workers of one parallel_tests run share a report directory.
8
+ #
9
+ # Every worker of a run gets the same PARALLEL_PID_FILE, so it identifies the run.
10
+ # The first worker of a new run empties the report directory. The other workers keep
11
+ # what it contains. Each worker saves its groups as JSON, and each worker writes the
12
+ # overview from the groups of all the workers that have finished. The last worker to
13
+ # finish writes the complete overview.
14
+ #
15
+ # Outside parallel_tests, or with a parallel_tests version that does not set
16
+ # PARALLEL_PID_FILE, the reporter works as one process.
17
+ ##
18
+ module RspecPrettyHtmlParallel
19
+ STATE_DIR = '.parallel'
20
+
21
+ def self.worker?
22
+ !ENV['PARALLEL_TEST_GROUPS'].to_s.empty? && !ENV['PARALLEL_PID_FILE'].to_s.empty?
23
+ end
24
+
25
+ ##
26
+ # Runs the block while it holds a lock that all the workers share.
27
+ # A single process does not lock.
28
+ # @param [String] report_path The report directory
29
+ ##
30
+ def self.synchronize(report_path)
31
+ return yield unless worker?
32
+
33
+ # A trailing separator would put the lock file inside the report directory,
34
+ # which the first worker of a run deletes.
35
+ report_path = File.expand_path(report_path)
36
+ FileUtils.mkpath(File.dirname(report_path))
37
+ File.open("#{report_path}.lock", File::RDWR | File::CREAT) do |lock|
38
+ lock.flock(File::LOCK_EX)
39
+ yield
40
+ end
41
+ end
42
+
43
+ ##
44
+ # @param [String] report_path The report directory
45
+ # @return [Boolean] True when another worker of this run has prepared the report directory
46
+ ##
47
+ def self.current_run?(report_path)
48
+ File.read(run_file(report_path)) == ENV['PARALLEL_PID_FILE']
49
+ rescue Errno::ENOENT
50
+ false
51
+ end
52
+
53
+ ##
54
+ # Records that the report directory belongs to this run.
55
+ # @param [String] report_path The report directory
56
+ ##
57
+ def self.mark_run(report_path)
58
+ FileUtils.mkpath(File.join(report_path, STATE_DIR))
59
+ File.write(run_file(report_path), ENV['PARALLEL_PID_FILE'])
60
+ end
61
+
62
+ ##
63
+ # Saves the groups of this worker, then returns the groups of all the workers that have finished.
64
+ # A worker that dies while it writes leaves only a temporary file, which the merge ignores.
65
+ # The merge also skips a groups file that does not parse, so one bad file costs only its own groups.
66
+ # @param [String] report_path The report directory
67
+ # @param [Hash] groups The groups of this worker, by the name of their report file
68
+ # @return [Hash] The groups of all the finished workers, in worker order
69
+ ##
70
+ def self.merge_groups(report_path, groups)
71
+ path = groups_file(report_path, worker_number)
72
+ File.write("#{path}.tmp", JSON.generate(groups))
73
+ File.rename("#{path}.tmp", path)
74
+
75
+ Dir[groups_file(report_path, '*')].sort_by { |file| file[/(\d+)\.json\z/, 1].to_i }.each_with_object({}) do |file, all|
76
+ JSON.parse(File.read(file), symbolize_names: true).each { |name, group| all[name.to_s] = group }
77
+ rescue JSON::ParserError
78
+ next
79
+ end
80
+ end
81
+
82
+ ##
83
+ # The first worker has an empty TEST_ENV_NUMBER, unless parallel_tests runs with --first-is-1.
84
+ ##
85
+ def self.worker_number
86
+ ENV['TEST_ENV_NUMBER'].to_s.empty? ? 1 : ENV['TEST_ENV_NUMBER'].to_i
87
+ end
88
+
89
+ def self.run_file(report_path)
90
+ File.join(report_path, STATE_DIR, 'run')
91
+ end
92
+
93
+ def self.groups_file(report_path, number)
94
+ File.join(report_path, STATE_DIR, "groups-#{number}.json")
95
+ end
96
+ end
@@ -7,6 +7,7 @@ require 'fileutils'
7
7
  require 'erb'
8
8
  require 'rspec_pretty_html_reporter/example'
9
9
  require 'rspec_pretty_html_reporter/helpers'
10
+ require 'rspec_pretty_html_reporter/parallel'
10
11
 
11
12
  I18n.enforce_available_locales = false
12
13
 
@@ -23,10 +24,9 @@ class RspecPrettyHtmlReporter < RSpec::Core::Formatters::BaseFormatter
23
24
  :example_group_finished
24
25
 
25
26
  def initialize(_io_standard_out)
26
- create_reports_dir
27
+ prepare_reports_dir
27
28
  create_screenshots_dir
28
29
  create_screenrecords_dir
29
- copy_resources
30
30
  @all_groups = {}
31
31
  @group_level = 0
32
32
  @theme = Helpers.theme(ENV['THEME'])
@@ -68,7 +68,7 @@ class RspecPrettyHtmlReporter < RSpec::Core::Formatters::BaseFormatter
68
68
  @group_level -= 1
69
69
 
70
70
  if @group_level.zero?
71
- @filename = Helpers.rename_duplicate_filename("#{REPORT_PATH}/#{notification.group.description.parameterize}.html")
71
+ @filename = reserve_filename("#{REPORT_PATH}/#{notification.group.description.parameterize}.html")
72
72
 
73
73
  File.open(@filename, 'w') do |f|
74
74
  @passed = @group_example_success_count
@@ -98,7 +98,8 @@ class RspecPrettyHtmlReporter < RSpec::Core::Formatters::BaseFormatter
98
98
  else
99
99
  (statuses.include?('passed') ? 'passed' : 'pending')
100
100
  end
101
- @spec_description = Helpers.rename_duplicate_description(@all_groups, notification.group.description.parameterize)
101
+ # The overview links to the report file, so the file name is the key. It stays unique across parallel workers.
102
+ @spec_description = File.basename(@filename, '.html')
102
103
  @all_groups[@spec_description] = {
103
104
  group: notification.group.description,
104
105
  examples: @examples.size,
@@ -118,9 +119,15 @@ class RspecPrettyHtmlReporter < RSpec::Core::Formatters::BaseFormatter
118
119
  end
119
120
  end
120
121
 
121
- def close(notification)
122
+ def close(_notification)
123
+ RspecPrettyHtmlParallel.synchronize(REPORT_PATH) { write_overview }
124
+ end
125
+
126
+ private
127
+
128
+ def write_overview
122
129
  File.open("#{REPORT_PATH}/overview.html", 'w') do |f|
123
- @overview = @all_groups
130
+ @overview = RspecPrettyHtmlParallel.worker? ? RspecPrettyHtmlParallel.merge_groups(REPORT_PATH, @all_groups) : @all_groups
124
131
 
125
132
  @passed = @overview.values.map { |g| g[:passed].size }.inject(0) { |sum, i| sum + i }
126
133
  @failed = @overview.values.map { |g| g[:failed].size }.inject(0) { |sum, i| sum + i }
@@ -144,11 +151,25 @@ class RspecPrettyHtmlReporter < RSpec::Core::Formatters::BaseFormatter
144
151
  end
145
152
  end
146
153
 
147
- private
154
+ # A parallel worker empties the report directory only when it is the first worker of a new run.
155
+ def prepare_reports_dir
156
+ RspecPrettyHtmlParallel.synchronize(REPORT_PATH) do
157
+ create_reports_dir unless RspecPrettyHtmlParallel.worker? && RspecPrettyHtmlParallel.current_run?(REPORT_PATH)
158
+ end
159
+ end
148
160
 
149
161
  def create_reports_dir
150
162
  FileUtils.rm_rf(REPORT_PATH) if File.exist?(REPORT_PATH)
151
163
  FileUtils.mkpath(REPORT_PATH)
164
+ copy_resources
165
+ RspecPrettyHtmlParallel.mark_run(REPORT_PATH) if RspecPrettyHtmlParallel.worker?
166
+ end
167
+
168
+ # Picks a free file name and creates the file, so a parallel worker cannot pick the same name.
169
+ def reserve_filename(filename)
170
+ RspecPrettyHtmlParallel.synchronize(REPORT_PATH) do
171
+ Helpers.rename_duplicate_filename(filename).tap { |name| FileUtils.touch(name) }
172
+ end
152
173
  end
153
174
 
154
175
  def create_screenshots_dir
@@ -11,8 +11,8 @@
11
11
  <link href="resources/bootstrap-4.0.0-dist/themes/bootstrap-theme-<%= @theme %>.min.css" rel="stylesheet">
12
12
 
13
13
  <script src="resources/js/jquery-1.11.1.min.js"></script>
14
+ <script src="resources/tether-1.3.3-dist/js/tether.min.js"></script>
14
15
  <script src="resources/bootstrap-4.0.0-dist/js/bootstrap.min.js"></script>
15
- <script src="resources/tether-1.3.3/js/tether.min.js"></script>
16
16
  <script src="resources/js/chart-3.7.1.js"></script>
17
17
  <style>
18
18
  * {
data/templates/report.erb CHANGED
@@ -11,8 +11,8 @@
11
11
  <link href="resources/bootstrap-4.0.0-dist/themes/bootstrap-theme-<%= @theme %>.min.css" rel="stylesheet">
12
12
 
13
13
  <script src="resources/js/jquery-1.11.1.min.js"></script>
14
+ <script src="resources/tether-1.3.3-dist/js/tether.min.js"></script>
14
15
  <script src="resources/bootstrap-4.0.0-dist/js/bootstrap.min.js"></script>
15
- <script src="resources/tether-1.3.3/js/tether.min.js"></script>
16
16
  <script src="resources/js/chart-3.7.1.js"></script>
17
17
  <style>
18
18
  * {
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec_pretty_html_reporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Gutierrez
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2026-11-23 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: activesupport
@@ -18,7 +19,7 @@ dependencies:
18
19
  version: 4.1.4
19
20
  - - "<"
20
21
  - !ruby/object:Gem::Version
21
- version: '8.0'
22
+ version: '9.0'
22
23
  type: :runtime
23
24
  prerelease: false
24
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -28,21 +29,27 @@ dependencies:
28
29
  version: 4.1.4
29
30
  - - "<"
30
31
  - !ruby/object:Gem::Version
31
- version: '8.0'
32
+ version: '9.0'
32
33
  - !ruby/object:Gem::Dependency
33
34
  name: bundler
34
35
  requirement: !ruby/object:Gem::Requirement
35
36
  requirements:
36
- - - "~>"
37
+ - - ">="
37
38
  - !ruby/object:Gem::Version
38
39
  version: '2.2'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '5'
39
43
  type: :runtime
40
44
  prerelease: false
41
45
  version_requirements: !ruby/object:Gem::Requirement
42
46
  requirements:
43
- - - "~>"
47
+ - - ">="
44
48
  - !ruby/object:Gem::Version
45
49
  version: '2.2'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '5'
46
53
  - !ruby/object:Gem::Dependency
47
54
  name: rouge
48
55
  requirement: !ruby/object:Gem::Requirement
@@ -226,6 +233,7 @@ files:
226
233
  - lib/rspec_pretty_html_reporter/example.rb
227
234
  - lib/rspec_pretty_html_reporter/helpers.rb
228
235
  - lib/rspec_pretty_html_reporter/oopsy.rb
236
+ - lib/rspec_pretty_html_reporter/parallel.rb
229
237
  - resources/bootstrap-4.0.0-dist/css/bootstrap.min.css
230
238
  - resources/bootstrap-4.0.0-dist/js/bootstrap.js
231
239
  - resources/bootstrap-4.0.0-dist/js/bootstrap.min.js
@@ -244,6 +252,7 @@ homepage: https://github.com/TheSpartan1980/rspec_pretty_html_reporter
244
252
  licenses:
245
253
  - MIT
246
254
  metadata: {}
255
+ post_install_message:
247
256
  rdoc_options: []
248
257
  require_paths:
249
258
  - lib
@@ -258,7 +267,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
258
267
  - !ruby/object:Gem::Version
259
268
  version: '0'
260
269
  requirements: []
261
- rubygems_version: 3.6.9
270
+ rubygems_version: 3.5.3
271
+ signing_key:
262
272
  specification_version: 4
263
273
  summary: RSpec Pretty HTML Reporter
264
274
  test_files: []