Skip to content

Commit

Permalink
Refactor all yaml files for self-tests to fit Ceedling format for wid…
Browse files Browse the repository at this point in the history
…er reuse.

Fix mistake in unity selftest without output spy running.
Namespace self-tests for consistency across ThrowTheSwitch projects (like being able to test:all)
Reduce clutter of NAMED self-tests in task list.
  • Loading branch information
mvandervoord committed Dec 14, 2019
1 parent e3132cd commit 3f71d10
Show file tree
Hide file tree
Showing 20 changed files with 1,227 additions and 1,233 deletions.
46 changes: 23 additions & 23 deletions test/rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ include RakefileHelpers
DEFAULT_CONFIG_FILE = 'gcc_auto_stdint.yml'
configure_toolchain(DEFAULT_CONFIG_FILE)

desc "Test unity with its own unit tests"
task :unit => [:prepare_for_tests] do
run_tests unit_test_files
end
namespace :test do
desc "Build and test Unity"
task :all => [:clean, :prepare_for_tests, 'test:scripts', 'test:unit', :style, 'test:summary']

desc "Test unity's helper scripts"
task :scripts => [:prepare_for_tests] do
Dir['tests/test_*.rb'].each do |scriptfile|
require "./"+scriptfile
desc "Test unity with its own unit tests"
task :unit => [:prepare_for_tests] do
run_tests unit_test_files
end
end

desc "Run all rspecs"
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/**/*_spec.rb'
end

desc "Generate test summary"
task :summary do
report_summary
end
desc "Test unity's helper scripts"
task :scripts => [:prepare_for_tests] do
Dir['tests/test_*.rb'].each do |scriptfile|
require "./"+scriptfile
end
end

namespace :test do
desc "Build and test Unity"
task :all => [:clean, :prepare_for_tests, :scripts, :unit, :style, :summary]
desc "Run all rspecs"
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/**/*_spec.rb'
end

desc "Generate test summary"
task :summary do
report_summary
end
end

# Shorthand for many common tasks
Expand Down Expand Up @@ -86,7 +86,7 @@ namespace :style do
namespace :check do
Dir['../**/*.rb'].each do |f|
filename = File.basename(f, '.rb')
desc "Check Style of #{filename}"
#desc "Check Style of #{filename}"
task filename.to_sym => ['style:clean'] do
report execute("rubocop #{f} --color --config .rubocop.yml", true)
report "Style Checked for #{f}"
Expand All @@ -102,7 +102,7 @@ namespace :style do
namespace :c do
Dir['../{src,extras/**}/*.{c,h}'].each do |f|
filename = File.basename(f)[0..-3]
desc "Check Style of #{filename}"
#desc "Check Style of #{filename}"
task filename.to_sym do
run_astyle f
end
Expand Down
158 changes: 67 additions & 91 deletions test/rakefile_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def load_configuration(config_file)
end

def configure_clean
CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil?
CLEAN.include('build/*.*')
end

def configure_toolchain(config_file = DEFAULT_CONFIG_FILE)
Expand All @@ -33,13 +33,16 @@ def configure_toolchain(config_file = DEFAULT_CONFIG_FILE)
end

def unit_test_files
path = $cfg['compiler']['unit_tests_path'] + 'test*' + C_EXTENSION
path = 'tests/test*' + C_EXTENSION
path.tr!('\\', '/')
FileList.new(path)
end

def local_include_dirs
include_dirs = $cfg['compiler']['includes']['items'].dup
include_dirs = $cfg[:paths][:includes].dup || []
include_dirs += $cfg[:paths][:source].dup || []
include_dirs += $cfg[:paths][:test].dup || []
include_dirs += $cfg[:paths][:support].dup || []
include_dirs.delete_if { |dir| dir.is_a?(Array) }
include_dirs
end
Expand Down Expand Up @@ -86,75 +89,71 @@ def should(behave, &block)
end
end

