diff --git a/assets/file_report.html.erb b/assets/file_report.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/gcovinator.gemspec b/gcovinator.gemspec index e9582a8..c7f404b 100644 --- a/gcovinator.gemspec +++ b/gcovinator.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |spec| spec.homepage = "https://github.com/holtrop/gcovinator" spec.license = "MIT" - spec.files = Dir["{exe,lib}/**/*", "*.gemspec"] + spec.files = Dir["{assets,exe,lib}/**/*", "*.gemspec"] spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] diff --git a/lib/gcovinator.rb b/lib/gcovinator.rb index 877c29b..a7c9556 100644 --- a/lib/gcovinator.rb +++ b/lib/gcovinator.rb @@ -1,9 +1,11 @@ require_relative "gcovinator/file_coverage" +require_relative "gcovinator/file_report" require_relative "gcovinator/gcov_parser" require_relative "gcovinator/version" -require "tmpdir" +require "fileutils" require "open3" require "pathname" +require "tmpdir" module Gcovinator @@ -33,6 +35,10 @@ module Gcovinator end end end + FileUtils.mkdir_p(output_dir) + file_reports = file_coverages.each_with_index.map do |(source_file_name, file_coverage), i| + FileReport.new(source_file_name, file_coverage, source_dirs, output_dir, sprintf("s%04d.html", i)) + end end end diff --git a/lib/gcovinator/file_report.rb b/lib/gcovinator/file_report.rb new file mode 100644 index 0000000..6ae91b2 --- /dev/null +++ b/lib/gcovinator/file_report.rb @@ -0,0 +1,47 @@ +module Gcovinator + class FileReport + + def initialize(source_file_name, file_coverage, source_dirs, output_dir, report_file_name) + @source_file_name = clean_source_file_name(source_file_name, source_dirs) + @report_file_name = report_file_name + @total_lines = 0 + @covered_lines = 0 + @total_branches = 0 + @covered_branches = 0 + run(source_file_name, File.join(output_dir, report_file_name)) + end + + private + + def clean_source_file_name(source_file_name, source_dirs) + source_dirs.each do |source_dir| + prefix_test = "#{source_dir}/" + if source_file_name.start_with?(prefix_test) + return source_file_name[prefix_test.size, source_file_name.size] + end + end + source_file_name + end + + def run(source_file_name, output_file_name) + require "erb" + source_file = read_source_file(source_file_name) + source_file_lines = source_file.lines.to_a + file_report_template = File.read(File.join(File.dirname(__FILE__), "../../assets/file_report.html.erb")) + erb = ERB.new(file_report_template, nil, "<>") + report = erb.run(binding.clone) + File.open(output_file_name, "w") do |fh| + fh.write(report) + end + end + + def read_source_file(source_file_name) + begin + File.read(source_file_name) + rescue + %[] + end + end + + end +end