coverband 4.2.7 → 5.0.0.rc.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.standard.yml +25 -0
  3. data/.travis.yml +2 -1
  4. data/Gemfile +5 -7
  5. data/Gemfile.rails4 +0 -3
  6. data/Gemfile.rails6 +0 -3
  7. data/README.md +8 -77
  8. data/Rakefile +17 -17
  9. data/changes.md +18 -27
  10. data/config.ru +1 -1
  11. data/coverband.gemspec +29 -34
  12. data/lib/coverband.rb +55 -43
  13. data/lib/coverband/adapters/base.rb +17 -17
  14. data/lib/coverband/adapters/file_store.rb +2 -2
  15. data/lib/coverband/adapters/hash_redis_store.rb +24 -21
  16. data/lib/coverband/adapters/redis_store.rb +12 -12
  17. data/lib/coverband/at_exit.rb +1 -1
  18. data/lib/coverband/collectors/coverage.rb +15 -27
  19. data/lib/coverband/collectors/delta.rb +19 -9
  20. data/lib/coverband/collectors/view_tracker.rb +10 -10
  21. data/lib/coverband/configuration.rb +47 -83
  22. data/lib/coverband/integrations/background.rb +3 -3
  23. data/lib/coverband/integrations/rack_server_check.rb +3 -3
  24. data/lib/coverband/reporters/base.rb +10 -10
  25. data/lib/coverband/reporters/console_report.rb +1 -1
  26. data/lib/coverband/reporters/html_report.rb +10 -30
  27. data/lib/coverband/reporters/web.rb +56 -49
  28. data/lib/coverband/utils/absolute_file_converter.rb +6 -6
  29. data/lib/coverband/utils/html_formatter.rb +32 -61
  30. data/lib/coverband/utils/railtie.rb +16 -4
  31. data/lib/coverband/utils/relative_file_converter.rb +2 -2
  32. data/lib/coverband/utils/result.rb +6 -11
  33. data/lib/coverband/utils/results.rb +0 -10
  34. data/lib/coverband/utils/source_file.rb +21 -30
  35. data/lib/coverband/utils/tasks.rb +7 -11
  36. data/lib/coverband/version.rb +1 -1
  37. data/public/application.js +0 -30
  38. data/test/benchmarks/benchmark.rake +97 -92
  39. data/test/benchmarks/dog.rb +1 -1
  40. data/test/benchmarks/init_rails.rake +4 -4
  41. data/test/coverband/adapters/base_test.rb +29 -30
  42. data/test/coverband/adapters/file_store_test.rb +15 -16
  43. data/test/coverband/adapters/hash_redis_store_test.rb +57 -57
  44. data/test/coverband/adapters/redis_store_test.rb +26 -26
  45. data/test/coverband/at_exit_test.rb +2 -2
  46. data/test/coverband/collectors/coverage_test.rb +33 -47
  47. data/test/coverband/collectors/delta_test.rb +51 -23
  48. data/test/coverband/collectors/view_tracker_test.rb +35 -35
  49. data/test/coverband/configuration_test.rb +15 -41
  50. data/test/coverband/coverband_test.rb +11 -11
  51. data/test/coverband/integrations/background_middleware_test.rb +10 -10
  52. data/test/coverband/integrations/background_test.rb +3 -2
  53. data/test/coverband/integrations/rack_server_check_test.rb +7 -7
  54. data/test/coverband/integrations/report_middleware_test.rb +9 -9
  55. data/test/coverband/integrations/resque_worker_test.rb +9 -9
  56. data/test/coverband/integrations/test_resque_job.rb +1 -1
  57. data/test/coverband/reporters/base_test.rb +9 -9
  58. data/test/coverband/reporters/console_test.rb +6 -6
  59. data/test/coverband/reporters/html_test.rb +36 -48
  60. data/test/coverband/reporters/web_test.rb +16 -18
  61. data/test/coverband/utils/absolute_file_converter_test.rb +22 -22
  62. data/test/coverband/utils/file_hasher_test.rb +6 -12
  63. data/test/coverband/utils/file_list_test.rb +13 -13
  64. data/test/coverband/utils/html_formatter_test.rb +9 -23
  65. data/test/coverband/utils/lines_classifier_test.rb +29 -29
  66. data/test/coverband/utils/relative_file_converter_test.rb +13 -13
  67. data/test/coverband/utils/result_test.rb +18 -18
  68. data/test/coverband/utils/results_test.rb +17 -17
  69. data/test/coverband/utils/source_file_line_test.rb +46 -46
  70. data/test/coverband/utils/source_file_test.rb +38 -88
  71. data/test/dog.rb +1 -1
  72. data/test/fake_app/basic_rack.rb +2 -2
  73. data/test/fixtures/app/controllers/sample_controller.rb +1 -1
  74. data/test/fixtures/app/models/user.rb +1 -1
  75. data/test/fixtures/sample.rb +1 -1
  76. data/test/fixtures/utf-8.rb +0 -2
  77. data/test/forked/rails_full_stack_test.rb +24 -27
  78. data/test/forked/rails_rake_full_stack_test.rb +7 -26
  79. data/test/integration/full_stack_test.rb +11 -22
  80. data/test/jruby_check.rb +2 -3
  81. data/test/rails4_dummy/Rakefile +1 -1
  82. data/test/rails4_dummy/config.ru +1 -1
  83. data/test/rails4_dummy/config/application.rb +4 -4
  84. data/test/rails4_dummy/config/boot.rb +2 -2
  85. data/test/rails4_dummy/config/coverband.rb +1 -1
  86. data/test/rails4_dummy/config/coverband_missing_redis.rb +1 -1
  87. data/test/rails4_dummy/config/environment.rb +1 -1
  88. data/test/rails4_dummy/config/routes.rb +2 -2
  89. data/test/rails5_dummy/Rakefile +1 -1
  90. data/test/rails5_dummy/config.ru +1 -1
  91. data/test/rails5_dummy/config/application.rb +3 -3
  92. data/test/rails5_dummy/config/coverband.rb +8 -8
  93. data/test/rails5_dummy/config/coverband_missing_redis.rb +8 -8
  94. data/test/rails5_dummy/config/environment.rb +1 -1
  95. data/test/rails5_dummy/config/routes.rb +2 -2
  96. data/test/rails6_dummy/Rakefile +1 -1
  97. data/test/rails6_dummy/config.ru +1 -1
  98. data/test/rails6_dummy/config/application.rb +4 -4
  99. data/test/rails6_dummy/config/boot.rb +2 -2
  100. data/test/rails6_dummy/config/coverband.rb +1 -1
  101. data/test/rails6_dummy/config/coverband_missing_redis.rb +1 -1
  102. data/test/rails6_dummy/config/environment.rb +1 -1
  103. data/test/rails6_dummy/config/routes.rb +2 -2
  104. data/test/rails_test_helper.rb +11 -11
  105. data/test/test_helper.rb +41 -34
  106. data/test/unique_files.rb +10 -10
  107. data/views/layout.erb +2 -12
  108. metadata +6 -45
  109. data/.rubocop.yml +0 -86
  110. data/lib/coverband/integrations/bundler.rb +0 -8
  111. data/lib/coverband/utils/file_groups.rb +0 -53
  112. data/lib/coverband/utils/gem_list.rb +0 -31
  113. data/lib/coverband/utils/s3_report.rb +0 -105
  114. data/test/coverband/utils/file_groups_test.rb +0 -61
  115. data/test/coverband/utils/gem_list_test.rb +0 -48
  116. data/test/coverband/utils/s3_report_test.rb +0 -44
  117. data/views/gem_list.erb +0 -63
