diff --git a/src/libcalamares/utils/Runner.cpp b/src/libcalamares/utils/Runner.cpp index 8e81cb487..a8bb3be1c 100644 --- a/src/libcalamares/utils/Runner.cpp +++ b/src/libcalamares/utils/Runner.cpp @@ -40,7 +40,7 @@ relativeChangeDirectory( QDir& directory, const QString& subdir ) STATICTEST std::pair< bool, QDir > -calculateWorkingDirectory( Calamares::Utils::RunLocation location, QString directory ) +calculateWorkingDirectory( Calamares::Utils::RunLocation location, const QString& directory ) { Calamares::GlobalStorage* gs = Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr; diff --git a/src/libcalamares/utils/Tests.cpp b/src/libcalamares/utils/Tests.cpp index 9297be31f..b20581f4c 100644 --- a/src/libcalamares/utils/Tests.cpp +++ b/src/libcalamares/utils/Tests.cpp @@ -13,6 +13,7 @@ #include "Entropy.h" #include "Logger.h" #include "RAII.h" +#include "Runner.h" #include "String.h" #include "Traits.h" #include "UMask.h" @@ -72,6 +73,7 @@ private Q_SLOTS: /** @section Test Runner directory-manipulation. */ void testRunnerDirs(); + void testCalculateWorkingDirectory(); private: void recursiveCompareMap( const QVariantMap& a, const QVariantMap& b, int depth ); @@ -836,6 +838,52 @@ LibCalamaresTests::testRunnerDirs() } } +// Method under test +extern std::pair< bool, QDir > calculateWorkingDirectory( Calamares::Utils::RunLocation location, + const QString& directory ); + +void +LibCalamaresTests::testCalculateWorkingDirectory() +{ + Calamares::GlobalStorage* gs + = Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr; + + if ( !gs ) + { + cDebug() << "Creating new JobQueue"; + (void)new Calamares::JobQueue(); + gs = Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr; + } + QVERIFY( gs ); + + // Working with a rootMountPoint set + QTemporaryDir tempRoot( QDir::tempPath() + QStringLiteral( "/test-job-XXXXXX" ) ); + gs->insert( "rootMountPoint", tempRoot.path() ); + + { + auto [ ok, d ] = calculateWorkingDirectory( CalamaresUtils::System::RunLocation::RunInHost, QString() ); + QVERIFY( ok ); + QCOMPARE( d, QDir::current() ); + } + { + auto [ ok, d ] = calculateWorkingDirectory( CalamaresUtils::System::RunLocation::RunInTarget, QString() ); + QVERIFY( ok ); + QCOMPARE( d.absolutePath(), tempRoot.path() ); + } + + gs->remove( "rootMountPoint" ); + { + auto [ ok, d ] = calculateWorkingDirectory( CalamaresUtils::System::RunLocation::RunInHost, QString() ); + QVERIFY( ok ); + QCOMPARE( d, QDir::current() ); + } + { + auto [ ok, d ] = calculateWorkingDirectory( CalamaresUtils::System::RunLocation::RunInTarget, QString() ); + QVERIFY( !ok ); + QCOMPARE( d, QDir::current() ); + } +} + QTEST_GUILESS_MAIN( LibCalamaresTests )