|
| 1 | +# CMake to build and test fboss-image/distro_cli |
| 2 | + |
| 3 | +# In general, libraries and binaries in fboss/foo/bar are built by |
| 4 | +# cmake/FooBar.cmake |
| 5 | + |
| 6 | +# distro_cli requires Python 3.10+ with widespread use of union type syntax |
| 7 | +# Save and temporarily override Python3_EXECUTABLE |
| 8 | +if(DEFINED Python3_EXECUTABLE) |
| 9 | + set(SAVED_Python3_EXECUTABLE "${Python3_EXECUTABLE}") |
| 10 | +endif() |
| 11 | + |
| 12 | +# Find the highest available Python version >= 3.10 |
| 13 | +find_package(Python3 3.10 COMPONENTS Interpreter REQUIRED) |
| 14 | +message(STATUS "Using Python ${Python3_VERSION} (${Python3_EXECUTABLE}) for distro_cli tests") |
| 15 | + |
| 16 | +include(FBPythonBinary) |
| 17 | + |
| 18 | +file(GLOB DISTRO_CLI_TEST_SOURCES |
| 19 | + "fboss-image/distro_cli/tests/*_test.py" |
| 20 | +) |
| 21 | + |
| 22 | +# Exclude image_builder_test.py - requires source tree access for templates |
| 23 | +# which isn't available in CMake build directory |
| 24 | +list(FILTER DISTRO_CLI_TEST_SOURCES EXCLUDE REGEX "image_builder_test\\.py$") |
| 25 | + |
| 26 | +# Exclude: manual e2e tests |
| 27 | +list(FILTER DISTRO_CLI_TEST_SOURCES EXCLUDE REGEX "kernel_build_test\\.py$") |
| 28 | +list(FILTER DISTRO_CLI_TEST_SOURCES EXCLUDE REGEX "sai_build_test\\.py$") |
| 29 | + |
| 30 | +# Exclude: Docker not available |
| 31 | +list(FILTER DISTRO_CLI_TEST_SOURCES EXCLUDE REGEX "build_entrypoint_test\\.py$") |
| 32 | +list(FILTER DISTRO_CLI_TEST_SOURCES EXCLUDE REGEX "build_test\\.py$") |
| 33 | +list(FILTER DISTRO_CLI_TEST_SOURCES EXCLUDE REGEX "docker_test\\.py$") |
| 34 | + |
| 35 | +file(GLOB DISTRO_CLI_TEST_HELPERS |
| 36 | + "fboss-image/distro_cli/tests/test_helpers.py" |
| 37 | +) |
| 38 | + |
| 39 | +file(GLOB_RECURSE DISTRO_CLI_LIB_SOURCES |
| 40 | + "fboss-image/distro_cli/builder/*.py" |
| 41 | + "fboss-image/distro_cli/cmds/*.py" |
| 42 | + "fboss-image/distro_cli/lib/*.py" |
| 43 | + "fboss-image/distro_cli/tools/*.py" |
| 44 | +) |
| 45 | + |
| 46 | +# Create Python unittest executable with test data files |
| 47 | +# Use TYPE "dir" to create a directory-based executable instead of zipapp. |
| 48 | +# This allows tests to access data files via Path(__file__).parent, which |
| 49 | +# doesn't work inside zip archives. |
| 50 | +# |
| 51 | +# Note: Only include .py files in SOURCES. Non-Python data files would be |
| 52 | +# treated as Python modules during test discovery, causing import errors. |
| 53 | +# Data files are copied via add_custom_command below after the build |
| 54 | +# generates the directory structure. |
| 55 | +add_fb_python_unittest( |
| 56 | + distro_cli_tests |
| 57 | + BASE_DIR "fboss-image" |
| 58 | + TYPE "dir" |
| 59 | + SOURCES |
| 60 | + ${DISTRO_CLI_TEST_SOURCES} |
| 61 | + ${DISTRO_CLI_TEST_HELPERS} |
| 62 | + ${DISTRO_CLI_LIB_SOURCES} |
| 63 | + ENV |
| 64 | + "PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}/fboss-image" |
| 65 | +) |
| 66 | + |
| 67 | +# Copy test data files AFTER the build generates the directory structure |
| 68 | +# Tests access these via Path(__file__).parent / "data" / "filename" |
| 69 | +# With TYPE "dir", the executable is created at distro_cli_tests/ so data |
| 70 | +# files need to be inside that directory structure. |
| 71 | +# |
| 72 | +# We use add_custom_command to copy AFTER the .GEN_PY_EXE target runs, |
| 73 | +# because that target creates/recreates the distro_cli_tests/ directory. |
| 74 | +set(DATA_DEST_DIR "${CMAKE_CURRENT_BINARY_DIR}/distro_cli_tests/distro_cli/tests") |
| 75 | +set(DATA_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/fboss-image/distro_cli/tests/data") |
| 76 | + |
| 77 | +add_custom_command( |
| 78 | + TARGET distro_cli_tests.GEN_PY_EXE |
| 79 | + POST_BUILD |
| 80 | + COMMAND ${CMAKE_COMMAND} -E copy_directory |
| 81 | + "${DATA_SOURCE_DIR}" |
| 82 | + "${DATA_DEST_DIR}/data" |
| 83 | + COMMENT "Copying test data files for distro_cli_tests" |
| 84 | +) |
| 85 | + |
| 86 | +install_fb_python_executable(distro_cli_tests) |
| 87 | + |
| 88 | +# Restore the original Python3_EXECUTABLE if it was set |
| 89 | +if(DEFINED SAVED_Python3_EXECUTABLE) |
| 90 | + set(Python3_EXECUTABLE "${SAVED_Python3_EXECUTABLE}") |
| 91 | + unset(SAVED_Python3_EXECUTABLE) |
| 92 | +endif() |
0 commit comments