Class: RcovStats

Inherits:
Object
  • Object
show all
Defined in:
lib/rcov_stats.rb

Constant Summary collapse

TYPES =
["units", "functionals"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_, sections_ = nil) ⇒ RcovStats

Returns a new instance of RcovStats.



33
34
35
36
# File 'lib/rcov_stats.rb', line 33

def initialize(name_, sections_ = nil)
  self.name = name_
  self.sections = sections_.blank? ? [name_] : sections_
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



29
30
31
# File 'lib/rcov_stats.rb', line 29

def name
  @name
end

#sectionsObject

Returns the value of attribute sections.



29
30
31
# File 'lib/rcov_stats.rb', line 29

def sections
  @sections
end

Class Method Details

.before_rcovObject



42
43
44
# File 'lib/rcov_stats.rb', line 42

def self.before_rcov
  (pre_rcov = get_config("before_rcov")).blank? ? nil : pre_rcov
end

.cattr_accessor_with_default(name, value = nil) ⇒ Object



10
11
12
13
# File 'lib/rcov_stats.rb', line 10

def self.cattr_accessor_with_default(name, value = nil)
  cattr_accessor name
  self.send("#{name}=", value) if value
end

.get_config(option) ⇒ Object



38
39
40
# File 'lib/rcov_stats.rb', line 38

def self.get_config(option)
  YAML::load(File.open(File.join(root, 'config', 'rcov_stats.yml')))[option]
end

.rootObject



18
19
20
# File 'lib/rcov_stats.rb', line 18

def self.root
  "."
end

.setupObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rcov_stats.rb', line 125

def self.setup
  if is_merb
    Merb::Plugins.add_rakefiles(File.join(rcov_stats_dir, "rcov_stats_tasks"))
  end

  if defined?(RSpec)
    require 'rcov_stats_related/integrations/rspec2'
    include RcovStatsRelated::Integrations::Rspec2
    which_conf_use =  'rcov_rspec'
  elsif defined?(Spec)
    require 'rcov_stats_related/integrations/rspec'
    include RcovStatsRelated::Integrations::Rspec
    which_conf_use =  'rcov_rspec'
  else
    require 'rcov_stats_related/integrations/test_unit'
    include RcovStatsRelated::Integrations::TestUnit
    which_conf_use = 'rcov_standard'
  end


  unless File.exists?(rcov_stats_config_file)
    which_conf_use += '.yml'
    FileUtils.cp(File.join(rcov_stats_dir, '..', 'config', which_conf_use), rcov_stats_config_file)
  end
end

Instance Method Details

#bundler?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/rcov_stats.rb', line 121

def bundler?
  File.exist?("./Gemfile")
end

#files_to_coverObject



52
53
54
# File 'lib/rcov_stats.rb', line 52

def files_to_cover
  get_array_data "files_to_cover"
end

#files_to_testObject



56
57
58
# File 'lib/rcov_stats.rb', line 56

def files_to_test
  get_array_data "files_to_test"
end

#generate_indexObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rcov_stats.rb', line 86

def generate_index
  Dir[File.join(self.class.rcov_stats_dir, '..', 'templates/*')].each do |i|
    FileUtils.mkdir_p File.join(self.class.root, 'coverage')
    FileUtils.cp(i, File.join(self.class.root, 'coverage', i.split("/").last))
  end

  template_object = {}

  @sections.each do |i|
    FileUtils.mkdir_p File.join(self.class.root, 'coverage', i)
    coverage_index = File.join(self.class.root, 'coverage', i, "index.html")
    next unless File.exists?(coverage_index)
    doc = open(coverage_index) { |f| Hpricot(f) }
    footer_tts = doc.search("//tfoot/tr/td//tt")
    footer_div_covered = doc.search("//tfoot/tr/td//div[@class='covered']")
    footer_div_uncovered = doc.search("//tfoot/tr/td//div[@class='uncovered']")

    template_object["#{i}_total_lines"] = footer_tts[0].inner_text
    template_object["#{i}_code_lines"] =  footer_tts[1].inner_text
    template_object["#{i}_total_result"] = footer_tts[2].inner_text
    template_object["#{i}_code_result"] =  footer_tts[3].inner_text
    template_object["#{i}_total_rpx"] =  footer_div_covered[0].get_attribute("style")
    template_object["#{i}_total_cpx"] =  footer_div_covered[1].get_attribute("style")
    template_object["#{i}_total_lrpx"] = footer_div_uncovered[0].get_attribute("style")
    template_object["#{i}_total_lcpx"] =  footer_div_uncovered[1].get_attribute("style")
  end
  
  template_object["generated_on"] = "Generated on #{Time.now}"
  template_source = ERB.new(IO.read(File.join(self.class.root, 'coverage', "index.html")))

  File.open(File.join(self.class.root, 'coverage', "index.html"), "w+") do |f|
    f.write( template_source.result(RcovStatsRelated::ErbBinding.new(template_object).get_binding))
  end
end

#get_array_data(type) ⇒ Object



46
47
48
49
50
# File 'lib/rcov_stats.rb', line 46

def get_array_data(type)
  @sections.map do |section|
    (self.class.get_config("#{section}_#{type}") || []).reject { |d| d.blank? }
  end.flatten.uniq
end

#parse_file_to_cover(list) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/rcov_stats.rb', line 71

def parse_file_to_cover(list)
  result = []
  list.each do |f|
    file_list = File.directory?(File.join(self.class.root, f)) ? File.join(f, "**", self.class.cover_file_indicator) : File.join(f)
    unless (list_of_read_files = Dir[file_list]).empty?
      result += list_of_read_files
    end
  end
  result.uniq
end

#parse_file_to_test(list) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/rcov_stats.rb', line 60

def parse_file_to_test(list)
  result = []
  list.each do |f|
    file_list = File.directory?(File.join(self.class.root, test_name, f)) ? File.join(test_name, f, "**", test_file_indicator) : File.join(test_name, f)
    unless (list_of_read_files = Dir[file_list]).empty?
      result += list_of_read_files
    end
  end
  result.uniq
end

#test_file_indicatorObject



82
83
84
# File 'lib/rcov_stats.rb', line 82

def test_file_indicator
  "*_#{test_name}.rb"
end