@@ -3,16 +3,16 @@
3
3
  module Coverband
4
4
  class Configuration
5
5
  attr_accessor :root_paths, :root,
6
- :additional_files, :verbose,
7
- :reporter, :redis_namespace, :redis_ttl,
8
- :background_reporting_enabled,
9
- :background_reporting_sleep_seconds, :test_env,
10
- :web_enable_clear, :gem_details, :web_debug, :report_on_exit,
11
- :simulate_oneshot_lines_coverage, :track_views, :view_tracker,
12
- :reporting_wiggle
6
+ :additional_files, :verbose,
7
+ :reporter, :redis_namespace, :redis_ttl,
8
+ :background_reporting_enabled,
9
+ :background_reporting_sleep_seconds, :test_env,
10
+ :web_enable_clear, :gem_details, :web_debug, :report_on_exit,
11
+ :simulate_oneshot_lines_coverage, :track_views, :view_tracker,
12
+ :reporting_wiggle
13
13
 
14
14
  attr_writer :logger, :s3_region, :s3_bucket, :s3_access_key_id,
15
- :s3_secret_access_key, :password
15
+ :s3_secret_access_key, :password
16
16
  attr_reader :track_gems, :ignore, :use_oneshot_lines_coverage
17
17
 
18
18
  #####
@@ -22,20 +22,20 @@ module Coverband
22
22
  # * should we skip /bin/rails webpacker:compile ?
23
23
  # * Perhaps detect heroku deployment ENV var opposed to tasks?
24
24
  #####
25
- IGNORE_TASKS = ['coverband:clear',
26
- 'coverband:coverage',
27
- 'coverband:coverage_server',
28
- 'coverband:migrate',
29
- 'assets:precompile',
30
- 'db:version',
31
- 'db:create',
32
- 'db:drop',
33
- 'db:seed',
34
- 'db:setup',
35
- 'db:test:prepare',
36
- 'db:structure:dump',
37
- 'db:structure:load',
38
- 'db:version']
25
+ IGNORE_TASKS = ["coverband:clear",
26
+ "coverband:coverage",
27
+ "coverband:coverage_server",
28
+ "coverband:migrate",
29
+ "assets:precompile",
30
+ "db:version",
31
+ "db:create",
32
+ "db:drop",
33
+ "db:seed",
34
+ "db:setup",
35
+ "db:test:prepare",
36
+ "db:structure:dump",
37
+ "db:structure:load",
38
+ "db:version"]
39
39
 
40
40
  # Heroku when building assets runs code from a dynamic directory
41
41
  # /tmp was added to avoid coverage from /tmp/build directories during
@@ -57,7 +57,7 @@ module Coverband
57
57
  @search_paths = TRACKED_DEFAULT_PATHS.dup
58
58
  @additional_files = []
59
59
  @verbose = false
60
- @reporter = 'scov'
60
+ @reporter = "scov"
61
61
  @logger = nil
62
62
  @store = nil
63
63
  @background_reporting_enabled = true
@@ -67,21 +67,21 @@ module Coverband
67
67
  @track_gems = false
68
68
  @gem_details = false
69
69
  @track_views = false
70
- @groups = {}
71
70
  @web_debug = false
72
71
  @report_on_exit = true
73
- @use_oneshot_lines_coverage = ENV['ONESHOT'] || false
74
- @simulate_oneshot_lines_coverage = ENV['SIMULATE_ONESHOT'] || false
72
+ @use_oneshot_lines_coverage = ENV["ONESHOT"] || false
73
+ @simulate_oneshot_lines_coverage = ENV["SIMULATE_ONESHOT"] || false
75
74
  @current_root = nil
76
75
  @all_root_paths = nil
77
76
  @all_root_patterns = nil
78
77
  @password = nil
79
78
 
80
- # TODO: should we push these to adapter configs
79
+ # TODO: these are deprecated
81
80
  @s3_region = nil
82
81
  @s3_bucket = nil
83
82
  @s3_access_key_id = nil
84
83
  @s3_secret_access_key = nil
84
+
85
85
  @redis_namespace = nil
86
86
  @redis_ttl = 2_592_000 # in seconds. Default is 30 days.
87
87
  @reporting_wiggle = nil
@@ -89,30 +89,30 @@ module Coverband
89
89
 
90
90
  def logger
91
91
  @logger ||= if defined?(Rails.logger) && Rails.logger
92
- Rails.logger
93
- else
94
- Logger.new(STDOUT)
95
- end
92
+ Rails.logger
93
+ else
94
+ Logger.new(STDOUT)
95
+ end
96
96
  end
97
97
 
98
98
  def password
99
- @password || ENV['COVERBAND_PASSWORD']
99
+ @password || ENV["COVERBAND_PASSWORD"]
100
100
  end
101
101
 
102
102
  def s3_bucket
103
- @s3_bucket || ENV['AWS_BUCKET']
103
+ puts "deprecated, s3 is no longer support"
104
104
  end
105
105
 
106
106
  def s3_region
107
- @s3_region || ENV['AWS_REGION']
107
+ puts "deprecated, s3 is no longer support"
108
108
  end
109
109
 
110
110
  def s3_access_key_id
111
- @s3_access_key_id || ENV['AWS_ACCESS_KEY_ID']
111
+ puts "deprecated, s3 is no longer support"
112
112
  end
113
113
 
114
114
  def s3_secret_access_key
115
- @s3_secret_access_key || ENV['AWS_SECRET_ACCESS_KEY']
115
+ puts "deprecated, s3 is no longer support"
116
116
  end
117
117
 
118
118
  def store
@@ -120,7 +120,7 @@ module Coverband
120
120
  end
121
121
 
122
122
  def store=(store)
