Class: GCOVTOOLS::Project

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "") ⇒ Project

Returns a new instance of Project.



12
13
14
15
16
# File 'lib/project.rb', line 12

def initialize name=""
  @name = name
  @files = {}
  @adding = false
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



9
10
11
# File 'lib/project.rb', line 9

def files
  @files
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#statsObject (readonly)

Returns the value of attribute stats.



9
10
11
# File 'lib/project.rb', line 9

def stats
  @stats
end

Class Method Details

.load_dir(path, hash = {}) ⇒ Object

#add_file



139
140
141
142
143
# File 'lib/project.rb', line 139

def self.load_dir path, hash={}
  project = GCOVTOOLS::Project.new
  project.add_dir path, hash
  project
end

Instance Method Details

#<<(file) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/project.rb', line 18

def <<(file)
  if @files.has_key?file.name
    @files[file.name].merge! file
  else
    @files[file.name] = file
  end
  _update_stats unless @adding
end

#_add_file(path, hash_ = {}) ⇒ Object



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
# File 'lib/project.rb', line 87

def _add_file path, hash_={}
  GCOVTOOLS::logger.debug "parsing file: #{path}"

  hash = hash_.dup
  
  files = GCOVTOOLS::File.load(path)

  # apply _inclusive_ filters first
  files.select!{ |file| 
    hash[:include].nil? or hash[:include].empty? or !hash[:include].select{|f| f.match(::Pathname.new(file.meta['Source']).cleanpath.to_s) }.empty? 
  }

  GCOVTOOLS::logger.debug "included #{files.size} files"

  old_size = files.size

  # apply _exclusive_ filters next
  files.select!{ |file| 
    hash[:exclude].nil? or hash[:exclude].empty? or hash[:exclude].select{|f| f.match(::Pathname.new(file.meta['Source']).cleanpath.to_s) }.empty? 
  }

  GCOVTOOLS::logger.debug "excluded #{old_size-files.size} files"

  # add all the files that survived the gauntlet
  files.map{ |file| self << file }
end

#_update_statsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/project.rb', line 52

def _update_stats
  @stats = { 
    :missed_lines => 0,
    :exec_lines => 0,
    :empty_lines => 0,
    :total_exec => 0,
    :total_lines => 0,
    :lines => 0,
    :coverage => 0.0,
    :hits_per_line => 0
  }

  @files.each do |name,file|
    @stats[:missed_lines] += file.stats[:missed_lines]
    @stats[:exec_lines] += file.stats[:exec_lines]
    @stats[:total_exec] += file.stats[:total_exec]
    @stats[:empty_lines] += file.stats[:empty_lines]
  end
    
  @stats[:lines] = @stats[:exec_lines] + @stats[:missed_lines]
  @stats[:total_lines] = @stats[:lines] + @stats[:empty_lines]

  if @stats[:lines] > 0
    @stats[:coverage] = @stats[:exec_lines].to_f / @stats[:lines].to_f
    @stats[:hits_per_line] = @stats[:total_exec].to_f / @stats[:lines]
  else
    @stats[:coverage] = 1
    @stats[:hits_per_line] = 0
  end  
  
  @stats[:coverage_s] = sprintf("%0.01f%",100.0*@stats[:coverage])
  @stats[:hits_per_line_s] = sprintf("%0.02f",@stats[:hits_per_line])

end

#add_dir(path, hash_ = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/project.rb', line 114

def add_dir path, hash_={}
  GCOVTOOLS::logger.debug "searching: #{path}"
  hash = hash_.dup
  if hash[:recursive] == true
    filenames = Dir["#{path}/**/*.gcov"]
  else
    filenames = Dir["#{path}/*.gcov"]
  end

  GCOVTOOLS::logger.debug "found: #{filenames}"

  add_files do 
    filenames.each do |filename|
      _add_file filename, hash
    end # each filename
  end # add_files
end

#add_file(path, hash_ = {}) ⇒ Object

#add_dir



132
133
134
135
136
137
# File 'lib/project.rb', line 132

def add_file path, hash_={}
  hash = hash_.dup
  add_files do
    _add_file path, hash
  end # add_files
end

#add_files(&block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/project.rb', line 38

def add_files &block
  # suspend stat updates until done adding files
  fail "add_files requires a block" unless block_given?

  # guard against nested calls
  was_adding = @adding
  @adding = true
  yield
  @adding = was_adding

  _update_stats unless @adding

end