Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:276e7a263c35 1 # ==========================================
MACRUM 0:276e7a263c35 2 # Unity Project - A Test Framework for C
MACRUM 0:276e7a263c35 3 # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
MACRUM 0:276e7a263c35 4 # [Released under MIT License. Please refer to license.txt for details]
MACRUM 0:276e7a263c35 5 # ==========================================
MACRUM 0:276e7a263c35 6
MACRUM 0:276e7a263c35 7 require 'yaml'
MACRUM 0:276e7a263c35 8 require 'fileutils'
MACRUM 0:276e7a263c35 9 require UNITY_ROOT + '../auto/unity_test_summary'
MACRUM 0:276e7a263c35 10 require UNITY_ROOT + '../auto/generate_test_runner'
MACRUM 0:276e7a263c35 11 require UNITY_ROOT + '../auto/colour_reporter'
MACRUM 0:276e7a263c35 12
MACRUM 0:276e7a263c35 13 module RakefileHelpers
MACRUM 0:276e7a263c35 14
MACRUM 0:276e7a263c35 15 C_EXTENSION = '.c'
MACRUM 0:276e7a263c35 16
MACRUM 0:276e7a263c35 17 def load_configuration(config_file)
MACRUM 0:276e7a263c35 18 unless ($configured)
MACRUM 0:276e7a263c35 19 $cfg_file = "targets/#{config_file}" unless (config_file =~ /[\\|\/]/)
MACRUM 0:276e7a263c35 20 $cfg = YAML.load(File.read($cfg_file))
MACRUM 0:276e7a263c35 21 $colour_output = false unless $cfg['colour']
MACRUM 0:276e7a263c35 22 $configured = true if (config_file != DEFAULT_CONFIG_FILE)
MACRUM 0:276e7a263c35 23 end
MACRUM 0:276e7a263c35 24 end
MACRUM 0:276e7a263c35 25
MACRUM 0:276e7a263c35 26 def configure_clean
MACRUM 0:276e7a263c35 27 CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil?
MACRUM 0:276e7a263c35 28 end
MACRUM 0:276e7a263c35 29
MACRUM 0:276e7a263c35 30 def configure_toolchain(config_file=DEFAULT_CONFIG_FILE)
MACRUM 0:276e7a263c35 31 config_file += '.yml' unless config_file =~ /\.yml$/
MACRUM 0:276e7a263c35 32 config_file = config_file unless config_file =~ /[\\|\/]/
MACRUM 0:276e7a263c35 33 load_configuration(config_file)
MACRUM 0:276e7a263c35 34 configure_clean
MACRUM 0:276e7a263c35 35 end
MACRUM 0:276e7a263c35 36
MACRUM 0:276e7a263c35 37 def get_unit_test_files
MACRUM 0:276e7a263c35 38 path = $cfg['compiler']['unit_tests_path'] + 'test*' + C_EXTENSION
MACRUM 0:276e7a263c35 39 path.gsub!(/\\/, '/')
MACRUM 0:276e7a263c35 40 FileList.new(path)
MACRUM 0:276e7a263c35 41 end
MACRUM 0:276e7a263c35 42
MACRUM 0:276e7a263c35 43 def get_local_include_dirs
MACRUM 0:276e7a263c35 44 include_dirs = $cfg['compiler']['includes']['items'].dup
MACRUM 0:276e7a263c35 45 include_dirs.delete_if {|dir| dir.is_a?(Array)}
MACRUM 0:276e7a263c35 46 return include_dirs
MACRUM 0:276e7a263c35 47 end
MACRUM 0:276e7a263c35 48
MACRUM 0:276e7a263c35 49 def extract_headers(filename)
MACRUM 0:276e7a263c35 50 includes = []
MACRUM 0:276e7a263c35 51 lines = File.readlines(filename)
MACRUM 0:276e7a263c35 52 lines.each do |line|
MACRUM 0:276e7a263c35 53 m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/)
MACRUM 0:276e7a263c35 54 if not m.nil?
MACRUM 0:276e7a263c35 55 includes << m[1]
MACRUM 0:276e7a263c35 56 end
MACRUM 0:276e7a263c35 57 end
MACRUM 0:276e7a263c35 58 return includes
MACRUM 0:276e7a263c35 59 end
MACRUM 0:276e7a263c35 60
MACRUM 0:276e7a263c35 61 def find_source_file(header, paths)
MACRUM 0:276e7a263c35 62 paths.each do |dir|
MACRUM 0:276e7a263c35 63 src_file = dir + header.ext(C_EXTENSION)
MACRUM 0:276e7a263c35 64 if (File.exists?(src_file))
MACRUM 0:276e7a263c35 65 return src_file
MACRUM 0:276e7a263c35 66 end
MACRUM 0:276e7a263c35 67 end
MACRUM 0:276e7a263c35 68 return nil
MACRUM 0:276e7a263c35 69 end
MACRUM 0:276e7a263c35 70
MACRUM 0:276e7a263c35 71 def tackit(strings)
MACRUM 0:276e7a263c35 72 if strings.is_a?(Array)
MACRUM 0:276e7a263c35 73 result = "\"#{strings.join}\""
MACRUM 0:276e7a263c35 74 else
MACRUM 0:276e7a263c35 75 result = strings
MACRUM 0:276e7a263c35 76 end
MACRUM 0:276e7a263c35 77 return result
MACRUM 0:276e7a263c35 78 end
MACRUM 0:276e7a263c35 79
MACRUM 0:276e7a263c35 80 def squash(prefix, items)
MACRUM 0:276e7a263c35 81 result = ''
MACRUM 0:276e7a263c35 82 items.each { |item| result += " #{prefix}#{tackit(item)}" }
MACRUM 0:276e7a263c35 83 return result
MACRUM 0:276e7a263c35 84 end
MACRUM 0:276e7a263c35 85
MACRUM 0:276e7a263c35 86 def should(behave, &block)
MACRUM 0:276e7a263c35 87 if block
MACRUM 0:276e7a263c35 88 puts "Should " + behave
MACRUM 0:276e7a263c35 89 yield block
MACRUM 0:276e7a263c35 90 else
MACRUM 0:276e7a263c35 91 puts "UNIMPLEMENTED CASE: Should #{behave}"
MACRUM 0:276e7a263c35 92 end
MACRUM 0:276e7a263c35 93 end
MACRUM 0:276e7a263c35 94
MACRUM 0:276e7a263c35 95 def build_compiler_fields
MACRUM 0:276e7a263c35 96 command = tackit($cfg['compiler']['path'])
MACRUM 0:276e7a263c35 97 if $cfg['compiler']['defines']['items'].nil?
MACRUM 0:276e7a263c35 98 defines = ''
MACRUM 0:276e7a263c35 99 else
MACRUM 0:276e7a263c35 100 defines = squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'] + ['UNITY_OUTPUT_CHAR=putcharSpy'])
MACRUM 0:276e7a263c35 101 end
MACRUM 0:276e7a263c35 102 options = squash('', $cfg['compiler']['options'])
MACRUM 0:276e7a263c35 103 includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items'])
MACRUM 0:276e7a263c35 104 includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
MACRUM 0:276e7a263c35 105 return {:command => command, :defines => defines, :options => options, :includes => includes}
MACRUM 0:276e7a263c35 106 end
MACRUM 0:276e7a263c35 107
MACRUM 0:276e7a263c35 108 def compile(file, defines=[])
MACRUM 0:276e7a263c35 109 compiler = build_compiler_fields
MACRUM 0:276e7a263c35 110 cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " +
MACRUM 0:276e7a263c35 111 "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}"
MACRUM 0:276e7a263c35 112 obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}"
MACRUM 0:276e7a263c35 113 execute(cmd_str + obj_file)
MACRUM 0:276e7a263c35 114 return obj_file
MACRUM 0:276e7a263c35 115 end
MACRUM 0:276e7a263c35 116
MACRUM 0:276e7a263c35 117 def build_linker_fields
MACRUM 0:276e7a263c35 118 command = tackit($cfg['linker']['path'])
MACRUM 0:276e7a263c35 119 if $cfg['linker']['options'].nil?
MACRUM 0:276e7a263c35 120 options = ''
MACRUM 0:276e7a263c35 121 else
MACRUM 0:276e7a263c35 122 options = squash('', $cfg['linker']['options'])
MACRUM 0:276e7a263c35 123 end
MACRUM 0:276e7a263c35 124 if ($cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?)
MACRUM 0:276e7a263c35 125 includes = ''
MACRUM 0:276e7a263c35 126 else
MACRUM 0:276e7a263c35 127 includes = squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items'])
MACRUM 0:276e7a263c35 128 end
MACRUM 0:276e7a263c35 129 includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
MACRUM 0:276e7a263c35 130 return {:command => command, :options => options, :includes => includes}
MACRUM 0:276e7a263c35 131 end
MACRUM 0:276e7a263c35 132
MACRUM 0:276e7a263c35 133 def link_it(exe_name, obj_list)
MACRUM 0:276e7a263c35 134 linker = build_linker_fields
MACRUM 0:276e7a263c35 135 cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " +
MACRUM 0:276e7a263c35 136 (obj_list.map{|obj|"#{$cfg['linker']['object_files']['path']}#{obj} "}).join +
MACRUM 0:276e7a263c35 137 $cfg['linker']['bin_files']['prefix'] + ' ' +
MACRUM 0:276e7a263c35 138 $cfg['linker']['bin_files']['destination'] +
MACRUM 0:276e7a263c35 139 exe_name + $cfg['linker']['bin_files']['extension']
MACRUM 0:276e7a263c35 140 execute(cmd_str)
MACRUM 0:276e7a263c35 141 end
MACRUM 0:276e7a263c35 142
MACRUM 0:276e7a263c35 143 def build_simulator_fields
MACRUM 0:276e7a263c35 144 return nil if $cfg['simulator'].nil?
MACRUM 0:276e7a263c35 145 if $cfg['simulator']['path'].nil?
MACRUM 0:276e7a263c35 146 command = ''
MACRUM 0:276e7a263c35 147 else
MACRUM 0:276e7a263c35 148 command = (tackit($cfg['simulator']['path']) + ' ')
MACRUM 0:276e7a263c35 149 end
MACRUM 0:276e7a263c35 150 if $cfg['simulator']['pre_support'].nil?
MACRUM 0:276e7a263c35 151 pre_support = ''
MACRUM 0:276e7a263c35 152 else
MACRUM 0:276e7a263c35 153 pre_support = squash('', $cfg['simulator']['pre_support'])
MACRUM 0:276e7a263c35 154 end
MACRUM 0:276e7a263c35 155 if $cfg['simulator']['post_support'].nil?
MACRUM 0:276e7a263c35 156 post_support = ''
MACRUM 0:276e7a263c35 157 else
MACRUM 0:276e7a263c35 158 post_support = squash('', $cfg['simulator']['post_support'])
MACRUM 0:276e7a263c35 159 end
MACRUM 0:276e7a263c35 160 return {:command => command, :pre_support => pre_support, :post_support => post_support}
MACRUM 0:276e7a263c35 161 end
MACRUM 0:276e7a263c35 162
MACRUM 0:276e7a263c35 163 def execute(command_string, verbose=true)
MACRUM 0:276e7a263c35 164 report command_string
MACRUM 0:276e7a263c35 165 output = `#{command_string}`.chomp
MACRUM 0:276e7a263c35 166 report(output) if (verbose && !output.nil? && (output.length > 0))
MACRUM 0:276e7a263c35 167 if $?.exitstatus != 0
MACRUM 0:276e7a263c35 168 raise "Command failed. (Returned #{$?.exitstatus})"
MACRUM 0:276e7a263c35 169 end
MACRUM 0:276e7a263c35 170 return output
MACRUM 0:276e7a263c35 171 end
MACRUM 0:276e7a263c35 172
MACRUM 0:276e7a263c35 173 def report_summary
MACRUM 0:276e7a263c35 174 summary = UnityTestSummary.new
MACRUM 0:276e7a263c35 175 summary.set_root_path(UNITY_ROOT)
MACRUM 0:276e7a263c35 176 results_glob = "#{$cfg['compiler']['build_path']}*.test*"
MACRUM 0:276e7a263c35 177 results_glob.gsub!(/\\/, '/')
MACRUM 0:276e7a263c35 178 results = Dir[results_glob]
MACRUM 0:276e7a263c35 179 summary.set_targets(results)
MACRUM 0:276e7a263c35 180 report summary.run
MACRUM 0:276e7a263c35 181 end
MACRUM 0:276e7a263c35 182
MACRUM 0:276e7a263c35 183 def run_tests(test_files)
MACRUM 0:276e7a263c35 184 report 'Running Unity system tests...'
MACRUM 0:276e7a263c35 185
MACRUM 0:276e7a263c35 186 # Tack on TEST define for compiling unit tests
MACRUM 0:276e7a263c35 187 load_configuration($cfg_file)
MACRUM 0:276e7a263c35 188 test_defines = ['TEST']
MACRUM 0:276e7a263c35 189 $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil?
MACRUM 0:276e7a263c35 190 $cfg['compiler']['defines']['items'] << 'TEST'
MACRUM 0:276e7a263c35 191
MACRUM 0:276e7a263c35 192 include_dirs = get_local_include_dirs
MACRUM 0:276e7a263c35 193
MACRUM 0:276e7a263c35 194 # Build and execute each unit test
MACRUM 0:276e7a263c35 195 test_files.each do |test|
MACRUM 0:276e7a263c35 196 obj_list = []
MACRUM 0:276e7a263c35 197
MACRUM 0:276e7a263c35 198 if !$cfg['compiler']['aux_sources'].nil?
MACRUM 0:276e7a263c35 199 $cfg['compiler']['aux_sources'].each do |aux|
MACRUM 0:276e7a263c35 200 obj_list << compile(aux, test_defines)
MACRUM 0:276e7a263c35 201 end
MACRUM 0:276e7a263c35 202 end
MACRUM 0:276e7a263c35 203
MACRUM 0:276e7a263c35 204 # Detect dependencies and build required modules
MACRUM 0:276e7a263c35 205 extract_headers(test).each do |header|
MACRUM 0:276e7a263c35 206 # Compile corresponding source file if it exists
MACRUM 0:276e7a263c35 207 src_file = find_source_file(header, include_dirs)
MACRUM 0:276e7a263c35 208 if !src_file.nil?
MACRUM 0:276e7a263c35 209 obj_list << compile(src_file, test_defines)
MACRUM 0:276e7a263c35 210 end
MACRUM 0:276e7a263c35 211 end
MACRUM 0:276e7a263c35 212
MACRUM 0:276e7a263c35 213 # Build the test runner (generate if configured to do so)
MACRUM 0:276e7a263c35 214 test_base = File.basename(test, C_EXTENSION)
MACRUM 0:276e7a263c35 215
MACRUM 0:276e7a263c35 216 runner_name = test_base + '_Runner.c'
MACRUM 0:276e7a263c35 217 runner_path = ''
MACRUM 0:276e7a263c35 218
MACRUM 0:276e7a263c35 219 if $cfg['compiler']['runner_path'].nil?
MACRUM 0:276e7a263c35 220 runner_path = $cfg['compiler']['build_path'] + runner_name
MACRUM 0:276e7a263c35 221 else
MACRUM 0:276e7a263c35 222 runner_path = $cfg['compiler']['runner_path'] + runner_name
MACRUM 0:276e7a263c35 223 end
MACRUM 0:276e7a263c35 224
MACRUM 0:276e7a263c35 225 options = $cfg[:unity]
MACRUM 0:276e7a263c35 226 options[:use_param_tests] = (test =~ /parameterized/) ? true : false
MACRUM 0:276e7a263c35 227 UnityTestRunnerGenerator.new(options).run(test, runner_path)
MACRUM 0:276e7a263c35 228 obj_list << compile(runner_path, test_defines)
MACRUM 0:276e7a263c35 229
MACRUM 0:276e7a263c35 230 # Build the test module
MACRUM 0:276e7a263c35 231 obj_list << compile(test, test_defines)
MACRUM 0:276e7a263c35 232
MACRUM 0:276e7a263c35 233 # Link the test executable
MACRUM 0:276e7a263c35 234 link_it(test_base, obj_list)
MACRUM 0:276e7a263c35 235
MACRUM 0:276e7a263c35 236 # Execute unit test and generate results file
MACRUM 0:276e7a263c35 237 simulator = build_simulator_fields
MACRUM 0:276e7a263c35 238 executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension']
MACRUM 0:276e7a263c35 239 if simulator.nil?
MACRUM 0:276e7a263c35 240 cmd_str = executable
MACRUM 0:276e7a263c35 241 else
MACRUM 0:276e7a263c35 242 cmd_str = "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}"
MACRUM 0:276e7a263c35 243 end
MACRUM 0:276e7a263c35 244 output = execute(cmd_str)
MACRUM 0:276e7a263c35 245 test_results = $cfg['compiler']['build_path'] + test_base
MACRUM 0:276e7a263c35 246 if output.match(/OK$/m).nil?
MACRUM 0:276e7a263c35 247 test_results += '.testfail'
MACRUM 0:276e7a263c35 248 else
MACRUM 0:276e7a263c35 249 test_results += '.testpass'
MACRUM 0:276e7a263c35 250 end
MACRUM 0:276e7a263c35 251 File.open(test_results, 'w') { |f| f.print output }
MACRUM 0:276e7a263c35 252
MACRUM 0:276e7a263c35 253 end
MACRUM 0:276e7a263c35 254 end
MACRUM 0:276e7a263c35 255 end