Add test to ensure MuEditor is removed when flag is off (#359)#362
Add test to ensure MuEditor is removed when flag is off (#359)#362guptapiyush16 wants to merge 4 commits intosven-n:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a CTest check to ensure MuEditor sources are not included in the Main target when ENABLE_EDITOR=OFF, helping prevent editor code from leaking into non-admin builds.
Changes:
- Added a Python-based CTest that scans the configured
Maintarget sources for anyMuEditorpaths whenENABLE_EDITORis disabled. - Updated
tests/CMakeLists.txtto generate amain_sources.txtfile viafile(GENERATE)and register the new test. - Adjusted
src/.gitignoreentries around.tmp/.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/editor/test_leak.py | New Python script that fails if any MuEditor file appears in the generated Main source list. |
| tests/CMakeLists.txt | Generates a sources manifest for Main and adds a CTest entry to run the leak check when ENABLE_EDITOR is off. |
| src/.gitignore | Modifies .tmp ignore patterns (currently introduces a duplicate .tmp/ entry). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| # Add Editor Leak Test (only runs when ENABLE_EDITOR is OFF) | ||
| if(NOT ENABLE_EDITOR) | ||
| # Generate a text file containing all sources of the Main target | ||
| file(GENERATE | ||
| OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/main_sources.txt" | ||
| CONTENT "$<JOIN:$<TARGET_PROPERTY:Main,SOURCES>,\n>") | ||
|
|
||
| # Add a test that runs a Python script to verify MuEditor isn't in those sources | ||
| find_package(Python3 COMPONENTS Interpreter REQUIRED) | ||
| add_test(NAME test_editor_leak | ||
| COMMAND Python3::Interpreter ${CMAKE_CURRENT_SOURCE_DIR}/editor/test_leak.py "${CMAKE_CURRENT_BINARY_DIR}/main_sources.txt") | ||
| endif() |
| @@ -0,0 +1,26 @@ | |||
| import sys | |||
| import os | |||
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the project's build integrity by implementing a regression test to ensure that optional components are properly excluded from the final binary. By leveraging CMake's generation capabilities and a lightweight verification script, the build process now automatically validates that the MuEditor source files do not inadvertently leak into the Main target when the feature is toggled off. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a leak detection test to ensure that MuEditor source files are not included in the build when the editor is disabled. This is implemented via a new Python script and CMake configuration. Feedback focuses on improving the robustness of the leak detection logic to avoid false positives, removing a duplicate entry in the .gitignore file, cleaning up an unused import, and specifying the file encoding when reading source lists.
| with open(sources_file, 'r') as f: | ||
| sources = f.read().splitlines() | ||
|
|
||
| leaked_files = [s for s in sources if 'MuEditor' in s] |
There was a problem hiding this comment.
The check 'MuEditor' in s is prone to false positives if the project is cloned into a directory that contains "MuEditor" in its path (e.g., /home/user/MuEditor/src/...). A more robust check should look for the specific directory structure of the editor sources, such as src/MuEditor/ or a relative path starting with MuEditor/.
| leaked_files = [s for s in sources if 'MuEditor' in s] | |
| leaked_files = [s for s in sources if 'src/MuEditor/' in s.replace('\\', '/') or s.replace('\\', '/').startswith('MuEditor/')] |
| *.sdf | ||
| .tmp/ | ||
| .tmp\ | ||
| .tmp/ |
| @@ -0,0 +1,26 @@ | |||
| import sys | |||
| import os | |||
|
|
||
| sources_file = sys.argv[1] | ||
|
|
||
| with open(sources_file, 'r') as f: |
There was a problem hiding this comment.
It is recommended to specify an explicit encoding (e.g., 'utf-8') when opening files to ensure consistent behavior across different platforms and locales, especially since CMake's file(GENERATE) typically outputs UTF-8.
| with open(sources_file, 'r') as f: | |
| with open(sources_file, 'r', encoding='utf-8') as f: |
|
@guptapiyush16 notify me once all conversations are resolved 👍🏼 |
a0e0e95 to
4b4d074
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Test tree. New test modules go in their own subdirectory and call | ||
| # add_subdirectory() below. Keep the entry point here minimal — module | ||
| # CMakeLists.txt files own their own sources, link, and call mu_add_test(). | ||
|
|
||
| include(third_party/doctest/doctest.cmake) | ||
|
|
||
| # When cross-compiling Windows binaries on a non-Windows host (CI's Linux | ||
| # runner, WSL/MinGW), wine has to launch the .exe. Setting this once makes | ||
| # both add_test and doctest_discover_tests pick it up via the | ||
| # CROSSCOMPILING_EMULATOR target property. | ||
| if(WIN32 AND NOT CMAKE_HOST_WIN32 AND NOT CMAKE_CROSSCOMPILING_EMULATOR) | ||
| find_program(WINE_EXECUTABLE wine) | ||
| if(NOT WINE_EXECUTABLE) | ||
| message(FATAL_ERROR | ||
| "BUILD_TESTING=ON on a non-Windows host requires wine to run " | ||
| "the cross-compiled test binaries. Install it (Ubuntu/Debian: " | ||
| "sudo apt-get install wine wine32) or configure with " | ||
| "-DBUILD_TESTING=OFF.") | ||
| endif() | ||
| set(CMAKE_CROSSCOMPILING_EMULATOR ${WINE_EXECUTABLE} CACHE STRING "" FORCE) | ||
| endif() | ||
|
|
||
| add_library(mu_test_main STATIC main.cpp) | ||
| target_include_directories(mu_test_main PUBLIC third_party/doctest) | ||
| target_compile_features(mu_test_main PUBLIC cxx_std_20) | ||
| # Test sources contain non-ASCII wide string literals. MSVC defaults to the | ||
| # system codepage when reading sources, which mangles UTF-8 characters into | ||
| # wrong codepoints and breaks every non-Latin language test. /utf-8 forces | ||
| # both the source charset and the narrow execution charset to UTF-8; the wide | ||
| # execution charset stays UTF-16 (Windows wchar_t). | ||
| if(MSVC) | ||
| target_compile_options(mu_test_main PUBLIC /utf-8) | ||
| endif() | ||
|
|
||
| # Helper for module CMakeLists. Usage: | ||
| # mu_add_test(NAME <test_name> SOURCES a.cpp b.cpp LINK_LIBS lib1 lib2) | ||
| # Registers each TEST_CASE inside the binary as its own CTest entry, so | ||
| # failures point at the specific case in CI logs. | ||
| function(mu_add_test) | ||
| cmake_parse_arguments(MAT "" "NAME" "SOURCES;LINK_LIBS" ${ARGN}) | ||
| add_executable(${MAT_NAME} ${MAT_SOURCES}) | ||
| target_link_libraries(${MAT_NAME} PRIVATE mu_test_main ${MAT_LINK_LIBS}) | ||
| target_include_directories(${MAT_NAME} PRIVATE | ||
| ${CMAKE_SOURCE_DIR}/src/source | ||
| ${CMAKE_CURRENT_SOURCE_DIR}/third_party/doctest | ||
| ) | ||
| if(MSVC) | ||
| target_compile_options(${MAT_NAME} PRIVATE /utf-8) | ||
| endif() | ||
| doctest_discover_tests(${MAT_NAME}) | ||
| endfunction() | ||
|
|
||
| add_subdirectory(text) | ||
|
|
||
| # Add Editor Leak Test (only runs when ENABLE_EDITOR is OFF) | ||
| if(NOT ENABLE_EDITOR) | ||
| # Generate a text file containing all sources of the Main target | ||
| file(GENERATE | ||
| OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/main_sources.txt" | ||
| CONTENT "$<JOIN:$<TARGET_PROPERTY:Main,SOURCES>,\n>") | ||
|
|
||
| # Add a test that runs a Python script to verify MuEditor isn't in those sources | ||
| find_package(Python3 COMPONENTS Interpreter REQUIRED) | ||
| add_test(NAME test_editor_leak |
| include(third_party/doctest/doctest.cmake) | ||
|
|
| add_library(mu_test_main STATIC main.cpp) | ||
| target_include_directories(mu_test_main PUBLIC third_party/doctest) | ||
| target_compile_features(mu_test_main PUBLIC cxx_std_20) | ||
| # Test sources contain non-ASCII wide string literals. MSVC defaults to the | ||
| # system codepage when reading sources, which mangles UTF-8 characters into | ||
| # wrong codepoints and breaks every non-Latin language test. /utf-8 forces | ||
| # both the source charset and the narrow execution charset to UTF-8; the wide | ||
| # execution charset stays UTF-16 (Windows wchar_t). | ||
| if(MSVC) | ||
| target_compile_options(mu_test_main PUBLIC /utf-8) | ||
| endif() |
| add_subdirectory(text) | ||
|
|
| # Add a test that runs a Python script to verify MuEditor isn't in those sources | ||
| find_package(Python3 COMPONENTS Interpreter REQUIRED) | ||
| add_test(NAME test_editor_leak | ||
| COMMAND Python3::Interpreter ${CMAKE_CURRENT_SOURCE_DIR}/editor/test_leak.py "${CMAKE_CURRENT_BINARY_DIR}/main_sources.txt") |
| # Add a test that runs a Python script to verify MuEditor isn't in those sources | ||
| find_package(Python3 COMPONENTS Interpreter REQUIRED) | ||
| add_test(NAME test_editor_leak | ||
| COMMAND Python3::Interpreter ${CMAKE_CURRENT_SOURCE_DIR}/editor/test_leak.py "${CMAKE_CURRENT_BINARY_DIR}/main_sources.txt") |
| find_package(Python3 COMPONENTS Interpreter REQUIRED) | ||
| add_test(NAME test_editor_leak | ||
| COMMAND Python3::Interpreter ${CMAKE_CURRENT_SOURCE_DIR}/editor/test_leak.py "${CMAKE_CURRENT_BINARY_DIR}/main_sources.txt") |
| with open(sources_file, 'r', encoding='utf-8') as f: | ||
| sources = f.read().splitlines() | ||
|
|
||
| # Normalise to forward slashes so the path-segment check is | ||
| # platform-independent and won't fire just because the repository | ||
| # happens to be cloned inside a directory called "MuEditor". | ||
| leaked_files = [s for s in sources if '/MuEditor/' in s.replace('\\', '/')] | ||
|
|
||
| if leaked_files: | ||
| print("FAIL: MuEditor leaked into the build! The following editor files were found in the Main target sources:") | ||
| for f in leaked_files: | ||
| print(f" - {f}") |
| find_package(Python3 COMPONENTS Interpreter QUIET) | ||
| if(Python3_Interpreter_FOUND) | ||
| add_test(NAME test_editor_leak | ||
| COMMAND Python3::Interpreter ${CMAKE_CURRENT_SOURCE_DIR}/editor/test_leak.py "${CMAKE_CURRENT_BINARY_DIR}/main_sources.txt") |
| # Normalise to forward slashes so the path-segment check is | ||
| # platform-independent and won't fire just because the repository | ||
| # happens to be cloned inside a directory called "MuEditor". | ||
| leaked_files = [s for s in sources if '/MuEditor/' in s.replace('\\', '/')] |
Summary
This PR adds an automated test to ensure that the
MuEditorcomponent is completely excluded from the build when theENABLE_EDITORflag is set toOFF.It utilizes CMake's
file(GENERATE)command to statically dump the final list of source files configured for theMaintarget during a build. A lightweight Python script is then hooked intoctestto parse those sources and fail the build if any paths containingMuEditoraccidentally leak through.Related issues
Closes #359
Checklist
docs/CODING_RULES.md.docs/build-guide.md).