123
- raise 'Pass in an instance of Coverband::Adapters' unless store.is_a?(Coverband::Adapters::Base)
123
+ raise "Pass in an instance of Coverband::Adapters" unless store.is_a?(Coverband::Adapters::Base)
124
124
 
125
125
  # Default to 5 minutes if using the hash redis store
126
126
  # This is a safer default for the high server volumes that need the hash store
@@ -134,7 +134,7 @@ module Coverband
134
134
  # Search Paths
135
135
  ###
136
136
  def tracked_search_paths
137
- "#{Coverband.configuration.current_root}/{#{@search_paths.join(',')}}/**/*.{rb}"
137
+ "#{Coverband.configuration.current_root}/{#{@search_paths.join(",")}}/**/*.{rb}"
138
138
  end
139
139
 
140
140
  ###
@@ -151,43 +151,8 @@ module Coverband
151
151
  @ignore = (@ignore + ignored_array).uniq
152
152
  end
153
153
 
154
- def track_gems=(value)
155
- @track_gems = value
156
- return unless @track_gems
157
-
158
- # by default we ignore vendor where many deployments put gems
159
- # we will remove this default if track_gems is set
160
- @ignore.delete('vendor/')
161
- # while we want to allow vendored gems we don't want to track vendored ruby STDLIB
162
- @ignore << 'vendor/ruby-*' unless @ignore.include?('vendor/ruby-*')
163
- add_group('App', root)
164
- # TODO: rework support for multiple gem paths
165
- # this works but seems hacky and error prone
166
- # basically since it is converted to a regex we join all the paths
167
- # with a regex 'OR' using '|'
168
- add_group('Gems', gem_paths.join('|'))
169
- end
170
-
171
- #
172
- # Returns the configured groups. Add groups using SimpleCov.add_group
173
- #
174
- def groups
175
- @groups ||= {}
176
- end
177
-
178
- #
179
- # Define a group for files. Works similar to add_filter, only that the first
180
- # argument is the desired group name and files PASSING the filter end up in the group
181
- # (while filters exclude when the filter is applicable).
182
- #
183
- def add_group(group_name, filter_argument = nil)
184
- groups[group_name] = filter_argument
185
- end
186
-
187
- def gem_paths
188
- # notes ignore any paths that aren't on this system, resolves
189
- # bug related to multiple ruby version managers / bad dot files
190
- Gem::PathSupport.new(ENV).path.select { |path| File.exist?(path) }
154
+ def track_gems=(_value)
155
+ puts "gem tracking is deprecated, setting this will be ignored"
191
156
  end
192
157
 
193
158
  def current_root
@@ -198,7 +163,6 @@ module Coverband
198
163
  return @all_root_paths if @all_root_paths
199
164
 
200
165
  @all_root_paths = Coverband.configuration.root_paths.dup
201
- @all_root_paths += Coverband.configuration.gem_paths.dup if Coverband.configuration.track_gems
202
166
  @all_root_paths << "#{Coverband.configuration.current_root}/"
203
167
  @all_root_paths
204
168
  end
@@ -210,30 +174,30 @@ module Coverband
210
174
  SKIPPED_SETTINGS = %w[@s3_secret_access_key @store]
211
175
  def to_h
212
176
  instance_variables
213
- .each_with_object('gem_paths': gem_paths) do |var, hash|
214
- hash[var.to_s.delete('@')] = instance_variable_get(var) unless SKIPPED_SETTINGS.include?(var.to_s)
177
+ .each_with_object({}) do |var, hash|
178
+ hash[var.to_s.delete("@")] = instance_variable_get(var) unless SKIPPED_SETTINGS.include?(var.to_s)
215
179
  end
216
180
  end
217
181
 
218
182
  def use_oneshot_lines_coverage=(value)
219
- raise(Exception, 'One shot line coverage is only available in ruby >= 2.6') unless one_shot_coverage_implemented_in_ruby_version? || !value
183
+ raise(StandardError, "One shot line coverage is only available in ruby >= 2.6") unless one_shot_coverage_implemented_in_ruby_version? || !value
220
184
 
221
185
  @use_oneshot_lines_coverage = value
222
186
  end
223
187
 
224
188
  def one_shot_coverage_implemented_in_ruby_version?
225
- Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0')
189
+ Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.6.0")
226
190
  end
227
191
 
228
192
  private
229
193
 
230
194
  def redis_url
231
- ENV['COVERBAND_REDIS_URL'] || ENV['REDIS_URL']
195
+ ENV["COVERBAND_REDIS_URL"] || ENV["REDIS_URL"]
232
196
  end
