Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmock: can't use :subdir option because of the way generated c files are added to the MOCK_GENERATED_SRCS list (IDFGH-12326) #13360

Open
3 tasks done
BastienGermond opened this issue Mar 11, 2024 · 4 comments · May be fixed by #15308
Assignees
Labels
Status: Opened Issue is new

Comments

@BastienGermond
Copy link

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

Hello,

While trying to play with CMock for my project I noticed that it is not possible to mock component that use some kind of namespaced headers (e.g #include <myproject/logic.h>) and for that use :subdir option from CMock.

The issue is caused by the way mocked files are added in:

list(APPEND MOCK_GENERATED_SRCS "${MOCK_GEN_DIR}/mocks/Mock${file_without_dir}.c")

Every mocked files are expected to be directly under /mocks/, is it possible to maybe add another configuration option to idf_component_mock to specify a SUBFOLDER ?

Since the :subdir option is closely related to :mock_path maybe it would be useful to have by default mocks and something else if specified otherwise ?


Example of mock_config.yaml used :

:cmock:
  :subdir: "myproject"
  :plugins:
    - :ignore
    - :callback

Currently, the build fails because it will not be able to find the c files generated under mocks/myproject/Mock<...>.c and expect c files mocks/Mock<...>.c.

@espressif-bot espressif-bot added the Status: Opened Issue is new label Mar 11, 2024
@github-actions github-actions bot changed the title cmock: can't use :subdir option because of the way generated c files are added to the MOCK_GENERATED_SRCS list cmock: can't use :subdir option because of the way generated c files are added to the MOCK_GENERATED_SRCS list (IDFGH-12326) Mar 11, 2024
@0xjakob
Copy link
Contributor

0xjakob commented Mar 12, 2024

Hi @BastienGermond! It seems that there are two different aspects here:

  1. The place where the generated mock implementations are placed.
  2. Problems including the correct header files.

Do you have problems with both or just one of these aspects?
Regarding 1.: Usually, it shouldn't matter where the source files (*.c) are placed, the includes are absolute paths. Furthermore, the IDF build system should take care of setting up the compiler calls so all source files are found. Do you have source files with the same name which would conflict with each other after the mocks have been generated?
Regarding 2.: This should also work as long as the include directories are set in the CMakeLists.txt of your mocking component (the one that is creating the mocks).

@BastienGermond
Copy link
Author

Hi @0xjakob,

  1. The place where the generated mock implementations are placed.

The issue is that the files generated with the cmock_configuration :subdir: myproject would be created in this path ${MOCK_GEN_DIR}/mocks/myproject/Mock${file_without_dir}.[ch].

What I'm trying to say is that currently the path is hardcoded expecting the default values from CMock (:subdir: "", :mock_path: "mocks" and :mock_prefix: "Mock").

In case we have a mock_config.yaml like this :

:cmock:
  :subdir: "myproject"
  :mock_prefix: "Espressif"
  :mock_path: "EspMocks"
  :plugins:
    - :ignore
    - :callback

This would generate files following this path : ${MOCK_GEN_DIR}/EspMocks/myproject/Espressif${file_without_dir}.[ch]

the IDF build system should take care of setting up the compiler calls so all source files are found.

list(APPEND MOCK_GENERATED_HEADERS "${MOCK_GEN_DIR}/mocks/Mock${file_without_dir}.h")
list(APPEND MOCK_GENERATED_SRCS "${MOCK_GEN_DIR}/mocks/Mock${file_without_dir}.c")

idf_component_register(SRCS "${MOCK_GENERATED_SRCS}"
INCLUDE_DIRS ${__INCLUDE_DIRS}
REQUIRES ${__REQUIRES})

And because of this different path the c-files are not registered correctly (and thus not found/compiled during compilation causing missing symbols during linking).

  1. Problems including the correct header files.

I also have this problem caused by not using :subdir, in my project I use a #include "myproject/nvs.h" but generating mocks for this without subdir will include "nvs.h" and not the expected "myproject/nvs.h" in Mocknvs.h.

I patched it the first time the same way esp-idf does for freertos with adding manually my correct header with :includes_h_pre_orig_header.

:includes_h_pre_orig_header:
- FreeRTOSConfig.h
- FreeRTOS.h

CMock still add an #include "nvs.h" in the mock header generated but at least this added afterward the correct #include "myproject/nvs.h".

However, in case the "nvs.h" was not something found in any include path or something that is not wanted in this header, the compilation will fail with an include not found error or maybe collide with the right header (myproject/nvs.h).


I apologize if my explanation was unclear. Please let me know if you did not catch what I mean and I will try to rephrase it 😄.

@0xjakob
Copy link
Contributor

0xjakob commented Mar 12, 2024

It starts to make much more sense now, thanks! Supporting :subdir: makes sense then, in particular because of the includes.

Regarding support for custom :mock_path: and :mock_dir:: I think we could also add that option, although I don't quite understand why they are necessary. Taking :subdir: aside, is there a situation where you have a custom :mock_path: or :mock_prefix: configured in IDF?

@BastienGermond
Copy link
Author

Taking :subdir: aside, is there a situation where you have a custom :mock_path: or :mock_prefix: configured in IDF?

Not really, and I agree that I don't see a use case right now. Maybe some company may have some convention about filename enforced by tools ?

I was mentioning them only since they will not work with the current process, the real issue for me was the missing :subdir support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Opened Issue is new
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants