From 6e00c37de7296099a74b383799e9be3d407c2d33 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 4 Sep 2023 23:11:29 -0400 Subject: [PATCH] ci(tests): add test framework --- .gitmodules | 4 + cmake/compile_definitions/common.cmake | 111 +++++++++++++----------- cmake/compile_definitions/linux.cmake | 50 +++++------ cmake/compile_definitions/macos.cmake | 30 +++---- cmake/compile_definitions/windows.cmake | 36 ++++---- cmake/dependencies/common.cmake | 14 ++- cmake/prep/options.cmake | 2 + tests/CMakeLists.txt | 24 +++++ tests/main.cpp | 7 ++ tests/test_main.cpp | 19 ++++ third-party/googletest | 1 + tools/CMakeLists.txt | 1 - 12 files changed, 183 insertions(+), 116 deletions(-) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/main.cpp create mode 100644 tests/test_main.cpp create mode 160000 third-party/googletest diff --git a/.gitmodules b/.gitmodules index dc00523bac2..1a64c1fab3a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -58,3 +58,7 @@ path = third-party/ffmpeg-linux-powerpc64le url = https://github.com/LizardByte/build-deps branch = ffmpeg-linux-powerpc64le +[submodule "third-party/googletest"] + path = third-party/googletest + url = https://github.com/google/googletest/ + branch = main diff --git a/cmake/compile_definitions/common.cmake b/cmake/compile_definitions/common.cmake index f10b2006cf6..f716d4ce828 100644 --- a/cmake/compile_definitions/common.cmake +++ b/cmake/compile_definitions/common.cmake @@ -23,65 +23,70 @@ elseif(UNIX) endif() endif() -include_directories(SYSTEM third-party/nv-codec-headers/include) +# tests +if(BUILD_TESTS) + add_subdirectory(tests) +endif() + +include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/third-party/nv-codec-headers/include) file(GLOB NVENC_SOURCES CONFIGURE_DEPENDS "src/nvenc/*.cpp" "src/nvenc/*.h") list(APPEND PLATFORM_TARGET_FILES ${NVENC_SOURCES}) -configure_file(src/version.h.in version.h @ONLY) +configure_file(${CMAKE_SOURCE_DIR}/src/version.h.in version.h @ONLY) include_directories(${CMAKE_CURRENT_BINARY_DIR}) set(SUNSHINE_TARGET_FILES - third-party/nanors/rs.c - third-party/nanors/rs.h - third-party/moonlight-common-c/src/Input.h - third-party/moonlight-common-c/src/Rtsp.h - third-party/moonlight-common-c/src/RtspParser.c - third-party/moonlight-common-c/src/Video.h - third-party/tray/tray.h - src/upnp.cpp - src/upnp.h - src/cbs.cpp - src/utility.h - src/uuid.h - src/config.h - src/config.cpp - src/main.cpp - src/main.h - src/crypto.cpp - src/crypto.h - src/nvhttp.cpp - src/nvhttp.h - src/httpcommon.cpp - src/httpcommon.h - src/confighttp.cpp - src/confighttp.h - src/rtsp.cpp - src/rtsp.h - src/stream.cpp - src/stream.h - src/video.cpp - src/video.h - src/video_colorspace.cpp - src/video_colorspace.h - src/input.cpp - src/input.h - src/audio.cpp - src/audio.h - src/platform/common.h - src/process.cpp - src/process.h - src/network.cpp - src/network.h - src/move_by_copy.h - src/system_tray.cpp - src/system_tray.h - src/task_pool.h - src/thread_pool.h - src/thread_safe.h - src/sync.h - src/round_robin.h - src/stat_trackers.h - src/stat_trackers.cpp + ${CMAKE_SOURCE_DIR}/third-party/nanors/rs.c + ${CMAKE_SOURCE_DIR}/third-party/nanors/rs.h + ${CMAKE_SOURCE_DIR}/third-party/moonlight-common-c/src/Input.h + ${CMAKE_SOURCE_DIR}/third-party/moonlight-common-c/src/Rtsp.h + ${CMAKE_SOURCE_DIR}/third-party/moonlight-common-c/src/RtspParser.c + ${CMAKE_SOURCE_DIR}/third-party/moonlight-common-c/src/Video.h + ${CMAKE_SOURCE_DIR}/third-party/tray/tray.h + ${CMAKE_SOURCE_DIR}/src/upnp.cpp + ${CMAKE_SOURCE_DIR}/src/upnp.h + ${CMAKE_SOURCE_DIR}/src/cbs.cpp + ${CMAKE_SOURCE_DIR}/src/utility.h + ${CMAKE_SOURCE_DIR}/src/uuid.h + ${CMAKE_SOURCE_DIR}/src/config.h + ${CMAKE_SOURCE_DIR}/src/config.cpp + ${CMAKE_SOURCE_DIR}/src/main.cpp + ${CMAKE_SOURCE_DIR}/src/main.h + ${CMAKE_SOURCE_DIR}/src/crypto.cpp + ${CMAKE_SOURCE_DIR}/src/crypto.h + ${CMAKE_SOURCE_DIR}/src/nvhttp.cpp + ${CMAKE_SOURCE_DIR}/src/nvhttp.h + ${CMAKE_SOURCE_DIR}/src/httpcommon.cpp + ${CMAKE_SOURCE_DIR}/src/httpcommon.h + ${CMAKE_SOURCE_DIR}/src/confighttp.cpp + ${CMAKE_SOURCE_DIR}/src/confighttp.h + ${CMAKE_SOURCE_DIR}/src/rtsp.cpp + ${CMAKE_SOURCE_DIR}/src/rtsp.h + ${CMAKE_SOURCE_DIR}/src/stream.cpp + ${CMAKE_SOURCE_DIR}/src/stream.h + ${CMAKE_SOURCE_DIR}/src/video.cpp + ${CMAKE_SOURCE_DIR}/src/video.h + ${CMAKE_SOURCE_DIR}/src/video_colorspace.cpp + ${CMAKE_SOURCE_DIR}/src/video_colorspace.h + ${CMAKE_SOURCE_DIR}/src/input.cpp + ${CMAKE_SOURCE_DIR}/src/input.h + ${CMAKE_SOURCE_DIR}/src/audio.cpp + ${CMAKE_SOURCE_DIR}/src/audio.h + ${CMAKE_SOURCE_DIR}/src/platform/common.h + ${CMAKE_SOURCE_DIR}/src/process.cpp + ${CMAKE_SOURCE_DIR}/src/process.h + ${CMAKE_SOURCE_DIR}/src/network.cpp + ${CMAKE_SOURCE_DIR}/src/network.h + ${CMAKE_SOURCE_DIR}/src/move_by_copy.h + ${CMAKE_SOURCE_DIR}/src/system_tray.cpp + ${CMAKE_SOURCE_DIR}/src/system_tray.h + ${CMAKE_SOURCE_DIR}/src/task_pool.h + ${CMAKE_SOURCE_DIR}/src/thread_pool.h + ${CMAKE_SOURCE_DIR}/src/thread_safe.h + ${CMAKE_SOURCE_DIR}/src/sync.h + ${CMAKE_SOURCE_DIR}/src/round_robin.h + ${CMAKE_SOURCE_DIR}/src/stat_trackers.h + ${CMAKE_SOURCE_DIR}/src/stat_trackers.cpp ${PLATFORM_TARGET_FILES}) set_source_files_properties(src/upnp.cpp PROPERTIES COMPILE_FLAGS -Wno-pedantic) diff --git a/cmake/compile_definitions/linux.cmake b/cmake/compile_definitions/linux.cmake index 4663e1d0a57..0ccc7d930b9 100644 --- a/cmake/compile_definitions/linux.cmake +++ b/cmake/compile_definitions/linux.cmake @@ -85,9 +85,9 @@ endif() if(CUDA_FOUND) include_directories(SYSTEM third-party/nvfbc) list(APPEND PLATFORM_TARGET_FILES - src/platform/linux/cuda.cu - src/platform/linux/cuda.cpp - third-party/nvfbc/NvFBC.h) + ${CMAKE_SOURCE_DIR}/src/platform/linux/cuda.cu + ${CMAKE_SOURCE_DIR}/src/platform/linux/cuda.cpp + ${CMAKE_SOURCE_DIR}/third-party/nvfbc/NvFBC.h) add_compile_definitions(SUNSHINE_BUILD_CUDA) endif() @@ -104,7 +104,7 @@ if(LIBDRM_FOUND AND LIBCAP_FOUND) add_compile_definitions(SUNSHINE_BUILD_DRM) include_directories(SYSTEM ${LIBDRM_INCLUDE_DIRS} ${LIBCAP_INCLUDE_DIRS}) list(APPEND PLATFORM_LIBRARIES ${LIBDRM_LIBRARIES} ${LIBCAP_LIBRARIES}) - list(APPEND PLATFORM_TARGET_FILES src/platform/linux/kmsgrab.cpp) + list(APPEND PLATFORM_TARGET_FILES ${CMAKE_SOURCE_DIR}/src/platform/linux/kmsgrab.cpp) list(APPEND SUNSHINE_DEFINITIONS EGL_NO_X11=1) elseif(NOT LIBDRM_FOUND) message(WARNING "Missing libdrm") @@ -132,8 +132,8 @@ if(WAYLAND_FOUND) list(APPEND PLATFORM_LIBRARIES ${WAYLAND_LIBRARIES}) list(APPEND PLATFORM_TARGET_FILES - src/platform/linux/wlgrab.cpp - src/platform/linux/wayland.cpp) + ${CMAKE_SOURCE_DIR}/src/platform/linux/wlgrab.cpp + ${CMAKE_SOURCE_DIR}/src/platform/linux/wayland.cpp) endif() # x11 @@ -146,7 +146,7 @@ if(X11_FOUND) add_compile_definitions(SUNSHINE_BUILD_X11) include_directories(SYSTEM ${X11_INCLUDE_DIR}) list(APPEND PLATFORM_LIBRARIES ${X11_LIBRARIES}) - list(APPEND PLATFORM_TARGET_FILES src/platform/linux/x11grab.cpp) + list(APPEND PLATFORM_TARGET_FILES ${CMAKE_SOURCE_DIR}/src/platform/linux/x11grab.cpp) endif() if(NOT ${CUDA_FOUND} AND NOT ${WAYLAND_FOUND} AND NOT ${X11_FOUND} AND NOT (${LIBDRM_FOUND} AND ${LIBCAP_FOUND})) @@ -171,24 +171,24 @@ else() endif() list(APPEND PLATFORM_TARGET_FILES - src/platform/linux/publish.cpp - src/platform/linux/vaapi.h - src/platform/linux/vaapi.cpp - src/platform/linux/cuda.h - src/platform/linux/graphics.h - src/platform/linux/graphics.cpp - src/platform/linux/misc.h - src/platform/linux/misc.cpp - src/platform/linux/audio.cpp - src/platform/linux/input.cpp - src/platform/linux/x11grab.h - src/platform/linux/wayland.h - third-party/glad/src/egl.c - third-party/glad/src/gl.c - third-party/glad/include/EGL/eglplatform.h - third-party/glad/include/KHR/khrplatform.h - third-party/glad/include/glad/gl.h - third-party/glad/include/glad/egl.h) + ${CMAKE_SOURCE_DIR}/src/platform/linux/publish.cpp + ${CMAKE_SOURCE_DIR}/src/platform/linux/vaapi.h + ${CMAKE_SOURCE_DIR}/src/platform/linux/vaapi.cpp + ${CMAKE_SOURCE_DIR}/src/platform/linux/cuda.h + ${CMAKE_SOURCE_DIR}/src/platform/linux/graphics.h + ${CMAKE_SOURCE_DIR}/src/platform/linux/graphics.cpp + ${CMAKE_SOURCE_DIR}/src/platform/linux/misc.h + ${CMAKE_SOURCE_DIR}/src/platform/linux/misc.cpp + ${CMAKE_SOURCE_DIR}/src/platform/linux/audio.cpp + ${CMAKE_SOURCE_DIR}/src/platform/linux/input.cpp + ${CMAKE_SOURCE_DIR}/src/platform/linux/x11grab.h + ${CMAKE_SOURCE_DIR}/src/platform/linux/wayland.h + ${CMAKE_SOURCE_DIR}/third-party/glad/src/egl.c + ${CMAKE_SOURCE_DIR}/third-party/glad/src/gl.c + ${CMAKE_SOURCE_DIR}/third-party/glad/include/EGL/eglplatform.h + ${CMAKE_SOURCE_DIR}/third-party/glad/include/KHR/khrplatform.h + ${CMAKE_SOURCE_DIR}/third-party/glad/include/glad/gl.h + ${CMAKE_SOURCE_DIR}/third-party/glad/include/glad/egl.h) list(APPEND PLATFORM_LIBRARIES Boost::dynamic_linking diff --git a/cmake/compile_definitions/macos.cmake b/cmake/compile_definitions/macos.cmake index 2e3b73f1c64..9ba9a91dd40 100644 --- a/cmake/compile_definitions/macos.cmake +++ b/cmake/compile_definitions/macos.cmake @@ -23,21 +23,21 @@ set(APPLE_PLIST_FILE ${SUNSHINE_SOURCE_ASSETS_DIR}/macos/assets/Info.plist) set(SUNSHINE_TRAY 0) set(PLATFORM_TARGET_FILES - src/platform/macos/av_audio.h - src/platform/macos/av_audio.m - src/platform/macos/av_img_t.h - src/platform/macos/av_video.h - src/platform/macos/av_video.m - src/platform/macos/display.mm - src/platform/macos/input.cpp - src/platform/macos/microphone.mm - src/platform/macos/misc.mm - src/platform/macos/misc.h - src/platform/macos/nv12_zero_device.cpp - src/platform/macos/nv12_zero_device.h - src/platform/macos/publish.cpp - third-party/TPCircularBuffer/TPCircularBuffer.c - third-party/TPCircularBuffer/TPCircularBuffer.h + ${CMAKE_SOURCE_DIR}/src/platform/macos/av_audio.h + ${CMAKE_SOURCE_DIR}/src/platform/macos/av_audio.m + ${CMAKE_SOURCE_DIR}/src/platform/macos/av_img_t.h + ${CMAKE_SOURCE_DIR}/src/platform/macos/av_video.h + ${CMAKE_SOURCE_DIR}/src/platform/macos/av_video.m + ${CMAKE_SOURCE_DIR}/src/platform/macos/display.mm + ${CMAKE_SOURCE_DIR}/src/platform/macos/input.cpp + ${CMAKE_SOURCE_DIR}/src/platform/macos/microphone.mm + ${CMAKE_SOURCE_DIR}/src/platform/macos/misc.mm + ${CMAKE_SOURCE_DIR}/src/platform/macos/misc.h + ${CMAKE_SOURCE_DIR}/src/platform/macos/nv12_zero_device.cpp + ${CMAKE_SOURCE_DIR}/src/platform/macos/nv12_zero_device.h + ${CMAKE_SOURCE_DIR}/src/platform/macos/publish.cpp + ${CMAKE_SOURCE_DIR}/third-party/TPCircularBuffer/TPCircularBuffer.c + ${CMAKE_SOURCE_DIR}/third-party/TPCircularBuffer/TPCircularBuffer.h ${APPLE_PLIST_FILE}) if(SUNSHINE_ENABLE_TRAY) diff --git a/cmake/compile_definitions/windows.cmake b/cmake/compile_definitions/windows.cmake index ec3e076f4de..da2d922fed3 100644 --- a/cmake/compile_definitions/windows.cmake +++ b/cmake/compile_definitions/windows.cmake @@ -22,10 +22,10 @@ file(GLOB NVPREFS_FILES CONFIGURE_DEPENDS "src/platform/windows/nvprefs/*.h") # vigem -include_directories(SYSTEM third-party/ViGEmClient/include) -set_source_files_properties(third-party/ViGEmClient/src/ViGEmClient.cpp +include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/third-party/ViGEmClient/include) +set_source_files_properties(${CMAKE_SOURCE_DIR}/third-party/ViGEmClient/src/ViGEmClient.cpp PROPERTIES COMPILE_DEFINITIONS "UNICODE=1;ERROR_INVALID_DEVICE_OBJECT_PARAMETER=650") -set_source_files_properties(third-party/ViGEmClient/src/ViGEmClient.cpp +set_source_files_properties(${CMAKE_SOURCE_DIR}/third-party/ViGEmClient/src/ViGEmClient.cpp PROPERTIES COMPILE_FLAGS "-Wno-unknown-pragmas -Wno-misleading-indentation -Wno-class-memaccess") # sunshine icon @@ -33,24 +33,24 @@ if(NOT DEFINED SUNSHINE_ICON_PATH) set(SUNSHINE_ICON_PATH "${CMAKE_CURRENT_SOURCE_DIR}/sunshine.ico") endif() -configure_file(src/platform/windows/windows.rs.in windows.rc @ONLY) +configure_file(${CMAKE_SOURCE_DIR}/src/platform/windows/windows.rs.in windows.rc @ONLY) set(PLATFORM_TARGET_FILES "${CMAKE_CURRENT_BINARY_DIR}/windows.rc" - src/platform/windows/publish.cpp - src/platform/windows/misc.h - src/platform/windows/misc.cpp - src/platform/windows/input.cpp - src/platform/windows/display.h - src/platform/windows/display_base.cpp - src/platform/windows/display_vram.cpp - src/platform/windows/display_ram.cpp - src/platform/windows/audio.cpp - third-party/ViGEmClient/src/ViGEmClient.cpp - third-party/ViGEmClient/include/ViGEm/Client.h - third-party/ViGEmClient/include/ViGEm/Common.h - third-party/ViGEmClient/include/ViGEm/Util.h - third-party/ViGEmClient/include/ViGEm/km/BusShared.h + ${CMAKE_SOURCE_DIR}/src/platform/windows/publish.cpp + ${CMAKE_SOURCE_DIR}/src/platform/windows/misc.h + ${CMAKE_SOURCE_DIR}/src/platform/windows/misc.cpp + ${CMAKE_SOURCE_DIR}/src/platform/windows/input.cpp + ${CMAKE_SOURCE_DIR}/src/platform/windows/display.h + ${CMAKE_SOURCE_DIR}/src/platform/windows/display_base.cpp + ${CMAKE_SOURCE_DIR}/src/platform/windows/display_vram.cpp + ${CMAKE_SOURCE_DIR}/src/platform/windows/display_ram.cpp + ${CMAKE_SOURCE_DIR}/src/platform/windows/audio.cpp + ${CMAKE_SOURCE_DIR}/third-party/ViGEmClient/src/ViGEmClient.cpp + ${CMAKE_SOURCE_DIR}/third-party/ViGEmClient/include/ViGEm/Client.h + ${CMAKE_SOURCE_DIR}/third-party/ViGEmClient/include/ViGEm/Common.h + ${CMAKE_SOURCE_DIR}/third-party/ViGEmClient/include/ViGEm/Util.h + ${CMAKE_SOURCE_DIR}/third-party/ViGEmClient/include/ViGEm/km/BusShared.h ${NVPREFS_FILES}) set(OPENSSL_LIBRARIES diff --git a/cmake/dependencies/common.cmake b/cmake/dependencies/common.cmake index 33372c2e991..23c3fc32159 100644 --- a/cmake/dependencies/common.cmake +++ b/cmake/dependencies/common.cmake @@ -2,19 +2,25 @@ # this file will also load platform specific dependencies # submodules +# testing +if(BUILD_TESTS) + add_subdirectory(${CMAKE_SOURCE_DIR}/third-party/googletest) + include_directories(${CMAKE_SOURCE_DIR}/third-party/googletest/include) +endif() + # moonlight common library -add_subdirectory(third-party/moonlight-common-c/enet) +add_subdirectory(${CMAKE_SOURCE_DIR}/third-party/moonlight-common-c/enet) # web server -add_subdirectory(third-party/Simple-Web-Server) +add_subdirectory(${CMAKE_SOURCE_DIR}/third-party/Simple-Web-Server) # miniupnp set(UPNPC_BUILD_SHARED OFF CACHE BOOL "No shared libraries") set(UPNPC_BUILD_TESTS OFF CACHE BOOL "Don't build tests for miniupnpc") set(UPNPC_BUILD_SAMPLE OFF CACHE BOOL "Don't build samples for miniupnpc") set(UPNPC_NO_INSTALL ON CACHE BOOL "Don't install any libraries build for miniupnpc") -add_subdirectory(third-party/miniupnp/miniupnpc) -include_directories(SYSTEM third-party/miniupnp/miniupnpc/include) +add_subdirectory(${CMAKE_SOURCE_DIR}/third-party/miniupnp/miniupnpc) +include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/third-party/miniupnp/miniupnpc/include) # ffmpeg pre-compiled binaries if(WIN32) diff --git a/cmake/prep/options.cmake b/cmake/prep/options.cmake index 4cb2ef117b6..90cae3b9f61 100644 --- a/cmake/prep/options.cmake +++ b/cmake/prep/options.cmake @@ -1,3 +1,5 @@ +option(BUILD_TESTS "Build tests" ON) + # if this option is set, the build will exit after configuring special package configuration files option(SUNSHINE_CONFIGURE_ONLY "Configure special files only, then exit." OFF) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000000..0cb0f4566a3 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,24 @@ +include_directories(SYSTEM ${CMAKE_SOURCE_DIR}) + +enable_testing() + +set(INSTALL_GTEST OFF) + +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # cmake-lint: disable=C0103 + +set(TEST_SOURCES + ${CMAKE_SOURCE_DIR}/tests/main.cpp + ${CMAKE_SOURCE_DIR}/tests/test_main.cpp +) + +add_executable(sunshine_test ${TEST_SOURCES}) +target_link_libraries(sunshine_test + ${SUNSHINE_TARGET_FILES} + ${SUNSHINE_EXTERNAL_LIBRARIES} + ${EXTRA_LIBS} + gtest +) +set_target_properties(sunshine_test PROPERTIES CXX_STANDARD 17) +target_compile_options(sunshine_test PRIVATE ${SUNSHINE_COMPILE_OPTIONS}) +add_test(NAME sunshine_test COMMAND sunshine_test) diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 00000000000..b4aac0e351e --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,7 @@ +#include "gtest/gtest.h" + +int +main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tests/test_main.cpp b/tests/test_main.cpp new file mode 100644 index 00000000000..3a315098962 --- /dev/null +++ b/tests/test_main.cpp @@ -0,0 +1,19 @@ +/** + * @file tests/test_main.cpp + * @brief Test src/main.*. + */ + +#include +#include + +TEST(MainTestSuite, WriteFileTest) { + EXPECT_EQ(write_file("write_file_test.txt", "test"), 0); +} + +TEST(MainTestSuite, ReadFileTest) { + // read file from WriteFileTest + EXPECT_EQ(read_file("write_file_test.txt"), "test"); + + // read missing file + EXPECT_EQ(read_file("non-existing-file.txt"), ""); +} diff --git a/third-party/googletest b/third-party/googletest new file mode 160000 index 00000000000..f8d7d77c069 --- /dev/null +++ b/third-party/googletest @@ -0,0 +1 @@ +Subproject commit f8d7d77c06936315286eb55f8de22cd23c188571 diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index e24be6e102c..c2121a7dbea 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -36,4 +36,3 @@ target_link_libraries(ddprobe d3d11 ${PLATFORM_LIBRARIES}) target_compile_options(ddprobe PRIVATE ${SUNSHINE_COMPILE_OPTIONS}) -