[libcalamares] Add tests for relativeCangeDirectory()

This commit is contained in:
Adriaan de Groot 2021-10-30 14:17:44 +02:00
parent 0b943e801c
commit e5fa58b890
2 changed files with 92 additions and 0 deletions

View File

@ -304,6 +304,7 @@ calamares_add_test(
libcalamaresutilstest
SOURCES
utils/Tests.cpp
utils/Runner.cpp
)
calamares_add_test(

View File

@ -70,6 +70,9 @@ private Q_SLOTS:
void testStringTruncationShorter();
void testStringTruncationDegenerate();
/** @section Test Runner directory-manipulation. */
void testRunnerDirs();
private:
void recursiveCompareMap( const QVariantMap& a, const QVariantMap& b, int depth );
};
@ -746,6 +749,94 @@ LibCalamaresTests::testStringTruncationDegenerate()
}
static QString
dirname( const QTemporaryDir& d )
{
return d.path().split( '/' ).last();
}
static QString
dirname( const QDir& d )
{
return d.absolutePath().split( '/' ).last();
}
// Method under test
extern bool relativeChangeDirectory( QDir& directory, const QString& subdir );
void
LibCalamaresTests::testRunnerDirs()
{
Logger::setupLogLevel( Logger::LOGDEBUG );
QDir startDir( QDir::current() );
QTemporaryDir tempDir( "./utilstest" );
QVERIFY( tempDir.isValid() );
QVERIFY( startDir.isReadable() );
// Test changing "downward"
{
QDir testDir( QDir::current() );
QCOMPARE( startDir, testDir );
}
{
QDir testDir( QDir::current() );
const bool could_change_to_dot = relativeChangeDirectory( testDir, QStringLiteral( "." ) );
QVERIFY( could_change_to_dot );
QCOMPARE( startDir, testDir );
}
{
// The tempDir was created inside the current directory, we want only the subdir-name
QDir testDir( QDir::current() );
const bool could_change_to_temp = relativeChangeDirectory( testDir, dirname( tempDir ) );
QVERIFY( could_change_to_temp );
QVERIFY( startDir != testDir );
QVERIFY( testDir.absolutePath().startsWith( startDir.absolutePath() ) );
}
// Test changing to something that doesn't exist
{
QDir testDir( QDir::current() );
const bool could_change_to_bogus = relativeChangeDirectory( testDir, QStringLiteral( "bogus" ) );
QVERIFY( !could_change_to_bogus );
QCOMPARE( startDir, testDir ); // Must be unchanged
}
// Testing escape-from-start
{
// Escape briefly from the start
QDir testDir( QDir::current() );
const bool could_change_to_current
= relativeChangeDirectory( testDir, QStringLiteral( "../" ) + dirname( startDir ) );
QVERIFY( could_change_to_current );
QCOMPARE( startDir, testDir ); // The change succeeded, but net effect is zero
const bool could_change_to_temp = relativeChangeDirectory(
testDir, QStringLiteral( "../" ) + dirname( startDir ) + QStringLiteral( "/./" ) + dirname( tempDir ) );
QVERIFY( could_change_to_temp );
QVERIFY( startDir != testDir );
QVERIFY( testDir.absolutePath().startsWith( startDir.absolutePath() ) );
}
{
// Escape?
QDir testDir( QDir::current() );
const bool could_change_to_parent = relativeChangeDirectory( testDir, QStringLiteral( "../" ) );
QVERIFY( !could_change_to_parent );
QCOMPARE( startDir, testDir ); // Change failed
const bool could_change_to_tmp = relativeChangeDirectory( testDir, QStringLiteral( "/tmp" ) );
QVERIFY( !could_change_to_tmp );
QCOMPARE( startDir, testDir );
const bool could_change_to_elsewhere = relativeChangeDirectory( testDir, QStringLiteral( "../src" ) );
QVERIFY( !could_change_to_elsewhere );
QCOMPARE( startDir, testDir );
}
}
QTEST_GUILESS_MAIN( LibCalamaresTests )
#include "utils/moc-warnings.h"