def build_compiler_fields(inject_defines)
command = tackit($cfg['compiler']['path'])
defines = if $cfg['compiler']['defines']['items'].nil?
''
else
squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'] + ['UNITY_OUTPUT_CHAR=putcharSpy'] + ['UNITY_OUTPUT_CHAR_HEADER_DECLARATION="putcharSpy(int)"'] + inject_defines)
end
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)

{ :command => command, :defines => defines, :options => options, :includes => includes }
end
def build_command_string(hash, values, defines = nil)

# Replace named and numbered slots
args = []
hash[:arguments].each do |arg|
if arg.include? '$'
if arg.include? ': COLLECTION_PATHS_TEST_TOOLCHAIN_INCLUDE'
pattern = arg.gsub(': COLLECTION_PATHS_TEST_TOOLCHAIN_INCLUDE','')
[ File.join('..','src') ].each do |f|
args << pattern.gsub(/\$/,f)
end

elsif arg.include? ': COLLECTION_PATHS_TEST_SUPPORT_SOURCE_INCLUDE_VENDOR'
pattern = arg.gsub(': COLLECTION_PATHS_TEST_SUPPORT_SOURCE_INCLUDE_VENDOR','')
[ 'src', File.join('tests'), File.join('testdata'), $cfg[:paths][:support] ].flatten.uniq.compact.each do |f|
args << pattern.gsub(/\$/,f)
end

elsif arg.include? ': COLLECTION_DEFINES_TEST_AND_VENDOR'
pattern = arg.gsub(': COLLECTION_DEFINES_TEST_AND_VENDOR','')
[ 'TEST', $cfg[:defines][:test], defines ].flatten.uniq.compact.each do |f|
args << pattern.gsub(/\$/,f)
end

elsif arg =~ /\$\{(\d+)\}/
i = $1.to_i - 1
if (values[i].is_a?(Array))
values[i].each {|v| args << arg.gsub(/\$\{\d+\}/, v)}
else
args << arg.gsub(/\$\{(\d)+\}/, values[i] || '')
end

else
args << arg

def compile(file, defines = [])
compiler = build_compiler_fields(defines)
cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " \
"#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}"
obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}"
execute(cmd_str + obj_file)
end
else
args << arg
end
end

obj_file
# Build Command
return tackit(hash[:executable]) + squash('', args)
end

def build_linker_fields
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 }
def compile(file, defines = [])
out_file = File.basename(file, C_EXTENSION) + $cfg[:extension][:object]
cmd_str = build_command_string( $cfg[:tools][:test_compiler], [ file, out_file ], defines )
execute(cmd_str)
out_file
end

def link_it(exe_name, obj_list)
linker = build_linker_fields
cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " +
(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']
cmd_str = build_command_string( $cfg[:tools][:test_linker], [ obj_list, exe_name ] )
execute(cmd_str)
end

def build_simulator_fields
return nil if $cfg['simulator'].nil?
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 }
def runtest(bin_name, ok_to_fail = false, extra_args = nil)
extra_args = extra_args.nil? ? "" : " " + extra_args
if $cfg[:tools][:test_fixture]
cmd_str = build_command_string( $cfg[:tools][:test_fixture], [ bin_name, extra_args ] )
else
cmd_str = bin_name + extra_args
end
execute(cmd_str, ok_to_fail)
end

def run_astyle(style_what)
Expand All @@ -180,7 +179,7 @@ def execute(command_string, ok_to_fail = false)
def report_summary
summary = UnityTestSummary.new
summary.root = __dir__
results_glob = "#{$cfg['compiler']['build_path']}*.test*"
results_glob = "build/*.test*"
results_glob.tr!('\\', '/')
results = Dir[results_glob]
summary.targets = results
Expand All @@ -190,16 +189,11 @@ def report_summary
def run_tests(test_files)
report 'Running Unity system tests...'

