add --prefix option

This commit is contained in:
Josh Holtrop 2017-01-15 15:50:49 -05:00
parent 6edbcff533
commit 4614eaea60
3 changed files with 15 additions and 11 deletions

View File

@ -12,7 +12,7 @@ module Gcovinator
class << self
def run(build_dir, source_dirs, files, output_dir)
def run(build_dir, source_dirs, files, output_dir, prefix)
build_dir = Pathname.new(File.expand_path(build_dir)).cleanpath.to_s
source_dirs = ["."] if source_dirs.empty?
source_dirs = source_dirs.map do |s|
@ -23,6 +23,7 @@ module Gcovinator
Pathname.new(File.expand_path(f)).cleanpath.to_s
end
output_dir = Pathname.new(File.expand_path(output_dir)).cleanpath.to_s
prefix = Pathname.new(File.expand_path(prefix)).cleanpath.to_s
file_coverages = {}
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
@ -38,7 +39,7 @@ module Gcovinator
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))
FileReport.new(source_file_name, file_coverage, prefix, output_dir, sprintf("s%04d.html", i))
end
IndexReport.new(output_dir, file_reports)
end

View File

@ -11,6 +11,7 @@ module Gcovinator
build_dir = "."
source_dirs = []
output_dir = "coverage"
prefix = "."
OptionParser.new do |opts|
@ -35,6 +36,10 @@ module Gcovinator
output_dir = o
end
opts.on("-p PREFIX", "--prefix PREFIX", "Prefix path to strip from source file paths. Defaults to '.' if not specified.") do |p|
prefix = p
end
opts.on("-s SRCDIR", "--source-dir SRCDIR", "Specify a source directory. Reports will only be generated for sources under a specified source directory. Multiple source directories may be specified. Defaults to '.' if not specified.") do |s|
source_dirs << s
end
@ -46,7 +51,7 @@ module Gcovinator
end.parse!(argv)
Gcovinator.run(build_dir, source_dirs, argv, output_dir)
Gcovinator.run(build_dir, source_dirs, argv, output_dir, prefix)
end
end

View File

@ -8,8 +8,8 @@ module Gcovinator
attr_reader :source_file_name
attr_reader :report_file_name
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)
def initialize(source_file_name, file_coverage, prefix, output_dir, report_file_name)
@source_file_name = clean_source_file_name(source_file_name, prefix)
@report_file_name = report_file_name
@total_lines = 0
@covered_lines = 0
@ -20,13 +20,11 @@ module Gcovinator
private
def clean_source_file_name(source_file_name, source_dirs)
source_dirs.each do |source_dir|
prefix_test = "#{source_dir}/"
def clean_source_file_name(source_file_name, prefix)
prefix_test = "#{prefix}/"
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