233
197
 
234
198
  def redis_store_options
235
- { ttl: Coverband.configuration.redis_ttl,
236
- redis_namespace: Coverband.configuration.redis_namespace }
199
+ {ttl: Coverband.configuration.redis_ttl,
200
+ redis_namespace: Coverband.configuration.redis_namespace}
237
201
  end
238
202
  end
239
203
  end
@@ -27,9 +27,9 @@ module Coverband
27
27
  @semaphore.synchronize do
28
28
  return if running?
29
29
 
30
- logger.debug('Coverband: Starting background reporting') if Coverband.configuration.verbose
30
+ logger.debug("Coverband: Starting background reporting") if Coverband.configuration.verbose
31
31
  sleep_seconds = Coverband.configuration.background_reporting_sleep_seconds
32
- @thread = Thread.new do
32
+ @thread = Thread.new {
33
33
  loop do
34
34
  Coverband.report_coverage
35
35
  Coverband.configuration.view_tracker&.report_views_tracked
@@ -41,7 +41,7 @@ module Coverband
41
41
  end
42
42
  sleep(sleep_seconds)
43
43
  end
44
- end
44
+ }
45
45
  end
46
46
  end
47
47
  end
@@ -15,14 +15,14 @@ module Coverband
15
15
  end
16
16
 
17
17
  def rack_server?
18
- @stack.any? { |line| line.path.include?('lib/rack/') }
18
+ @stack.any? { |line| line.path.include?("lib/rack/") }
19
19
  end
20
20
 
21
21
  def rails_server?
22
22
  @stack.any? do |location|
23
23
  (
24
- location.path.include?('rails/commands/commands_tasks.rb') && location.label == 'server' ||
25
- location.path.include?('rails/commands/server/server_command.rb') && location.label == 'perform'
24
+ location.path.include?("rails/commands/commands_tasks.rb") && location.label == "server" ||
25
+ location.path.include?("rails/commands/server/server_command.rb") && location.label == "perform"
26
26
  )
27
27
  end
28
28
  end
@@ -8,7 +8,7 @@ module Coverband
8
8
  ###
9
9
  class Base
10
10
  class << self
11
- DATA_KEY = 'data'
11
+ DATA_KEY = "data"
12
12
 
13
13
  def report(store, _options = {})
14
14
  all_roots = Coverband.configuration.all_root_paths
@@ -49,7 +49,7 @@ module Coverband
49
49
  protected
50
50
 
51
51
  def fix_file_names(report_hash, roots)
52
- Coverband.configuration.logger.debug "fixing root: #{roots.join(', ')}" if Coverband.configuration.verbose
52
+ Coverband.configuration.logger.debug "fixing root: #{roots.join(", ")}" if Coverband.configuration.verbose
53
53
 
54
54
  # normalize names across servers
55
55
  report_hash.each_with_object({}) do |(name, report), fixed_report|
@@ -57,12 +57,12 @@ module Coverband
57
57
  report.each_pair do |key, vals|
58
58
  filename = Coverband::Utils::AbsoluteFileConverter.convert(key)
59
59
  fixed_report[name][filename] = if fixed_report[name].key?(filename) && fixed_report[name][filename][DATA_KEY] && vals[DATA_KEY]
60
- merged_data = merge_arrays(fixed_report[name][filename][DATA_KEY], vals[DATA_KEY])
61
- vals[DATA_KEY] = merged_data
62
- vals
63
- else
64
- vals
65
- end
60
+ merged_data = merge_arrays(fixed_report[name][filename][DATA_KEY], vals[DATA_KEY])
61
+ vals[DATA_KEY] = merged_data
62
+ vals
63
+ else
64
+ vals
65
+ end
66
66
  end
67
67
  end
68
68
  end
@@ -75,8 +75,8 @@ module Coverband
75
75
 
76
76
  longest.each_with_index do |_line, index|
77
77
  merged[index] = if first[index] || second[index]
78
- (first[index].to_i + second[index].to_i)
79
- end
78
+ (first[index].to_i + second[index].to_i)
79
+ end
80
80
  end
81
81
 
82
82
  merged
@@ -12,7 +12,7 @@ module Coverband
12
12
  scov_style_report.each_pair do |file, usage|
13
13
  # TODO: In Coverband 5 deprecate none hash format
14
14
  if usage.is_a?(Hash)
