[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.
main
Adriaan de Groot 4 years ago
parent 4cd2a4ae91
commit d81d585c32

@ -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

@ -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,

@ -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.

Loading…
Cancel
Save