You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

html_formatter.rb 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require 'erb'
  19. require 'cgi'
  20. # A simple formatter for SimpleCov
  21. module Redmine
  22. module Coverage
  23. class HtmlFormatter
  24. def format(result)
  25. File.open(File.join(output_path, "index.html"), "w") do |file|
  26. file.puts template('index').result(binding)
  27. end
  28. result.source_files.each do |source_file|
  29. File.open(File.join(output_path, source_file_result(source_file)), "w") do |file|
  30. file.puts template('source').result(binding).force_encoding('utf-8')
  31. end
  32. end
  33. puts output_message(result)
  34. end
  35. def output_message(result)
  36. "Coverage report generated for #{result.command_name} to #{output_path}. #{result.covered_lines} / #{result.total_lines} LOC (#{result.covered_percent.round(2)}%) covered."
  37. end
  38. private
  39. def now
  40. @now = Time.now.utc
  41. end
  42. def output_path
  43. SimpleCov.coverage_path
  44. end
  45. def shortened_filename(source_file)
  46. source_file.filename.gsub(SimpleCov.root, '.').delete_prefix('./')
  47. end
  48. def link_to_source_file(source_file)
  49. %(<a href="#{source_file_result source_file}">#{shortened_filename source_file}</a>)
  50. end
  51. def source_file_result(source_file)
  52. shortened_filename(source_file).gsub('/', '__')+'.html'
  53. end
  54. def revision_link
  55. if revision = Redmine::VERSION.revision
  56. %(<a href="http://www.redmine.org/projects/redmine/repository/revisions/#{revision}">r#{revision}</a>)
  57. end
  58. end
  59. # Returns the an erb instance for the template of given name
  60. def template(name)
  61. ERB.new(File.read(File.join(File.dirname(__FILE__), 'views', "#{name}.erb")))
  62. end
  63. end
  64. end
  65. end