15
- Coverband.configuration.logger.info "#{file}: #{usage['data']}"
15
+ Coverband.configuration.logger.info "#{file}: #{usage["data"]}"
16
16
  else
17
17
  Coverband.configuration.logger.info "#{file}: #{usage}"
18
18
  end
@@ -3,16 +3,15 @@
3
3
  module Coverband
4
4
  module Reporters
5
5
  class HTMLReport < Base
6
- attr_accessor :filtered_report_files, :open_report, :static, :notice,
7
- :base_path, :filename
6
+ attr_accessor :filtered_report_files, :open_report, :notice,
7
+ :base_path, :filename
8
8
 
9
9
  def initialize(store, options = {})
10
10
  coverband_reports = Coverband::Reporters::Base.report(store, options)
11
11
  self.open_report = options.fetch(:open_report) { true }
12
- self.static = options.fetch(:static) { true }
13
12
  # TODO: refactor notice out to top level of web only
14
13
  self.notice = options.fetch(:notice) { nil }
15
- self.base_path = options.fetch(:base_path) { nil }
14
+ self.base_path = options.fetch(:base_path) { "./" }
16
15
  self.filename = options.fetch(:filename) { nil }
17
16
 
18
17
  self.filtered_report_files = self.class.fix_reports(coverband_reports)
@@ -20,16 +19,12 @@ module Coverband
20
19
 
21
20
  def file_details
22
21
  Coverband::Utils::HTMLFormatter.new(filtered_report_files,
23
- base_path: base_path,
24
- notice: notice).format_source_file!(filename)
22
+ base_path: base_path,
23
+ notice: notice).format_source_file!(filename)
25
24
  end
26
25
 
27
26
  def report
28
- if static?
29
- report_static_site
30
- else
31
- report_dynamic_html
32
- end
27
+ report_dynamic_html
33
28
  end
34
29
 
35
30
  def report_data
@@ -38,31 +33,16 @@ module Coverband
38
33
 
39
34
  private
40
35
 
41
- def static?
42
- static
43
- end
44
-
45
- def report_static_site
46
- Coverband::Utils::HTMLFormatter.new(filtered_report_files).format_static_html!
47
- if open_report
48
- `open #{Coverband.configuration.root}/coverage/index.html`
49
- else
50
- Coverband.configuration.logger.info 'report is ready and viewable: open coverage/index.html'
51
- end
52
-
53
- Coverband::Utils::S3Report.instance.persist! if Coverband.configuration.s3_bucket
54
- end
55
-
56
36
  def report_dynamic_html
57
37
  Coverband::Utils::HTMLFormatter.new(filtered_report_files,
58
- base_path: base_path,
59
- notice: notice).format_dynamic_html!
38
+ base_path: base_path,
39
+ notice: notice).format_dynamic_html!
60
40
  end
61
41
 
62
42
  def report_dynamic_data
63
43
  Coverband::Utils::HTMLFormatter.new(filtered_report_files,
64
- base_path: base_path,
65
- notice: notice).format_dynamic_data!
44
+ base_path: base_path,
45
+ notice: notice).format_dynamic_data!
66
46
  end
67
47
  end
68
48
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  begin
4
- require 'rack'
4
+ require "rack"
5
5
  rescue LoadError
6
- puts 'error loading Coverband web reporter as Rack is not available'
6
+ puts "error loading Coverband web reporter as Rack is not available"
7
7
  end
8
8
 
9
9
  module Coverband
@@ -12,27 +12,31 @@ module Coverband
12
12
  attr_reader :request
13
13
 
14
14
  def initialize
15
- full_path = Gem::Specification.find_by_name('coverband').full_gem_path
15
+ init_web
16
+ end
17
+
18
+ def init_web
19
+ full_path = Gem::Specification.find_by_name("coverband").full_gem_path
16
20
  @static = Rack::Static.new(self,
17
- root: File.expand_path('public', full_path),
18
- urls: [/.*\.css/, /.*\.js/, /.*\.gif/, /.*\.png/])
21
+ root: File.expand_path("public", full_path),
22
+ urls: [/.*\.css/, /.*\.js/, /.*\.gif/, /.*\.png/])
19
23
  end
20
24
 
21
25
  def check_auth
22
26
  return true unless Coverband.configuration.password
23
27
 
24
- auth_header = request.get_header('HTTP_AUTHORIZATION')
28
+ auth_header = request.get_header("HTTP_AUTHORIZATION")
25
29
  return unless auth_header
26
30
 
