#!/usr/bin/env python3 """ Generate a single GoogleTest binary from lightweight per-source test stubs. Run from repo root: python3 tests/gtest/generate_gtests.py """ import os from pathlib import Path ROOT = Path(__file__).resolve().parents[2] SRC_DIR = ROOT / 'Z' / 'src' OUT_DIR = ROOT / 'tests' / 'gtest' / 'Z' OUT_DIR.mkdir(parents=True, exist_ok=True) TEMPLATE = '''#include \n{includes}\n\nTEST({test_suite}, smoke) {{\n // TODO: replace with meaningful unit tests for {rel_cpp}\n ASSERT_TRUE(true);\n}}\n''' sources = [] for cpp in sorted(SRC_DIR.rglob('*.cpp')): rel = cpp.relative_to(SRC_DIR) name = str(rel).replace(os.sep, '__') out_cpp = OUT_DIR / (name + '_test.cpp') # try to include corresponding header if exists header = cpp.with_suffix('.h') includes = '' if header.exists(): includes = f'#include "{header.relative_to(ROOT)}"' else: includes = '// no corresponding header to include' content = TEMPLATE.format(includes=includes, test_suite=name.replace('.', '_'), rel_cpp=str(rel)) out_cpp.write_text(content) sources.append(str(Path('tests/gtest/Z') / (name + '_test.cpp'))) # Write CMakeLists for gtests cmake = ROOT / 'tests' / 'gtest' / 'CMakeLists.txt' cmake.write_text(''' cmake_minimum_required(VERSION 3.21) project(GTests LANGUAGES CXX) include(FetchContent) FetchContent_Declare( googletest URL https://github.com/google/googletest/archive/refs/tags/release-1.14.0.tar.gz ) FetchContent_MakeAvailable(googletest) # Collect generated test sources file(GLOB GTEST_SRCS "${CMAKE_SOURCE_DIR}/Z/*_test.cpp") add_executable(gtests_all ${GTEST_SRCS}) target_compile_features(gtests_all PRIVATE cxx_std_20) target_include_directories(gtests_all PRIVATE ${CMAKE_SOURCE_DIR}/../../) # link to gtest target_link_libraries(gtests_all PRIVATE GTest::gtest_main) include(GoogleTest) gtest_discover_tests(gtests_all) ''') print(f"Wrote {len(sources)} test stubs to {OUT_DIR}") print("Run: cmake -S . -B build && cmake --build build -- -j5 && ctest --test-dir build -j5")