Skip to content

Add test to ensure MuEditor is removed when flag is off (#359)#362

Open
guptapiyush16 wants to merge 4 commits intosven-n:mainfrom
guptapiyush16:add-editor-leak-test
Open

Add test to ensure MuEditor is removed when flag is off (#359)#362
guptapiyush16 wants to merge 4 commits intosven-n:mainfrom
guptapiyush16:add-editor-leak-test

Conversation

@guptapiyush16
Copy link
Copy Markdown

Summary

This PR adds an automated test to ensure that the MuEditor component is completely excluded from the build when the ENABLE_EDITOR flag is set to OFF.

It utilizes CMake's file(GENERATE) command to statically dump the final list of source files configured for the Main target during a build. A lightweight Python script is then hooked into ctest to parse those sources and fail the build if any paths containing MuEditor accidentally leak through.

Related issues

Closes #359

Checklist

Copilot AI review requested due to automatic review settings May 3, 2026 05:27
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Main target sources for any MuEditor paths when ENABLE_EDITOR is disabled.
  • Updated tests/CMakeLists.txt to generate a main_sources.txt file via file(GENERATE) and register the new test.
  • Adjusted src/.gitignore entries 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.

Comment thread tests/CMakeLists.txt Outdated
Comment on lines +54 to +66

# 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()
Comment thread src/.gitignore Outdated
Comment thread tests/editor/test_leak.py Outdated
@@ -0,0 +1,26 @@
import sys
import os
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Automated Leak Detection: Introduced a new Python-based test to verify that MuEditor components are correctly excluded from the build when the ENABLE_EDITOR flag is disabled.
  • CMake Integration: Updated CMake configuration to generate a source file list for the Main target and register the leak detection test via ctest.
  • Repository Cleanup: Standardized .gitignore entries to ensure consistent path formatting.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/editor/test_leak.py Outdated
with open(sources_file, 'r') as f:
sources = f.read().splitlines()

leaked_files = [s for s in sources if 'MuEditor' in s]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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/.

Suggested change
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/')]

Comment thread src/.gitignore Outdated
*.sdf
.tmp/
.tmp\
.tmp/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This entry is now a duplicate of line 6. Consider removing this line to keep the .gitignore file clean.

Comment thread tests/editor/test_leak.py Outdated
@@ -0,0 +1,26 @@
import sys
import os
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The os module is imported but not used anywhere in the script.

Comment thread tests/editor/test_leak.py Outdated

sources_file = sys.argv[1]

with open(sources_file, 'r') as f:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
with open(sources_file, 'r') as f:
with open(sources_file, 'r', encoding='utf-8') as f:

@Mosch0512 Mosch0512 self-assigned this May 3, 2026
@Mosch0512
Copy link
Copy Markdown
Collaborator

@guptapiyush16 notify me once all conversations are resolved 👍🏼

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/editor/CMakeLists.txt Outdated
Comment on lines +1 to +64
# 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
Comment thread tests/editor/CMakeLists.txt Outdated
Comment on lines +5 to +6
include(third_party/doctest/doctest.cmake)

Comment thread tests/editor/CMakeLists.txt Outdated
Comment on lines +23 to +33
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()
Comment thread tests/editor/CMakeLists.txt Outdated
Comment on lines +53 to +54
add_subdirectory(text)

Comment thread tests/editor/CMakeLists.txt Outdated
# 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")
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread tests/editor/CMakeLists.txt Outdated
# 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")
Comment thread tests/editor/CMakeLists.txt Outdated
Comment on lines +13 to +15
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")
Comment thread tests/editor/test_leak.py
Comment on lines +10 to +21
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}")
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread tests/editor/CMakeLists.txt Outdated
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")
Comment thread tests/editor/test_leak.py
Comment on lines +13 to +16
# 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('\\', '/')]
@guptapiyush16 guptapiyush16 requested a review from Copilot May 6, 2026 15:11
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.

Add Tests if MuEditor is completly removed when flag is not set

3 participants