# Tack on TEST define for compiling unit tests
load_configuration($cfg_file)
test_defines = ['TEST']
$cfg['compiler']['defines']['items'] ||= []
$cfg['compiler']['defines']['items'] << 'TEST'

include_dirs = local_include_dirs

# Build and execute each unit test
test_files.each do |test|
puts "DEBUG: " + test

# Drop Out if we're skipping this type of test
if $cfg[:skip_tests]
Expand All @@ -210,12 +204,7 @@ def run_tests(test_files)
end

obj_list = []

unless $cfg['compiler']['aux_sources'].nil?
$cfg['compiler']['aux_sources'].each do |aux|
obj_list << compile(aux, test_defines)
end
end
test_defines = []

# Detect dependencies and build required modules
extract_headers(test).each do |header|
Expand All @@ -227,14 +216,8 @@ def run_tests(test_files)

# Build the test runner (generate if configured to do so)
test_base = File.basename(test, C_EXTENSION)

runner_name = test_base + '_Runner.c'

runner_path = if $cfg['compiler']['runner_path'].nil?
$cfg['compiler']['build_path'] + runner_name
else
$cfg['compiler']['runner_path'] + runner_name
end
runner_path = 'build/' + runner_name

options = $cfg[:unity]
options[:use_param_tests] = test =~ /parameterized/ ? true : false
Expand All @@ -248,15 +231,8 @@ def run_tests(test_files)
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']
cmd_str = if simulator.nil?
executable
else
"#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}"
end
output = execute(cmd_str)
test_results = $cfg['compiler']['build_path'] + test_base
output = runtest(test_base)
test_results = 'build/' + test_base
if output.match(/OK$/m).nil?
test_results += '.testfail'
else
Expand Down
85 changes: 40 additions & 45 deletions test/targets/ansi.yml
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
compiler:
path: gcc
source_path: '../src/'
unit_tests_path: &unit_tests_path 'tests/'
build_path: &build_path 'build/'
options:
- '-c'
- '-m64'
- '-Wall'
- '-Wno-address'
- '-ansi'
#- '-pedantic'
includes:
prefix: '-I'
items:
- 'src/'
- '../src/'
- 'testdata/'
- *unit_tests_path
defines:
prefix: '-D'
items:
- UNITY_INCLUDE_DOUBLE
- UNITY_SUPPORT_TEST_CASES
- UNITY_EXCLUDE_TESTING_NEW_COMMENTS
- UNITY_SUPPORT_64
object_files:
prefix: '-o'
extension: '.o'
destination: *build_path
linker:
path: gcc
options:
- -lm
- '-m64'
includes:
prefix: '-I'
object_files:
path: *build_path
extension: '.o'
bin_files:
prefix: '-o'
extension: '.exe'
destination: *build_path
---
colour: true
:unity:
:plugins: []
:skip_tests:
- :parameterized
- :parameterized
:tools:
:test_compiler:
:name: compiler
:executable: gcc
:arguments:
- "-c"
- "-m64"
- "-Wall"
- "-Wno-address"
- "-ansi"
- '-I"$": COLLECTION_PATHS_TEST_TOOLCHAIN_INCLUDE'
- '-I"$": COLLECTION_PATHS_TEST_SUPPORT_SOURCE_INCLUDE_VENDOR'
- "-D$: COLLECTION_DEFINES_TEST_AND_VENDOR"
- "${1}"
- "-o ${2}"
:test_linker:
:name: linker
:executable: gcc
:arguments:
- "${1}"
- "-lm"
- "-m64"
- "-o ${2}"
:extension:
:object: ".o"
:executable: ".exe"
:paths:
:test:
- src/
- "../src/"
- testdata/
- tests/
:defines:
:test:
- UNITY_INCLUDE_DOUBLE
- UNITY_SUPPORT_TEST_CASES
- UNITY_EXCLUDE_TESTING_NEW_COMMENTS
- UNITY_SUPPORT_64
Loading

0 comments on commit 3f71d10

Please sign in to comment.