From 719548213f00a504f383ca67f8cc5f051586f3f3 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 27 Sep 2021 17:52:02 +0200 Subject: [PATCH] CMake: if pylint is available, run it as part of the test-suite This introduces a stub-implementation (fake) that mimics the API offered by libcalamares (the library is actually exposed to Python via Boost::Python, so it doesn't act like a C-extension). Using that stub-implementation, we can check Python modules for validity as part of the test-suite. The stub-implementation is needed, because otherwise every Python module already fails at `import libcalamares`. - stub-implement the API that is actually used by the Python modules - in globalstorage, be slightly smart about what keys are being requested (so that e.g. all the modules that handle partitions information get an empty list and can manipulate that, instead of erroring out when they get a string) --- .../CalamaresAddModuleSubdirectory.cmake | 25 +++++++++++++++++++ ci/libcalamares/__init__.py | 2 +- ci/libcalamares/globalstorage.py | 17 ++++++++++++- ci/libcalamares/utils.py | 8 ++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/CMakeModules/CalamaresAddModuleSubdirectory.cmake b/CMakeModules/CalamaresAddModuleSubdirectory.cmake index 1f1c02300..91524a09e 100644 --- a/CMakeModules/CalamaresAddModuleSubdirectory.cmake +++ b/CMakeModules/CalamaresAddModuleSubdirectory.cmake @@ -42,6 +42,18 @@ include( CalamaresCheckModuleSelection ) set( MODULE_DATA_DESTINATION share/calamares/modules ) +# We look for Pylint (just once) so that unittests can be added that +# check the syntax / variables of Python modules. This should help +# avoid more typo's-in-releases. +if(BUILD_TESTING AND NOT PYLINT_COMMAND_SEARCHED) + set(PYLINT_COMMAND_SEARCHED TRUE) + find_program( + PYLINT_COMMAND + NAMES pylint3 pylint + PATHS $ENV{HOME}/.local/bin + ) +endif() + function( _calamares_add_module_subdirectory_impl ) set( SUBDIRECTORY ${ARGV0} ) @@ -241,6 +253,19 @@ function( _calamares_add_module_subdirectory_impl ) if ( EXISTS ${_testdir}/CMakeTests.txt AND NOT EXISTS ${_mod_dir}/CMakeLists.txt ) include( ${_testdir}/CMakeTests.txt ) endif() + if ( PYLINT_COMMAND AND MODULE_INTERFACE MATCHES "python" ) + # Python modules get an additional test via pylint; this + # needs to run at top-level because the ci/libcalamares directory + # contains API stubs. + # + # TODO: the entry point is assumed to be `main.py`, but that is + # configurable through module.desc + add_test( + NAME lint-${SUBDIRECTORY} + COMMAND env PYTHONPATH=ci: ${PYLINT_COMMAND} -E ${_mod_dir}/main.py + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + ) + endif() endif() endfunction() diff --git a/ci/libcalamares/__init__.py b/ci/libcalamares/__init__.py index 11bf0ffe2..1f7a09430 100644 --- a/ci/libcalamares/__init__.py +++ b/ci/libcalamares/__init__.py @@ -6,4 +6,4 @@ # Boost::Python, not as a bare C-extension) so that # pylint doesn't complain about libcalamares internals. -pass +VERSION_SHORT="1.0" diff --git a/ci/libcalamares/globalstorage.py b/ci/libcalamares/globalstorage.py index 3c2acb870..d40a28204 100644 --- a/ci/libcalamares/globalstorage.py +++ b/ci/libcalamares/globalstorage.py @@ -6,4 +6,19 @@ # Boost::Python, not as a bare C-extension) so that # pylint doesn't complain about libcalamares internals. -def value(_): return 1 +def count(): return 1 + +def keys(): return [] + +def contains(_): return True + +def value(key): + if key in ("branding",): + return dict() + if key in ("partitions",): + return list() + return "" + +def insert(key, value): pass + +def remove(_): pass diff --git a/ci/libcalamares/utils.py b/ci/libcalamares/utils.py index 1d423893b..706e4a95a 100644 --- a/ci/libcalamares/utils.py +++ b/ci/libcalamares/utils.py @@ -10,6 +10,14 @@ def debug(_): pass def warning(_): pass +def error(_): pass + def gettext_path(): pass def gettext_languages(): pass + +def target_env_call(_): return 0 + +def check_target_env_call(_): pass + +def mount(device, mountpoint, fstype, options): return 0