Skip to content

Add :mock_name_include_folder runner option - #854

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

A test cannot use two mocks that share a filename. The runner names each mock's Init(), Verify(), and Destroy() calls by the filename alone. For instance, uart/MockConfig.h and spi/MockConfig.h both produce MockConfig_Init(). This PR adds an opt-in option that keeps the folder a mock is included from. A companion CMock PR adds the matching option on the generator side.

Behavior

With :mock_name_include_folder enabled, #include "uart/MockConfig.h" yields calls to uart_MockConfig_Init(), uart_MockConfig_Verify() and uart_MockConfig_Destroy(). A mock included without a folder keeps its current name. A leading ./ is ignored. The result passes through TypeSanitizer.sanitize_c_identifier as before, so / and \ become _.

The option defaults to off, and runners generated without it are unchanged. Runner and mocks must agree on naming, so enabling it in only one tool breaks the link. It can be set in an options hash, in a :unity YAML section, or in a :cmock section shared with CMock, because grab_config already falls back to that section.

Changes

  • auto/generate_test_runner.rb: default_options gains mock_name_include_folder: false. When the option is on, create_mock_management strips only the extension from each mock include. Otherwise it keeps File.basename(mock, '.*').
  • docs/UnityHelperScriptsGuide.md: new :mock_name_include_folder entry beside :mock_prefix and :mock_suffix.
  • test/tests/test_generate_test_runner.rb: the SameNamedMocksByFolder case generates, compiles, links and runs a runner against two same-named stand-in mocks and checks that each mock's lifecycle functions are called. NameMocksByBasenameWithoutFolderOption checks that the default still names by filename.
  • test/testdata/uart/MockConfig.h, test/testdata/spi/MockConfig.h, test/testdata/testRunnerGeneratorWithSameNamedMocks.c: the stand-in mocks and their test file.

Related

The runner names each mock's Init, Verify and Destroy calls by the mock's
filename alone. Two mocks sharing a filename in different folders therefore
collapse to the same calls. With :mock_name_include_folder enabled the
runner keeps the folder a mock is included from, so uart/MockConfig.h
yields uart_MockConfig_Init(). CMock gains a matching option that names
its generated functions the same way.

The option defaults to off and leaves existing runners unchanged.

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