From 2b89990106553704de6a5b496858724093224f3e Mon Sep 17 00:00:00 2001 From: Eivind Tagseth Date: Fri, 10 Apr 2015 22:31:32 +0200 Subject: [PATCH] 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 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. --- docs/CMock_Summary.md | 4 ++++ lib/cmock_config.rb | 1 + lib/cmock_file_writer.rb | 13 ++++++++++--- lib/cmock_generator.rb | 17 +++++++++++++---- test/unit/cmock_generator_main_test.rb | 3 +++ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/docs/CMock_Summary.md b/docs/CMock_Summary.md index e7fca6b..7b684d6 100644 --- a/docs/CMock_Summary.md +++ b/docs/CMock_Summary.md @@ -284,6 +284,10 @@ Defined in the yaml file, they look more like this: The prefix to append to your mock files. Defaults to “Mock”, so a file “USART.h” will get a mock called “MockUSART.c” +* `:subdir`: + Relative subdir for your mocks. Set this to e.g. "sys" in order to + create mock for `sys/types.h` in `:mock_path`/sys/ + * `:plugins`: An array of which plugins to enable. 'expect' is always active. Also available currently are `:ignore,` `:ignore_args,` `:array,` diff --git a/lib/cmock_config.rb b/lib/cmock_config.rb index ec18bcc..058157b 100644 --- a/lib/cmock_config.rb +++ b/lib/cmock_config.rb @@ -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'], diff --git a/lib/cmock_file_writer.rb b/lib/cmock_file_writer.rb index efbef61..66dae03 100644 --- a/lib/cmock_file_writer.rb +++ b/lib/cmock_file_writer.rb @@ -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 diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index fd34e9b..83dcd1d 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -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 =~ /\n" file << "#include \n" diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index 40513ce..a38059e 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -50,6 +50,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @config.expect :includes_h_post_orig_header, nil @config.expect :includes_c_pre_header, nil @config.expect :includes_c_post_header, nil + @config.expect :subdir, nil @cmock_generator = CMockGenerator.new(@config, @file_writer, @utils, @plugins) @cmock_generator.module_name = @module_name @cmock_generator.mock_name = "Mock#{@module_name}" @@ -64,6 +65,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @config.expect :includes_h_post_orig_header, nil @config.expect :includes_c_pre_header, nil @config.expect :includes_c_post_header, nil + @config.expect :subdir, nil @cmock_generator_strict = CMockGenerator.new(@config, @file_writer, @utils, @plugins) @cmock_generator_strict.module_name = @module_name @cmock_generator_strict.mock_name = "Mock#{@module_name}" @@ -116,6 +118,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do @config.expect :includes_h_post_orig_header, nil @config.expect :includes_c_pre_header, nil @config.expect :includes_c_post_header, nil + @config.expect :subdir, nil @cmock_generator2 = CMockGenerator.new(@config, @file_writer, @utils, @plugins) @cmock_generator2.module_name = "Pout-Pout Fish" @cmock_generator2.mock_name = "MockPout-Pout Fish"