Skip to content

Add :mock_name_include_folder option - #540

Open
mkarlesky wants to merge 1 commit into
masterfrom
feature/mock-name-include-folder
Open

mkarlesky wants to merge 1 commit into
masterfrom
feature/mock-name-include-folder

Conversation

@mkarlesky

@mkarlesky mkarlesky commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Two mocks of the same filename cannot coexist in one test. CMock derives a mock's include guard and its Init(), Verify(), and Destroy() function names from the mock's filename alone. For example, mocks of uart/config.h and spi/config.h share the include guard _MOCKCONFIG_H causing the second header to compile as empty. Both also define Mockconfig_Init() and its siblings, which collide at link. This PR adds an opt-in option that folds the mock's folder into that name.

Behavior

With :mock_name_include_folder enabled, a mock's name includes the folder it is generated into below :mock_path. That folder is built from :subdir and any folder passed to setup_mocks, exactly as the existing placement logic in create_mock builds it. A mock of config.h generated into uart/ gets the guard _UART_MOCKCONFIG_H and functions named uart_Mockconfig_Init() and so on. A mock with no folder keeps its current name, and a leading ./ is ignored.

The option defaults to off, and mocks generated without it are unchanged. Unity's runner generator needs its option of the same name to call the renamed functions. A test should include such a mock by the same folder, which is the path the runner generator sees.

Changes

  • lib/cmock_config.rb: :mock_name_include_folder => false in CMOCK_DEFAULT_OPTIONS.
  • lib/cmock_generator.rb: initialize reads the option once. When it is on and a folder is present, create_mock computes :clean_name from the folder plus the mock name. Guard and lifecycle names already derive from :clean_name.
  • docs/CMock_Summary.md: new :mock_name_include_folder entry.
  • test/unit/cmock_generator_main_test.rb: covers the default, the option on with a folder (including a ./ prefix), and the option on without a folder. Both generator fixtures gain the new config expectation.
  • vendor/unity: moves to the Unity companion PR's commit so both tools carry the matching option. That commit descends from the previous pin.

Related

CMock names a mock's include guard and its Init, Verify and Destroy
functions by the mock's filename alone. Two mocks sharing a filename in
different folders therefore share a guard and define the same symbols.
With :mock_name_include_folder enabled the generator folds the folder a
mock is generated into below :mock_path into that name, so a mock of
config.h in uart/ becomes uart_Mockconfig.

The option defaults to off and leaves existing mocks unchanged. Unity's
test runner generator gains a matching option, and vendor/unity moves to
that commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant