[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.
This commit is contained in:
Adriaan de Groot 2020-08-11 16:15:37 +02:00
parent 4cd2a4ae91
commit d81d585c32
3 changed files with 33 additions and 7 deletions

View File

@ -282,12 +282,6 @@ Settings::reconcileInstancesAndSequence()
{ {
for ( const auto& instanceKey : step.second ) for ( const auto& instanceKey : step.second )
{ {
if ( !instanceKey.isValid() )
{
cWarning() << "Invalid instance key in *sequence*," << instanceKey;
continue;
}
targetKey = instanceKey; targetKey = instanceKey;
const auto it = std::find_if( m_moduleInstances.constBegin(), m_moduleInstances.constEnd(), moduleFinder ); const auto it = std::find_if( m_moduleInstances.constBegin(), m_moduleInstances.constEnd(), moduleFinder );
if ( it == m_moduleInstances.constEnd() ) if ( it == m_moduleInstances.constEnd() )
@ -447,4 +441,25 @@ Settings::init( const QString& path )
return new Calamares::Settings( path, true ); 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 } // namespace Calamares

View File

@ -122,6 +122,13 @@ public:
QString brandingComponentName() const; 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? /** @brief Is this a debugging run?
* *
* Returns true if Calamares is in debug mode. In debug mode, * Returns true if Calamares is in debug mode. In debug mode,

View File

@ -367,6 +367,7 @@ TestLibCalamares::testSettings()
{ {
Calamares::Settings s( false ); Calamares::Settings s( false );
QVERIFY( !s.debugMode() ); QVERIFY( !s.debugMode() );
QVERIFY( !s.isValid() );
} }
{ {
Calamares::Settings s( true ); Calamares::Settings s( true );
@ -374,8 +375,10 @@ TestLibCalamares::testSettings()
QVERIFY( s.moduleInstances().isEmpty() ); QVERIFY( s.moduleInstances().isEmpty() );
QVERIFY( s.modulesSequence().isEmpty() ); QVERIFY( s.modulesSequence().isEmpty() );
QVERIFY( s.brandingComponentName().isEmpty() ); QVERIFY( s.brandingComponentName().isEmpty() );
QVERIFY( !s.isValid() );
s.setConfiguration( R"(--- s.setConfiguration( R"(---
branding: default # needed for it to be considered valid
instances: instances:
- module: welcome - module: welcome
id: hi id: hi
@ -397,7 +400,8 @@ sequence:
QVERIFY( s.debugMode() ); QVERIFY( s.debugMode() );
QCOMPARE( s.moduleInstances().count(), 4 ); // there are 4 module instances mentioned QCOMPARE( s.moduleInstances().count(), 4 ); // there are 4 module instances mentioned
QCOMPARE( s.modulesSequence().count(), 2 ); // 2 steps (show, exec) 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, // Make a lambda where we can adjust what it looks for from the outside,
// by capturing a reference. // by capturing a reference.