From f157d9c4590f9b31303fb127d7241b223de030eb Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 11 Aug 2020 11:45:21 +0200 Subject: [PATCH] [libcalamares] Refactor data-loading in Settings - expose, for testing purposes, the load-from-YAML-data part alongside the public constructor that reads a YAML file - add test for building the list of instances --- src/libcalamares/Settings.cpp | 61 ++++++++++++++++++------------ src/libcalamares/Settings.h | 6 +++ src/libcalamares/Tests.cpp | 71 +++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 24 deletions(-) diff --git a/src/libcalamares/Settings.cpp b/src/libcalamares/Settings.cpp index c4e7a3b4c..0a475be00 100644 --- a/src/libcalamares/Settings.cpp +++ b/src/libcalamares/Settings.cpp @@ -213,6 +213,16 @@ interpretSequence( const YAML::Node& node, Settings::ModuleSequence& moduleSeque } } +Settings::Settings( bool debugMode ) + : QObject() + , m_debug( debugMode ) + , m_doChroot( true ) + , m_promptInstall( false ) + , m_disableCancel( false ) + , m_disableCancelDuringExec( false ) +{ +} + Settings::Settings( const QString& settingsFilePath, bool debugMode ) : QObject() , m_debug( debugMode ) @@ -225,30 +235,7 @@ Settings::Settings( const QString& settingsFilePath, bool debugMode ) QFile file( settingsFilePath ); if ( file.exists() && file.open( QFile::ReadOnly | QFile::Text ) ) { - QByteArray ba = file.readAll(); - - try - { - YAML::Node config = YAML::Load( ba.constData() ); - Q_ASSERT( config.IsMap() ); - - interpretModulesSearch( - debugMode, CalamaresUtils::yamlToStringList( config[ "modules-search" ] ), m_modulesSearchPaths ); - interpretInstances( config[ "instances" ], m_customModuleInstances ); - interpretSequence( config[ "sequence" ], m_modulesSequence ); - - m_brandingComponentName = requireString( config, "branding" ); - m_promptInstall = requireBool( config, "prompt-install", false ); - m_doChroot = !requireBool( config, "dont-chroot", false ); - m_isSetupMode = requireBool( config, "oem-setup", !m_doChroot ); - m_disableCancel = requireBool( config, "disable-cancel", false ); - m_disableCancelDuringExec = requireBool( config, "disable-cancel-during-exec", false ); - m_quitAtEnd = requireBool( config, "quit-at-end", false ); - } - catch ( YAML::Exception& e ) - { - CalamaresUtils::explainYamlException( e, ba, file.fileName() ); - } + setConfiguration( file.readAll(), file.fileName() ); } else { @@ -258,6 +245,32 @@ Settings::Settings( const QString& settingsFilePath, bool debugMode ) s_instance = this; } +void +Settings::setConfiguration( const QByteArray& ba, const QString& explainName ) +{ + try + { + YAML::Node config = YAML::Load( ba.constData() ); + Q_ASSERT( config.IsMap() ); + + interpretModulesSearch( + debugMode(), CalamaresUtils::yamlToStringList( config[ "modules-search" ] ), m_modulesSearchPaths ); + interpretInstances( config[ "instances" ], m_customModuleInstances ); + interpretSequence( config[ "sequence" ], m_modulesSequence ); + + m_brandingComponentName = requireString( config, "branding" ); + m_promptInstall = requireBool( config, "prompt-install", false ); + m_doChroot = !requireBool( config, "dont-chroot", false ); + m_isSetupMode = requireBool( config, "oem-setup", !m_doChroot ); + m_disableCancel = requireBool( config, "disable-cancel", false ); + m_disableCancelDuringExec = requireBool( config, "disable-cancel-during-exec", false ); + m_quitAtEnd = requireBool( config, "quit-at-end", false ); + } + catch ( YAML::Exception& e ) + { + CalamaresUtils::explainYamlException( e, ba, explainName ); + } +} QStringList Settings::modulesSearchPaths() const diff --git a/src/libcalamares/Settings.h b/src/libcalamares/Settings.h index 2ad489a15..de12ab57a 100644 --- a/src/libcalamares/Settings.h +++ b/src/libcalamares/Settings.h @@ -77,8 +77,14 @@ private: class DLLEXPORT Settings : public QObject { Q_OBJECT +#ifdef BUILD_AS_TEST +public: +#endif + explicit Settings( bool debugMode ); explicit Settings( const QString& settingsFilePath, bool debugMode ); + void setConfiguration( const QByteArray& configData, const QString& explainName ); + public: static Settings* instance(); /// @brief Find a settings.conf, following @p debugMode diff --git a/src/libcalamares/Tests.cpp b/src/libcalamares/Tests.cpp index a9f53dc56..9fe61e727 100644 --- a/src/libcalamares/Tests.cpp +++ b/src/libcalamares/Tests.cpp @@ -44,6 +44,8 @@ private Q_SLOTS: void testInstanceKey(); void testInstanceDescription(); + + void testSettings(); }; void @@ -374,6 +376,75 @@ TestLibCalamares::testInstanceDescription() } } +void +TestLibCalamares::testSettings() +{ + { + Calamares::Settings s( false ); + QVERIFY( !s.debugMode() ); + } + { + Calamares::Settings s( true ); + QVERIFY( s.debugMode() ); + QVERIFY( s.customModuleInstances().isEmpty() ); + QVERIFY( s.modulesSequence().isEmpty() ); + QVERIFY( s.brandingComponentName().isEmpty() ); + + s.setConfiguration( R"(--- +instances: + - module: welcome + id: hi + weight: 75 + - module: welcome + id: yo + config: yolo.conf +sequence: + - show: + - welcome@hi + - welcome@yo + - dummycpp + - summary + - exec: + - welcome@hi +)", + QStringLiteral( "" ) ); + + QVERIFY( s.debugMode() ); + QCOMPARE( s.customModuleInstances().count(), 2 ); + QCOMPARE( s.modulesSequence().count(), 2 ); // 2 steps (show, exec) + QVERIFY( s.brandingComponentName().isEmpty() ); + + // Make a lambda where we can adjust what it looks for from the outside, + // by capturing a reference. + QString moduleKey = QString( "welcome" ); + auto moduleFinder = [&moduleKey]( const Calamares::InstanceDescription& d ) { + return d.isValid() && d.key().module() == moduleKey; + }; + + const auto it0 = std::find_if( + s.customModuleInstances().constBegin(), s.customModuleInstances().constEnd(), moduleFinder ); + QVERIFY( it0 != s.customModuleInstances().constEnd() ); + + moduleKey = QString( "derp" ); + const auto it1 = std::find_if( + s.customModuleInstances().constBegin(), s.customModuleInstances().constEnd(), moduleFinder ); + QVERIFY( it1 == s.customModuleInstances().constEnd() ); + + int validCount = 0; + int customCount = 0; + for ( const auto& d : s.customModuleInstances() ) + { + if ( d.isValid() ) + validCount++; + if ( d.isCustom() ) + customCount++; + QVERIFY( d.isCustom() ? d.isValid() : true ); // All custom entries are valid + } + QCOMPARE( customCount, 2 ); + QCOMPARE( validCount, 2 ); + } +} + QTEST_GUILESS_MAIN( TestLibCalamares )