summaryrefslogtreecommitdiffstats
path: root/tinyusb/test/vendor/ceedling/plugins/bullseye/lib/bullseye.rb
blob: ffa444ac79bd959e561ca17d03eee204c9b0e0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
120
121
122
123
124
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require 'ceedling/plugin'
require 'ceedling/constants'

BULLSEYE_ROOT_NAME         = 'bullseye'
BULLSEYE_TASK_ROOT         = BULLSEYE_ROOT_NAME + ':'
BULLSEYE_SYM               = BULLSEYE_ROOT_NAME.to_sym

BULLSEYE_BUILD_PATH        = "#{PROJECT_BUILD_ROOT}/#{BULLSEYE_ROOT_NAME}"
BULLSEYE_BUILD_OUTPUT_PATH = "#{BULLSEYE_BUILD_PATH}/out"
BULLSEYE_RESULTS_PATH      = "#{BULLSEYE_BUILD_PATH}/results"
BULLSEYE_DEPENDENCIES_PATH = "#{BULLSEYE_BUILD_PATH}/dependencies"
BULLSEYE_ARTIFACTS_PATH    = "#{PROJECT_BUILD_ARTIFACTS_ROOT}/#{BULLSEYE_ROOT_NAME}"

BULLSEYE_IGNORE_SOURCES    = ['unity', 'cmock', 'cexception']


