Skip to content

Commit

Permalink
🐛 Test runners now generated with test includes
Browse files Browse the repository at this point in the history
Closes PR #622
  • Loading branch information
mkarlesky committed Jul 18, 2024
1 parent 147d57f commit 80cc9c4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/ceedling/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def generate_mock(context:, mock:, test:, input_filepath:, output_path:)
end
end

def generate_test_runner(context:, mock_list:, test_filepath:, input_filepath:, runner_filepath:)
def generate_test_runner(context:, mock_list:, includes_list:, test_filepath:, input_filepath:, runner_filepath:)
arg_hash = {
:context => context,
:test_file => test_filepath,
Expand Down Expand Up @@ -102,6 +102,7 @@ def generate_test_runner(context:, mock_list:, test_filepath:, input_filepath:,
module_name: module_name,
runner_filepath: runner_filepath,
mock_list: mock_list,
test_file_includes: includes_list,
header_extension: @configurator.extension_header
)
rescue
Expand Down
2 changes: 1 addition & 1 deletion lib/ceedling/generator_test_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize(config:, test_file_contents:, preprocessed_file_contents:nil)
parse_test_file( test_file_contents, preprocessed_file_contents )
end

def generate(module_name:, runner_filepath:, mock_list:, test_file_includes:[], header_extension:)
def generate(module_name:, runner_filepath:, mock_list:, test_file_includes:, header_extension:)
# Actually build the test runner using Unity's test runner generator.
@unity_runner_generator.generate(
module_name,
Expand Down
31 changes: 23 additions & 8 deletions lib/ceedling/test_context_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class TestContextExtractor
constructor :configurator, :file_wrapper, :loginator

def setup
@header_includes = {}
@source_includes = {}
@all_header_includes = {} # Full list of all headers a test #includes
@header_includes = {} # List of all headers minus mocks & framework files
@source_includes = {}
@source_extras = {}
@test_runner_details = {} # Test case lists & Unity runner generator instances
@mocks = {}
Expand Down Expand Up @@ -63,7 +64,12 @@ def extract_includes(filepath)
return extract_includes( filepath, content )
end

# Header includes of test file with file extension
# All header includes .h of test file
def lookup_full_header_includes_list(filepath)
return @all_header_includes[form_file_key( filepath )] || []
end

# Header includes .h (minus mocks & framework headers) in test file
def lookup_header_includes_list(filepath)
return @header_includes[form_file_key( filepath )] || []
end
Expand Down Expand Up @@ -108,19 +114,27 @@ def ingest_includes(filepath, includes)
mock_prefix = @configurator.cmock_mock_prefix
file_key = form_file_key( filepath )

mocks = []
headers = []
sources = []
mocks = []
all_headers = []
headers = []
sources = []

includes.each do |include|
# <*.h>
if include =~ /#{Regexp.escape(@configurator.extension_header)}$/
# Check if include is a mock with regex match that extracts only mock name (no .h)
scan_results = include.scan(/(#{mock_prefix}.+)#{Regexp.escape(@configurator.extension_header)}/)
mocks << scan_results[0][0] if (scan_results.size > 0)

if (scan_results.size > 0)
# Collect mock name
mocks << scan_results[0][0]
else
# If not a mock or framework file, collect tailored header filename
headers << include unless VENDORS_FILES.include?( include.ext('') )
end

# Add to .h includes list
headers << include
all_headers << include
# <*.c>
elsif include =~ /#{Regexp.escape(@configurator.extension_source)}$/
# Add to .c includes list
Expand All @@ -130,6 +144,7 @@ def ingest_includes(filepath, includes)

@lock.synchronize do
@mocks[file_key] = mocks
@all_header_includes[file_key] = all_headers
@header_includes[file_key] = headers
@source_includes[file_key] = sources
end
Expand Down
1 change: 1 addition & 0 deletions lib/ceedling/test_invoker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ def setup_and_invoke(tests:, context:TEST_SYM, options:{})
arg_hash = {
context: TEST_SYM,
mock_list: details[:mock_list],
includes_list: @test_context_extractor.lookup_header_includes_list( details[:filepath] ),
test_filepath: details[:filepath],
input_filepath: details[:runner][:input_filepath],
runner_filepath: details[:runner][:output_filepath]
Expand Down
2 changes: 1 addition & 1 deletion lib/ceedling/test_invoker_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def extract_sources(test_filepath)
_support_headers = COLLECTION_ALL_SUPPORT.map { |filepath| File.basename(filepath).ext(EXTENSION_HEADER) }

# Get all #include .h files from test file so we can find any source files by convention
includes = @test_context_extractor.lookup_header_includes_list(test_filepath)
includes = @test_context_extractor.lookup_full_header_includes_list(test_filepath)
includes.each do |include|
_basename = File.basename(include)
next if _basename == UNITY_H_FILE # Ignore Unity in this list
Expand Down

0 comments on commit 80cc9c4

Please sign in to comment.