2017-02-07 23:30:57 +03:00
|
|
|
# ==========================================
|
|
|
|
# Unity Project - A Test Framework for C
|
|
|
|
# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
|
|
|
|
# [Released under MIT License. Please refer to license.txt for details]
|
|
|
|
# ==========================================
|
|
|
|
|
|
|
|
require 'yaml'
|
|
|
|
require 'fileutils'
|
2017-04-27 03:54:33 +03:00
|
|
|
require HERE + '../../auto/unity_test_summary'
|
|
|
|
require HERE + '../../auto/generate_test_runner'
|
|
|
|
require HERE + '../../auto/colour_reporter'
|
2017-02-07 23:30:57 +03:00
|
|
|
|
|
|
|
module RakefileHelpers
|
2017-04-27 03:54:33 +03:00
|
|
|
C_EXTENSION = '.c'.freeze
|
2017-02-07 23:30:57 +03:00
|
|
|
|
|
|
|
def load_configuration(config_file)
|
2017-04-27 03:54:33 +03:00
|
|
|
return if $configured
|
|
|
|
|
|
|
|
$cfg_file = HERE + "../../test/targets/#{config_file}" unless config_file =~ /[\\|\/]/
|
|
|
|
$cfg = YAML.load(File.read($cfg_file))
|
|
|
|
$colour_output = false unless $cfg['colour']
|
|
|
|
$configured = true if config_file != DEFAULT_CONFIG_FILE
|
2017-02-07 23:30:57 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def configure_clean
|
|
|
|
CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil?
|
|
|
|
end
|
|
|
|
|
2017-04-27 03:54:33 +03:00
|
|
|
def configure_toolchain(config_file = DEFAULT_CONFIG_FILE)
|
2017-02-07 23:30:57 +03:00
|
|
|
config_file += '.yml' unless config_file =~ /\.yml$/
|
|
|
|
config_file = config_file unless config_file =~ /[\\|\/]/
|
|
|
|
load_configuration(config_file)
|
|
|
|
configure_clean
|
|
|
|
end
|
|
|
|
|
|
|
|
def tackit(strings)
|
2017-04-27 03:54:33 +03:00
|
|
|
result = if strings.is_a?(Array)
|
|
|
|
"\"#{strings.join}\""
|
|
|
|
else
|
|
|
|
strings
|
|
|
|
end
|
|
|
|
result
|
2017-02-07 23:30:57 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def squash(prefix, items)
|
|
|
|
result = ''
|
|
|
|
items.each { |item| result += " #{prefix}#{tackit(item)}" }
|
2017-04-27 03:54:33 +03:00
|
|
|
result
|
2017-02-07 23:30:57 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_compiler_fields
|
2017-04-27 03:54:33 +03:00
|
|
|
command = tackit($cfg['compiler']['path'])
|
|
|
|
defines = if $cfg['compiler']['defines']['items'].nil?
|
|
|
|
''
|
|
|
|
else
|
2017-09-12 19:30:07 +03:00
|
|
|
squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'] + ['UNITY_OUTPUT_CHAR=UnityOutputCharSpy_OutputChar'] + ['UNITY_OUTPUT_CHAR_HEADER_DECLARATION=UnityOutputCharSpy_OutputChar\(int\)'])
|
2017-04-27 03:54:33 +03:00
|
|
|
end
|
2017-02-07 23:30:57 +03:00
|
|
|
options = squash('', $cfg['compiler']['options'])
|
|
|
|
includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items'])
|
|
|
|
includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
|
2017-04-27 03:54:33 +03:00
|
|
|
|
|
|
|
{ command: command, defines: defines, options: options, includes: includes }
|
2017-02-07 23:30:57 +03:00
|
|
|
end
|
|
|
|
|
2017-04-27 03:54:33 +03:00
|
|
|
def compile(file, _defines = [])
|
2017-02-07 23:30:57 +03:00
|
|
|
compiler = build_compiler_fields
|
2017-04-27 03:54:33 +03:00
|
|
|
unity_include = $cfg['compiler']['includes']['prefix'] + '../../src'
|
|
|
|
cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{unity_include} #{file} " \
|
|
|
|
"#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}" \
|
|
|
|
"#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}"
|
|
|
|
|
2017-02-07 23:30:57 +03:00
|
|
|
execute(cmd_str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_linker_fields
|
2017-04-27 03:54:33 +03:00
|
|
|
command = tackit($cfg['linker']['path'])
|
|
|
|
options = if $cfg['linker']['options'].nil?
|
|
|
|
''
|
|
|
|
else
|
|
|
|
squash('', $cfg['linker']['options'])
|
|
|
|
end
|
|
|
|
includes = if $cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?
|
|
|
|
''
|
|
|
|
else
|
|
|
|
squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items'])
|
|
|
|
end.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
|
|
|
|
|
|
|
|
{ command: command, options: options, includes: includes }
|
2017-02-07 23:30:57 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_it(exe_name, obj_list)
|
|
|
|
linker = build_linker_fields
|
|
|
|
cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " +
|
2017-04-27 03:54:33 +03:00
|
|
|
(obj_list.map { |obj| "#{$cfg['linker']['object_files']['path']}#{obj} " }).join +
|
|
|
|
$cfg['linker']['bin_files']['prefix'] + ' ' +
|
|
|
|
$cfg['linker']['bin_files']['destination'] +
|
|
|
|
exe_name + $cfg['linker']['bin_files']['extension']
|
2017-02-07 23:30:57 +03:00
|
|
|
execute(cmd_str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_simulator_fields
|
|
|
|
return nil if $cfg['simulator'].nil?
|
2017-04-27 03:54:33 +03:00
|
|
|
command = if $cfg['simulator']['path'].nil?
|
|
|
|
''
|
|
|
|
else
|
|
|
|
(tackit($cfg['simulator']['path']) + ' ')
|
|
|
|
end
|
|
|
|
pre_support = if $cfg['simulator']['pre_support'].nil?
|
|
|
|
''
|
|
|
|
else
|
|
|
|
squash('', $cfg['simulator']['pre_support'])
|
|
|
|
end
|
|
|
|
post_support = if $cfg['simulator']['post_support'].nil?
|
|
|
|
''
|
|
|
|
else
|
|
|
|
squash('', $cfg['simulator']['post_support'])
|
|
|
|
end
|
|
|
|
{ command: command, pre_support: pre_support, post_support: post_support }
|
2017-02-07 23:30:57 +03:00
|
|
|
end
|
|
|
|
|
2017-04-27 03:54:33 +03:00
|
|
|
def execute(command_string, verbose = true)
|
2017-02-07 23:30:57 +03:00
|
|
|
report command_string
|
|
|
|
output = `#{command_string}`.chomp
|
2017-04-27 03:54:33 +03:00
|
|
|
report(output) if verbose && !output.nil? && !output.empty?
|
|
|
|
raise "Command failed. (Returned #{$?.exitstatus})" if $?.exitstatus != 0
|
|
|
|
output
|
2017-02-07 23:30:57 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def report_summary
|
|
|
|
summary = UnityTestSummary.new
|
2017-04-27 03:54:33 +03:00
|
|
|
summary.root = HERE
|
2017-02-07 23:30:57 +03:00
|
|
|
results_glob = "#{$cfg['compiler']['build_path']}*.test*"
|
2017-04-27 03:54:33 +03:00
|
|
|
results_glob.tr!('\\', '/')
|
2017-02-07 23:30:57 +03:00
|
|
|
results = Dir[results_glob]
|
2017-04-27 03:54:33 +03:00
|
|
|
summary.targets = results
|
2017-02-07 23:30:57 +03:00
|
|
|
summary.run
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_tests
|
|
|
|
report 'Running Unity system tests...'
|
|
|
|
|
|
|
|
# Tack on TEST define for compiling unit tests
|
|
|
|
load_configuration($cfg_file)
|
|
|
|
test_defines = ['TEST']
|
|
|
|
$cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil?
|
|
|
|
|
|
|
|
# Get a list of all source files needed
|
2017-04-27 03:54:33 +03:00
|
|
|
src_files = Dir[HERE + 'src/*.c']
|
|
|
|
src_files += Dir[HERE + 'test/*.c']
|
|
|
|
src_files += Dir[HERE + 'test/main/*.c']
|
2017-02-07 23:30:57 +03:00
|
|
|
src_files << '../../src/unity.c'
|
|
|
|
|
|
|
|
# Build object files
|
|
|
|
src_files.each { |f| compile(f, test_defines) }
|
2017-04-27 03:54:33 +03:00
|
|
|
obj_list = src_files.map { |f| File.basename(f.ext($cfg['compiler']['object_files']['extension'])) }
|
2017-02-07 23:30:57 +03:00
|
|
|
|
|
|
|
# Link the test executable
|
2017-04-27 03:54:33 +03:00
|
|
|
test_base = 'framework_test'
|
2017-02-07 23:30:57 +03:00
|
|
|
link_it(test_base, obj_list)
|
|
|
|
|
|
|
|
# Execute unit test and generate results file
|
|
|
|
simulator = build_simulator_fields
|
|
|
|
executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension']
|
2017-04-27 03:54:33 +03:00
|
|
|
cmd_str = if simulator.nil?
|
|
|
|
executable + ' -v -r'
|
|
|
|
else
|
|
|
|
"#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}"
|
|
|
|
end
|
2017-02-07 23:30:57 +03:00
|
|
|
output = execute(cmd_str)
|
|
|
|
test_results = $cfg['compiler']['build_path'] + test_base
|
2017-04-27 03:54:33 +03:00
|
|
|
test_results += if output.match(/OK$/m).nil?
|
|
|
|
'.testfail'
|
|
|
|
else
|
|
|
|
'.testpass'
|
|
|
|
end
|
2017-02-07 23:30:57 +03:00
|
|
|
File.open(test_results, 'w') { |f| f.print output }
|
|
|
|
end
|
|
|
|
end
|