From d81d585c3235d8f9feed9d30bd554b7648f02194 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 11 Aug 2020 16:15:37 +0200 Subject: [PATCH] [libcalamares] Add isValid() to Settings - settings can be invalid (missing data, whatever) and that can be used to shut things down early. Validity must be checked explicitly, though. --- src/libcalamares/Settings.cpp | 27 +++++++++++++++++++++------ src/libcalamares/Settings.h | 7 +++++++ src/libcalamares/Tests.cpp | 6 +++++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/libcalamares/Settings.cpp b/src/libcalamares/Settings.cpp index 813da5c35..7e10de0e8 100644 --- a/src/libcalamares/Settings.cpp +++ b/src/libcalamares/Settings.cpp @@ -282,12 +282,6 @@ Settings::reconcileInstancesAndSequence() { for ( const auto& instanceKey : step.second ) { - if ( !instanceKey.isValid() ) - { - cWarning() << "Invalid instance key in *sequence*," << instanceKey; - continue; - } - targetKey = instanceKey; const auto it = std::find_if( m_moduleInstances.constBegin(), m_moduleInstances.constEnd(), moduleFinder ); if ( it == m_moduleInstances.constEnd() ) @@ -447,4 +441,25 @@ Settings::init( const QString& path ) return new Calamares::Settings( path, true ); } +bool +Settings::isValid() const +{ + if ( brandingComponentName().isEmpty() ) + { + cWarning() << "No branding component is set"; + return false; + } + + const auto invalidDescriptor = []( const InstanceDescription& d ) { return !d.isValid(); }; + const auto invalidDescriptorIt + = std::find_if( m_moduleInstances.constBegin(), m_moduleInstances.constEnd(), invalidDescriptor ); + if ( invalidDescriptorIt != m_moduleInstances.constEnd() ) + { + cWarning() << "Invalid module instance in *instances* or *sequence*"; + return false; + } + + return true; +} + } // namespace Calamares diff --git a/src/libcalamares/Settings.h b/src/libcalamares/Settings.h index 5507e2322..8e463fc70 100644 --- a/src/libcalamares/Settings.h +++ b/src/libcalamares/Settings.h @@ -122,6 +122,13 @@ public: QString brandingComponentName() const; + /** @brief Are the settings consistent and valid? + * + * Checks that at least branding is set, and that the instances + * and sequence are valid. + */ + bool isValid() const; + /** @brief Is this a debugging run? * * Returns true if Calamares is in debug mode. In debug mode, diff --git a/src/libcalamares/Tests.cpp b/src/libcalamares/Tests.cpp index 61da7639e..1dc297dd4 100644 --- a/src/libcalamares/Tests.cpp +++ b/src/libcalamares/Tests.cpp @@ -367,6 +367,7 @@ TestLibCalamares::testSettings() { Calamares::Settings s( false ); QVERIFY( !s.debugMode() ); + QVERIFY( !s.isValid() ); } { Calamares::Settings s( true ); @@ -374,8 +375,10 @@ TestLibCalamares::testSettings() QVERIFY( s.moduleInstances().isEmpty() ); QVERIFY( s.modulesSequence().isEmpty() ); QVERIFY( s.brandingComponentName().isEmpty() ); + QVERIFY( !s.isValid() ); s.setConfiguration( R"(--- +branding: default # needed for it to be considered valid instances: - module: welcome id: hi @@ -397,7 +400,8 @@ sequence: QVERIFY( s.debugMode() ); QCOMPARE( s.moduleInstances().count(), 4 ); // there are 4 module instances mentioned QCOMPARE( s.modulesSequence().count(), 2 ); // 2 steps (show, exec) - QVERIFY( s.brandingComponentName().isEmpty() ); + QVERIFY( !s.brandingComponentName().isEmpty() ); + QVERIFY( s.isValid() ); // Make a lambda where we can adjust what it looks for from the outside, // by capturing a reference.