Tests: initial test in initializing lib and loading a backend

- The default backend test will generally fail because
   the backend isn't installed
 - Test known backends if they are built, by passing full path
This commit is contained in:
Adriaan de Groot 2017-10-02 15:40:41 +02:00
parent 8fd723770a
commit f1dee86084
5 changed files with 119 additions and 0 deletions

View File

@ -108,3 +108,6 @@ set_target_properties( kpmcore
message(STATUS "kpmcore ${VERSION} will be built for install into ${CMAKE_INSTALL_PREFIX}")
feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES)
enable_testing()
add_subdirectory(test)

35
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,35 @@
# Tests for KPMcore
#
# These are not so much "tests" as "small example programs". They illustrate
# how to use the library, how to perform common tasks.
set(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
include_directories(${CMAKE_SOURCE_DIR}/src) # To get at KPMcore headers
add_compile_options(-fPIC)
###
#
# Helper macro to link to the helper (for initialization of kpmcore)
# and to add a test with the given name.
#
add_library(testhelpers OBJECT helpers.cpp)
macro (kpm_test name)
add_executable(${name} ${ARGN} $<TARGET_OBJECTS:testhelpers>)
target_link_libraries(${name} kpmcore)
add_test(${name} ${name})
endmacro()
###
#
# Tests of initialization: try explicitly loading some backends
kpm_test(testinit testinit.cpp) # Default backend
if(TARGET pmdummybackendplugin)
add_test(NAME testinit-dummy COMMAND testinit $<TARGET_FILE:pmdummybackendplugin>)
endif()
if(TARGET pmlibpartedbackendplugin)
add_test(NAME testinit-parted COMMAND testinit $<TARGET_FILE:pmlibpartedbackendplugin>)
endif()

47
test/helpers.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "helpers.h"
#include "backend/corebackendmanager.h"
#include <QDebug>
#include <QString>
static bool s_KPMcoreInited = false;
KPMCoreInitializer::KPMCoreInitializer() :
m_isValid( s_KPMcoreInited )
{
if ( !s_KPMcoreInited )
{
QByteArray env = qgetenv( "KPMCORE_BACKEND" );
auto backendName = env.isEmpty() ? CoreBackendManager::defaultBackendName() : QString::fromLatin1(env);
if ( !CoreBackendManager::self()->load( backendName ) )
{
qWarning() << "Failed to load backend plugin" << backendName;
}
else
{
m_isValid = s_KPMcoreInited = true;
}
}
}
KPMCoreInitializer::KPMCoreInitializer(const QString& backendName) :
m_isValid( s_KPMcoreInited )
{
if (!s_KPMcoreInited)
{
if ( !CoreBackendManager::self()->load( backendName ) )
{
qWarning() << "Failed to load backend plugin" << backendName;
}
else
{
m_isValid = s_KPMcoreInited = true;
}
}
}
KPMCoreInitializer::KPMCoreInitializer(const char *backend) : KPMCoreInitializer( QString::fromLatin1(backend) )
{
}

18
test/helpers.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef TEST_KPMHELPERS_H
#define TEST_KPMHELPERS_H
class QString;
class KPMCoreInitializer
{
public:
KPMCoreInitializer();
KPMCoreInitializer(const QString& backend);
KPMCoreInitializer(const char *backend);
bool isValid() const { return m_isValid; }
private:
bool m_isValid;
} ;
#endif

16
test/testinit.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "helpers.h"
int main(int argc, char **argv)
{
if (argc != 2)
{
KPMCoreInitializer i;
return i.isValid() ? 0 : 1;
}
else
{
KPMCoreInitializer i(argv[1]);
return i.isValid() ? 0 : 1;
}
}