Add support for subdirs (ref issue #1)

New option :subdir has been added.  Creating a mock for
e.g. sys/types.h using cmock_generator.rb with this option
set in config file, or more likely, command line parameter
--subdir=sys makes cmock create the files
:cmock_path/:subdir/:cmock_prefix+"types.[ch]"

The generated mock file will include the original header
file using #include <sys/types.h> as expected.

Only the generating code has been changed, the example
code with scripts for creating makefiles/creating mocks
using rake was too much work for me, and I didn't need any
of it.
This commit is contained in:
Eivind Tagseth
2015-04-10 22:31:32 +02:00
parent 534a290322
commit 2b89990106
5 changed files with 31 additions and 7 deletions
+1
View File
@@ -11,6 +11,7 @@ class CMockConfig
:framework => :unity,
:mock_path => 'mocks',
:mock_prefix => 'Mock',
:subdir => nil,
:plugins => [],
:strippables => ['(?:__attribute__\s*\(+.*?\)+)'],
:attributes => ['__ramfunc', '__irq', '__fiq', 'register', 'extern'],
+10 -3
View File
@@ -12,10 +12,17 @@ class CMockFileWriter
@config = config
end
def create_file(filename)
def create_subdir(subdir)
if subdir && !Dir.exists?("#{@config.mock_path}/#{subdir+'/' if subdir}")
require 'fileutils'
FileUtils.mkdir_p "#{@config.mock_path}/#{subdir+'/' if subdir}"
end
end
def create_file(filename, subdir)
raise "Where's the block of data to create?" unless block_given?
full_file_name_temp = "#{@config.mock_path}/#{filename}.new"
full_file_name_done = "#{@config.mock_path}/#{filename}"
full_file_name_temp = "#{@config.mock_path}/#{subdir+'/' if subdir}#{filename}.new"
full_file_name_done = "#{@config.mock_path}/#{subdir+'/' if subdir}#{filename}"
File.open(full_file_name_temp, 'w') do |file|
yield(file, filename)
end
+13 -4
View File
@@ -17,6 +17,8 @@ class CMockGenerator
@ordered = @config.enforce_strict_ordering
@framework = @config.framework.to_s
@subdir = @config.subdir
@includes_h_pre_orig_header = (@config.includes || @config.includes_h_pre_orig_header || []).map{|h| h =~ /</ ? h : "\"#{h}\""}
@includes_h_post_orig_header = (@config.includes_h_post_orig_header || []).map{|h| h =~ /</ ? h : "\"#{h}\""}
@includes_c_pre_header = (@config.includes_c_pre_header || []).map{|h| h =~ /</ ? h : "\"#{h}\""}
@@ -39,14 +41,21 @@ class CMockGenerator
@module_name = module_name
@mock_name = @prefix + @module_name
@clean_mock_name = TypeSanitizer.sanitize_c_identifier(@mock_name)
create_mock_subdir()
create_mock_header_file(parsed_stuff)
create_mock_source_file(parsed_stuff)
end
private if $ThisIsOnlyATest.nil? ##############################
def create_mock_subdir()
if @subdir
@file_writer.create_subdir(@subdir)
end
end
def create_mock_header_file(parsed_stuff)
@file_writer.create_file(@mock_name + ".h") do |file, filename|
@file_writer.create_file(@mock_name + ".h", @subdir) do |file, filename|
create_mock_header_header(file, filename)
create_mock_header_service_call_declarations(file)
create_typedefs(file, parsed_stuff[:typedefs])
@@ -58,7 +67,7 @@ class CMockGenerator
end
def create_mock_source_file(parsed_stuff)
@file_writer.create_file(@mock_name + ".c") do |file, filename|
@file_writer.create_file(@mock_name + ".c", @subdir) do |file, filename|
create_source_header_section(file, filename)
create_instance_structure(file, parsed_stuff[:functions])
create_extern_declarations(file)
@@ -74,7 +83,7 @@ class CMockGenerator
def create_mock_header_header(file, filename)
define_name = @clean_mock_name.upcase
orig_filename = filename[@config.mock_prefix.size..-1]
orig_filename = (@subdir ? @subdir + "/" : "") + filename[@config.mock_prefix.size..-1]
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n"
file << "#ifndef _#{define_name}_H\n"
file << "#define _#{define_name}_H\n\n"
@@ -112,7 +121,7 @@ class CMockGenerator
end
def create_source_header_section(file, filename)
header_file = filename.gsub(".c",".h")
header_file = (@subdir ? @subdir + '/' : '') + filename.gsub(".c",".h")
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n"
file << "#include <string.h>\n"
file << "#include <stdlib.h>\n"