Class: MetricFu::Dcov

Inherits:
Generator show all
Defined in:
lib/generators/dcov.rb

Instance Attribute Summary

Attributes inherited from Generator

#report, #template

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generator

class_name, #create_data_dir_if_missing, #create_metric_dir_if_missing, #create_output_dir_if_missing, generate_report, #generate_report, #initialize, #metric_directory, metric_directory, #round_to_tenths, #to_graph

Constructor Details

This class inherits a constructor from MetricFu::Generator

Class Method Details

.verify_dependencies!Object



7
8
9
10
# File 'lib/generators/dcov.rb', line 7

def self.verify_dependencies!
  `dcov --help`
  raise 'sudo gem install dcov # if you want the dcov tasks' unless $?.success?
end

Instance Method Details

#analyzeObject

Parses the output file into a hash for use by template, abstract method override



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
86
# File 'lib/generators/dcov.rb', line 33

def analyze
  #get the logged output
  output1 = File.open(MetricFu::Dcov.metric_directory + '/dcov.txt').read
  
  #turn it into something easier to work with
  output1 = output1.split("\n")
  
  #parse output
  output_hash = {}        #somewhere to keep our parsed data
  sub_section_sym = ""    #aid for parsing sub sections
  item_name = ""          #aid for parsing sub sections
  output1.map! do |line|  #parse each line for essential data (totals, coverage percentage)
    next if line.blank?                #skip blank lines
    next if !line[/Not covered:/].nil? #skip these lines
    next if line[/Generating report/]  #skip these lines
    next if line[/Writing report/]     #skip these lines
    
    #typically the first line, total files checked
    if line[/\AFiles:/]  
      output_hash[:file_count] = line[/\d+/]
      next
    end
    
    #These are the totals for each section
    if line[/\ATotal \w+/]
      output_hash[underscore(line[/\ATotal \w+/]).to_sym] = line[/\d+/]
      next
    end
    
    #Actual coverages and list of uncovered items, marks start of item list
    if line[/\A\w+ coverage/]
      sub_section_sym  = underscore(line[/\A\w+ coverage/]).to_sym
      #output_hash[sub_section_sym] = line[/\d+%/]
      output_hash= output_hash.merge(sub_section_sym=>{})
      output_hash[sub_section_sym] = output_hash[sub_section_sym].merge({:coverage => line[/\d+%/]})
      next
    end
    
    #if it hasn't been caught by now, it's an item... 
    if item_name == ""
      #get Item name
      item_name = line.strip
    else
      #get item data and store pair
      if output_hash[sub_section_sym][:not_covored].nil?
        output_hash[sub_section_sym] = output_hash[sub_section_sym].merge(:not_covored=>{})
      end
      output_hash[sub_section_sym][:not_covored] = output_hash[sub_section_sym][:not_covored].merge({item_name=>line.strip})
      item_name = ""
    end
  end
  
  @dcov = output_hash
end

#emitObject

search through resource path and rub dcov on all .rb files, abstract method override



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/generators/dcov.rb', line 13

def emit
    #Clean up file system before we start
    FileUtils.rm_rf(MetricFu::Dcov.metric_directory, :verbose => false)
    Dir.mkdir(MetricFu::Dcov.metric_directory)
    
    #what files are we going to test
    test_files =  MetricFu.dcov[:test_files].join(' ')
    
    
    #set Dcov Options (See dcov rdocs)
    dcov_opts = MetricFu.dcov[:dcov_opts].nil? ? "" : MetricFu.dcov[:dcov_opts].join(' ')
    
    #setup place to store output
    output = "> #{MetricFu::Dcov.metric_directory}/dcov.txt 2>/dev/null"
    
    #actually do the test
    `dcov #{dcov_opts} #{test_files} #{output}`
end

#to_hObject

abstract method override



89
90
91
# File 'lib/generators/dcov.rb', line 89

def to_h
  {:dcov=> @dcov}   
end

#underscore(camel_cased_word) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/generators/dcov.rb', line 93

def underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  tr(" ", "_").
  downcase
end