[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
This commit is contained in:
parent
34e31d4331
commit
f157d9c459
@ -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,15 +235,26 @@ Settings::Settings( const QString& settingsFilePath, bool debugMode )
|
||||
QFile file( settingsFilePath );
|
||||
if ( file.exists() && file.open( QFile::ReadOnly | QFile::Text ) )
|
||||
{
|
||||
QByteArray ba = file.readAll();
|
||||
setConfiguration( file.readAll(), file.fileName() );
|
||||
}
|
||||
else
|
||||
{
|
||||
cWarning() << "Cannot read settings file" << file.fileName();
|
||||
}
|
||||
|
||||
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 );
|
||||
debugMode(), CalamaresUtils::yamlToStringList( config[ "modules-search" ] ), m_modulesSearchPaths );
|
||||
interpretInstances( config[ "instances" ], m_customModuleInstances );
|
||||
interpretSequence( config[ "sequence" ], m_modulesSequence );
|
||||
|
||||
@ -247,17 +268,9 @@ Settings::Settings( const QString& settingsFilePath, bool debugMode )
|
||||
}
|
||||
catch ( YAML::Exception& e )
|
||||
{
|
||||
CalamaresUtils::explainYamlException( e, ba, file.fileName() );
|
||||
CalamaresUtils::explainYamlException( e, ba, explainName );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cWarning() << "Cannot read settings file" << file.fileName();
|
||||
}
|
||||
|
||||
s_instance = this;
|
||||
}
|
||||
|
||||
|
||||
QStringList
|
||||
Settings::modulesSearchPaths() const
|
||||
|
@ -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
|
||||
|
@ -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( "<testdata>" ) );
|
||||
|
||||
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 )
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user