CuraTests.cmake 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #Add test for import statements which are not compatible with all builds
  49. add_test(
  50. NAME "invalid-imports"
  51. COMMAND ${Python3_EXECUTABLE} scripts/check_invalid_imports.py
  52. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  53. )
  54. cura_add_test(NAME pytest-main DIRECTORY ${CMAKE_SOURCE_DIR}/tests PYTHONPATH "${CMAKE_SOURCE_DIR}|${URANIUM_DIR}")
  55. file(GLOB_RECURSE _plugins plugins/*/__init__.py)
  56. foreach(_plugin ${_plugins})
  57. get_filename_component(_plugin_directory ${_plugin} DIRECTORY)
  58. if(EXISTS ${_plugin_directory}/tests)
  59. get_filename_component(_plugin_name ${_plugin_directory} NAME)
  60. cura_add_test(NAME pytest-${_plugin_name} DIRECTORY ${_plugin_directory} PYTHONPATH "${_plugin_directory}|${CMAKE_SOURCE_DIR}|${URANIUM_DIR}")
  61. endif()
  62. endforeach()
  63. #Add code style test.
  64. add_test(
  65. NAME "code-style"
  66. COMMAND ${Python3_EXECUTABLE} run_mypy.py
  67. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  68. )
  69. #Add test for whether the shortcut alt-keys are unique in every translation.
  70. add_test(
  71. NAME "shortcut-keys"
  72. COMMAND ${Python3_EXECUTABLE} scripts/check_shortcut_keys.py
  73. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  74. )