class Bullseye < Plugin

  def setup
    @result_list = []
    @environment = [ {:covfile => File.join( BULLSEYE_ARTIFACTS_PATH, 'test.cov' )} ]
    @plugin_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
    @coverage_template_all = @ceedling[:file_wrapper].read(File.join(@plugin_root, 'assets/template.erb'))
  end

  def config
    {
      :project_test_build_output_path     => BULLSEYE_BUILD_OUTPUT_PATH,
      :project_test_results_path          => BULLSEYE_RESULTS_PATH,
      :project_test_dependencies_path     => BULLSEYE_DEPENDENCIES_PATH,
      :defines_test                       => DEFINES_TEST + ['CODE_COVERAGE'],
      :collection_defines_test_and_vendor => COLLECTION_DEFINES_TEST_AND_VENDOR + ['CODE_COVERAGE']
    }
  end

  def generate_coverage_object_file(source, object)
    arg_hash = {:tool => TOOLS_BULLSEYE_INSTRUMENTATION, :context => BULLSEYE_SYM, :source => source, :object => object}
    @ceedling[:plugin_manager].pre_compile_execute(arg_hash)

    @ceedling[:streaminator].stdout_puts("Compiling #{File.basename(source)} with coverage...")
    compile_command  = 
      @ceedling[:tool_executor].build_command_line(
        TOOLS_BULLSEYE_COMPILER,
        @ceedling[:flaginator].flag_down( OPERATION_COMPILE_SYM, BULLSEYE_SYM, source ),
        source,
        object,
        @ceedling[:file_path_utils].form_test_build_list_filepath( object ) )
    coverage_command = @ceedling[:tool_executor].build_command_line(TOOLS_BULLSEYE_INSTRUMENTATION, [], compile_command[:line] )

    shell_result     = @ceedling[:tool_executor].exec( coverage_command[:line], coverage_command[:options] )
    
    arg_hash[:shell_result] = shell_result
    @ceedling[:plugin_manager].post_compile_execute(arg_hash)
  end

  def post_test_fixture_execute(arg_hash)
    result_file = arg_hash[:result_file]
  
    if ((result_file =~ /#{BULLSEYE_RESULTS_PATH}/) and (not @result_list.include?(result_file)))
      @result_list << arg_hash[:result_file]
    end
  end

  def post_build
    return if (not @ceedling[:task_invoker].invoked?(/^#{BULLSEYE_TASK_ROOT}/))

    # test results
    results = @ceedling[:plugin_reportinator].assemble_test_results(@result_list)
    hash = {
      :header => BULLSEYE_ROOT_NAME.upcase,
      :results => results
    }
    
    @ceedling[:plugin_reportinator].run_test_results_report(hash) do
      message = ''
      message = 'Unit test failures.' if (results[:counts][:failed] > 0)
      message
    end
    
    # coverage results
    return if (verify_coverage_file() == false)
    if (@ceedling[:task_invoker].invoked?(/^#{BULLSEYE_TASK_ROOT}(all|delta)/))
      command      = @ceedling[:tool_executor].build_command_line(TOOLS_BULLSEYE_REPORT_COVSRC, [])
      shell_result = @ceedling[:tool_executor].exec(command[:line], command[:options])
      report_coverage_results_all(shell_result[:output])
    else
      report_per_function_coverage_results(@ceedling[:test_invoker].sources)
    end
  end

  def summary
    return if (verify_coverage_file() == false)
    result_list = @ceedling[:file_path_utils].form_pass_results_filelist( BULLSEYE_RESULTS_PATH, COLLECTION_ALL_TESTS )

    # test results
    # get test results for only those tests in our configuration and of those only tests with results on disk
    hash = {
      :header => BULLSEYE_ROOT_NAME.upcase,
      :results => @ceedling[:plugin_reportinator].assemble_test_results(result_list, {:boom => false})
    }

    @ceedling[:plugin_reportinator].run_test_results_report(hash)
    
    # coverage results
    command = @ceedling[:tool_executor].build_command_line(TOOLS_BULLSEYE_REPORT_COVSRC)
    shell_result = @ceedling[:tool_executor].exec(command[:line], command[:options])
    report_coverage_results_all(shell_result[:output])
  end
  
  def enableBullseye(enable)
    if BULLSEYE_AUTO_LICENSE
      if (enable)
        args = ['push', 'on']
        @ceedling[:streaminator].stdout_puts("Enabling Bullseye")
      else
        args = ['pop']
        @ceedling[:streaminator].stdout_puts("Reverting Bullseye to previous state")
      end

      args.each do |arg| 
        command = @ceedling[:tool_executor].build_command_line(TOOLS_BULLSEYE_BUILD_ENABLE_DISABLE, [], arg)
        shell_result = @ceedling[:tool_executor].exec(command[:line], command[:options])
      end

    end
  end
  
  private ###################################

  def report_coverage_results_all(coverage)
    results = {
      :header => BULLSEYE_ROOT_NAME.upcase,
      :coverage => {
        :functions => nil,
        :branches  => nil
      }
    }

    if (coverage =~ /^Total.*?=\s+([0-9]+)\%/)
      results[:coverage][:functions] = $1.to_i
    end
    
    if (coverage =~ /^Total.*=\s+([0-9]+)\%\s*$/)
      results[:coverage][:branches] = $1.to_i
    end

    @ceedling[:plugin_reportinator].run_report($stdout, @coverage_template_all, results)
  end

  def report_per_function_coverage_results(sources)
    banner = @ceedling[:plugin_reportinator].generate_banner( "#{BULLSEYE_ROOT_NAME.upcase}: CODE COVERAGE SUMMARY" )
    @ceedling[:streaminator].stdout_puts "\n" + banner

    coverage_sources = sources.clone
    coverage_sources.delete_if {|item| item =~ /#{CMOCK_MOCK_PREFIX}.+#{EXTENSION_SOURCE}$/}
    coverage_sources.delete_if {|item| item =~ /#{BULLSEYE_IGNORE_SOURCES.join('|')}#{EXTENSION_SOURCE}$/}

    coverage_sources.each do |source|
      command          = @ceedling[:tool_executor].build_command_line(TOOLS_BULLSEYE_REPORT_COVFN, [], source)
      shell_results    = @ceedling[:tool_executor].exec(command[:line], command[:options])
      coverage_results = shell_results[:output].deep_clone
      coverage_results.sub!(/.*\n.*\n/,'') # Remove the Bullseye tool banner
      if (coverage_results =~ /warning cov814: report is empty/)
        coverage_results = "WARNING: #{source} contains no coverage data!\n\n"
        @ceedling[:streaminator].stdout_puts(coverage_results, Verbosity::COMPLAIN)
      else
        coverage_results += "\n"
        @ceedling[:streaminator].stdout_puts(coverage_results)
      end
    end
  end

  def verify_coverage_file
    exist = @ceedling[:file_wrapper].exist?( ENVIRONMENT_COVFILE )

    if (!exist)
      banner = @ceedling[:plugin_reportinator].generate_banner( "#{BULLSEYE_ROOT_NAME.upcase}: CODE COVERAGE SUMMARY" )
      @ceedling[:streaminator].stdout_puts "\n" + banner + "\nNo coverage file.\n\n"
    end
    
    return exist
  end
  
end


# end blocks always executed following rake run
END {
  # cache our input configurations to use in comparison upon next execution
  if (@ceedling[:task_invoker].invoked?(/^#{BULLSEYE_TASK_ROOT}/))
    @ceedling[:cacheinator].cache_test_config( @ceedling[:setupinator].config_hash ) 
    @ceedling[BULLSEYE_SYM].enableBullseye(false)
  end
}