27
- Coverband.configuration.password == Base64.decode64(auth_header.split[1]).split(':')[1]
31
+ Coverband.configuration.password == Base64.decode64(auth_header.split[1]).split(":")[1]
28
32
  end
29
33
 
30
34
  def call(env)
31
35
  @request = Rack::Request.new(env)
32
36
 
33
- return [401, { 'www-authenticate' => 'Basic realm=""' }, ['']] unless check_auth
37
+ return [401, {"www-authenticate" => 'Basic realm=""'}, [""]] unless check_auth
34
38
 
35
- request_path_info = (request.path_info == '') ? '/' : request.path_info
39
+ request_path_info = request.path_info == "" ? "/" : request.path_info
36
40
  if request.post?
37
41
  case request_path_info
38
42
  when %r{\/clear_view_tracking_file}
@@ -44,40 +48,40 @@ module Coverband
44
48
  when %r{\/clear}
45
49
  clear
46
50
  else
47
- [404, { 'Content-Type' => 'text/html' }, ['404 error!']]
51
+ [404, {"Content-Type" => "text/html"}, ["404 error!"]]
48
52
  end
49
53
  else
50
54
  case request_path_info
51
55
  when /.*\.(css|js|gif|png)/
52
56
  @static.call(env)
53
57
  when %r{\/settings}
54
- [200, { 'Content-Type' => 'text/html' }, [settings]]
58
+ [200, {"Content-Type" => "text/html"}, [settings]]
55
59
  when %r{\/view_tracker_data}
56
- [200, { 'Content-Type' => 'text/json' }, [view_tracker_data]]
60
+ [200, {"Content-Type" => "text/json"}, [view_tracker_data]]
57
61
  when %r{\/view_tracker}
58
- [200, { 'Content-Type' => 'text/html' }, [view_tracker]]
62
+ [200, {"Content-Type" => "text/html"}, [view_tracker]]
59
63
  when %r{\/enriched_debug_data}
60
- [200, { 'Content-Type' => 'text/json' }, [enriched_debug_data]]
64
+ [200, {"Content-Type" => "text/json"}, [enriched_debug_data]]
61
65
  when %r{\/debug_data}
62
- [200, { 'Content-Type' => 'text/json' }, [debug_data]]
66
+ [200, {"Content-Type" => "text/json"}, [debug_data]]
63
67
  when %r{\/load_file_details}
64
- [200, { 'Content-Type' => 'text/json' }, [load_file_details]]
68
+ [200, {"Content-Type" => "text/json"}, [load_file_details]]
65
69
  when %r{\/$}
66
- [200, { 'Content-Type' => 'text/html' }, [index]]
70
+ [200, {"Content-Type" => "text/html"}, [index]]
67
71
  else
68
- [404, { 'Content-Type' => 'text/html' }, ['404 error!']]
72
+ [404, {"Content-Type" => "text/html"}, ["404 error!"]]
69
73
  end
70
74
  end
71
75
  end
72
76
 
73
77
  def index
74
- notice = "<strong>Notice:</strong> #{Rack::Utils.escape_html(request.params['notice'])}<br/>"
75
- notice = request.params['notice'] ? notice : ''
78
+ notice = "<strong>Notice:</strong> #{Rack::Utils.escape_html(request.params["notice"])}<br/>"
79
+ notice = request.params["notice"] ? notice : ""
76
80
  Coverband::Reporters::HTMLReport.new(Coverband.configuration.store,
77
- static: false,
78
- base_path: base_path,
79
- notice: notice,
80
- open_report: false).report
81
+ static: false,
82
+ base_path: base_path,
83
+ notice: notice,
84
+ open_report: false).report
81
85
  end
82
86
 
83
87
  def settings
@@ -85,11 +89,11 @@ module Coverband
85
89
  end
86
90
 
87
91
  def view_tracker
88
- notice = "<strong>Notice:</strong> #{Rack::Utils.escape_html(request.params['notice'])}<br/>"
89
- notice = request.params['notice'] ? notice : ''
92
+ notice = "<strong>Notice:</strong> #{Rack::Utils.escape_html(request.params["notice"])}<br/>"
93
+ notice = request.params["notice"] ? notice : ""
90
94
  Coverband::Utils::HTMLFormatter.new(nil,
91
- notice: notice,
92
- base_path: base_path).format_view_tracker!
95
+ notice: notice,
96
+ base_path: base_path).format_view_tracker!
93
97
  end
94
98
 
95
99
  def view_tracker_data
