CuraTests.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. include(CTest)
  4. include(CMakeParseArguments)
  5. # FIXME: Remove the code for CMake <3.12 once we have switched over completely.
  6. # FindPython3 is a new module since CMake 3.12. It deprecates FindPythonInterp and FindPythonLibs. The FindPython3
  7. # module is copied from the CMake repository here so in CMake <3.12 we can still use it.
  8. if(${CMAKE_VERSION} VERSION_LESS 3.12)
  9. # Use FindPythonInterp and FindPythonLibs for CMake <3.12
  10. find_package(PythonInterp 3 REQUIRED)
  11. set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE})
  12. else()
  13. # Use FindPython3 for CMake >=3.12
  14. find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
  15. endif()
  16. add_custom_target(test-verbose COMMAND ${CMAKE_CTEST_COMMAND} --verbose)
  17. function(cura_add_test)
  18. set(_single_args NAME DIRECTORY PYTHONPATH)
  19. cmake_parse_arguments("" "" "${_single_args}" "" ${ARGN})
  20. if(NOT _NAME)
  21. message(FATAL_ERROR "cura_add_test requires a test name argument")
  22. endif()
  23. if(NOT _DIRECTORY)
  24. message(FATAL_ERROR "cura_add_test requires a directory to test")
  25. endif()
  26. if(NOT _PYTHONPATH)
  27. set(_PYTHONPATH ${_DIRECTORY})
  28. endif()
  29. if(WIN32)
  30. string(REPLACE "|" "\\;" _PYTHONPATH ${_PYTHONPATH})
  31. set(_PYTHONPATH "${_PYTHONPATH}\\;$ENV{PYTHONPATH}")
  32. else()
  33. string(REPLACE "|" ":" _PYTHONPATH ${_PYTHONPATH})
  34. set(_PYTHONPATH "${_PYTHONPATH}:$ENV{PYTHONPATH}")
  35. endif()
  36. get_test_property(${_NAME} ENVIRONMENT test_exists) #Find out if the test exists by getting a property from it that always exists (such as ENVIRONMENT because we set that ourselves).
  37. if (NOT ${test_exists})
  38. add_test(
  39. NAME ${_NAME}
  40. COMMAND ${Python3_EXECUTABLE} -m pytest --junitxml=${CMAKE_BINARY_DIR}/junit-${_NAME}.xml ${_DIRECTORY}
  41. )
  42. set_tests_properties(${_NAME} PROPERTIES ENVIRONMENT LANG=C)
  43. set_tests_properties(${_NAME} PROPERTIES ENVIRONMENT "PYTHONPATH=${_PYTHONPATH}")
  44. else()
  45. message(WARNING "Duplicate test ${_NAME}!")
  46. endif()
  47. endfunction()
  48. cura_add_test(NAME pytest-main DIRECTORY ${CMAKE_SOURCE_DIR}/tests PYTHONPATH "${CMAKE_SOURCE_DIR}|${URANIUM_DIR}")
  49. file(GLOB_RECURSE _plugins plugins/*/__init__.py)
  50. foreach(_plugin ${_plugins})
  51. get_filename_component(_plugin_directory ${_plugin} DIRECTORY)
  52. if(EXISTS ${_plugin_directory}/tests)
  53. get_filename_component(_plugin_name ${_plugin_directory} NAME)
  54. cura_add_test(NAME pytest-${_plugin_name} DIRECTORY ${_plugin_directory} PYTHONPATH "${_plugin_directory}|${CMAKE_SOURCE_DIR}|${URANIUM_DIR}")
  55. endif()
  56. endforeach()
  57. #Add code style test.
  58. add_test(
  59. NAME "code-style"
  60. COMMAND ${Python3_EXECUTABLE} run_mypy.py
  61. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  62. )
  63. #Add test for whether the shortcut alt-keys are unique in every translation.
  64. add_test(
  65. NAME "shortcut-keys"
  66. COMMAND ${Python3_EXECUTABLE} scripts/check_shortcut_keys.py
  67. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  68. )