@@ -102,62 +106,62 @@ module Coverband
102
106
 
103
107
  def enriched_debug_data
104
108
  Coverband::Reporters::HTMLReport.new(Coverband.configuration.store,
105
- static: false,
106
- base_path: base_path,
107
- notice: '',
108
- open_report: false).report_data
109
+ static: false,
110
+ base_path: base_path,
111
+ notice: "",
112
+ open_report: false).report_data
109
113
  end
110
114
 
111
115
  def load_file_details
112
- filename = request.params['filename']
116
+ filename = request.params["filename"]
113
117
  Coverband::Reporters::HTMLReport.new(Coverband.configuration.store,
114
- filename: filename,
115
- base_path: base_path,
116
- open_report: false).file_details
118
+ filename: filename,
119
+ base_path: base_path,
120
+ open_report: false).file_details
117
121
  end
118
122
 
119
123
  def clear
120
124
  if Coverband.configuration.web_enable_clear
121
125
  Coverband.configuration.store.clear!
122
- notice = 'coverband coverage cleared'
126
+ notice = "coverband coverage cleared"
123
127
  else
124
- notice = 'web_enable_clear isnt enabled in your configuration'
128
+ notice = "web_enable_clear isnt enabled in your configuration"
125
129
  end
126
- [301, { 'Location' => "#{base_path}?notice=#{notice}" }, []]
130
+ [301, {"Location" => "#{base_path}?notice=#{notice}"}, []]
127
131
  end
128
132
 
129
133
  def clear_file
130
134
  if Coverband.configuration.web_enable_clear
131
- filename = request.params['filename']
135
+ filename = request.params["filename"]
132
136
  Coverband.configuration.store.clear_file!(filename)
133
137
  notice = "coverage for file #{filename} cleared"
134
138
  else
135
- notice = 'web_enable_clear isnt enabled in your configuration'
139
+ notice = "web_enable_clear isnt enabled in your configuration"
136
140
  end
137
- [301, { 'Location' => "#{base_path}?notice=#{notice}" }, []]
141
+ [301, {"Location" => "#{base_path}?notice=#{notice}"}, []]
138
142
  end
139
143
 
140
144
  def clear_view_tracking
141
145
  if Coverband.configuration.web_enable_clear
142
146
  tracker = Coverband::Collectors::ViewTracker.new(store: Coverband.configuration.store)
143
147
  tracker.reset_recordings
144
- notice = 'view tracking reset'
148
+ notice = "view tracking reset"
145
149
  else
146
- notice = 'web_enable_clear isnt enabled in your configuration'
150
+ notice = "web_enable_clear isnt enabled in your configuration"
147
151
  end
148
- [301, { 'Location' => "#{base_path}/view_tracker?notice=#{notice}" }, []]
152
+ [301, {"Location" => "#{base_path}/view_tracker?notice=#{notice}"}, []]
149
153
  end
150
154
 
151
155
  def clear_view_tracking_file
152
156
  if Coverband.configuration.web_enable_clear
153
157
  tracker = Coverband::Collectors::ViewTracker.new(store: Coverband.configuration.store)
154
- filename = request.params['filename']
158
+ filename = request.params["filename"]
155
159
  tracker.clear_file!(filename)
156
160
  notice = "coverage for file #{filename} cleared"
157
161
  else
158
- notice = 'web_enable_clear isnt enabled in your configuration'
162
+ notice = "web_enable_clear isnt enabled in your configuration"
159
163
  end
160
- [301, { 'Location' => "#{base_path}/view_tracker?notice=#{notice}" }, []]
164
+ [301, {"Location" => "#{base_path}/view_tracker?notice=#{notice}"}, []]
161
165
  end
162
166
 
163
167
  private
@@ -167,8 +171,11 @@ module Coverband
167
171
  # mount Coverband::Web, at: '/coverage'
168
172
  # "/coverage/collect_coverage?" become:
169
173
  # /coverage/
174
+ # NOTE: DO NOT let standardrb `autofix` this to regex match
175
+ # %r{\/.*\/}.match?(request.path) ? request.path.match("\/.*\/")[0] : "/"
176
+ # ^^ the above is NOT valid Ruby 2.3/2.4 even though rubocop / standard think it is
170
177
  def base_path
171
- request.path =~ %r{\/.*\/} ? request.path.match("\/.*\/")[0] : '/'
178
+ request.path =~ %r{\/.*\/} ? request.path.match("\/.*\/")[0] : "/"
172
179
  end
173
180
  end
174
181
  end