From 75eb2c3cd4482543afec01a995f7249ced694c8a Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 8 Feb 2021 11:21:15 +0100 Subject: [PATCH] [libcalamares] Add tests for filesystem_use service --- src/libcalamares/CMakeLists.txt | 3 + src/libcalamares/partition/Tests.cpp | 93 ++++++++++++++++++++++++---- src/libcalamares/partition/Tests.h | 33 ---------- 3 files changed, 84 insertions(+), 45 deletions(-) delete mode 100644 src/libcalamares/partition/Tests.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index 404ac129f..69533cfff 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -242,7 +242,10 @@ calamares_add_test( calamares_add_test( libcalamarespartitiontest SOURCES + partition/Global.cpp partition/Tests.cpp + LIBRARIES + ${OPTIONAL_PRIVATE_LIBRARIES} ) if( KPMcore_FOUND ) diff --git a/src/libcalamares/partition/Tests.cpp b/src/libcalamares/partition/Tests.cpp index cd2922ee2..512a23110 100644 --- a/src/libcalamares/partition/Tests.cpp +++ b/src/libcalamares/partition/Tests.cpp @@ -8,32 +8,54 @@ * */ -#include "Tests.h" - #include "PartitionSize.h" +#include "GlobalStorage.h" +#include "utils/Logger.h" + +#include +#include + +// Implementation details being tested +extern bool isFilesystemUsedGS( const Calamares::GlobalStorage& gs, const QString& filesystemType ); +extern void useFilesystemGS( Calamares::GlobalStorage& gs, const QString& filesystemType, bool used ); + + using SizeUnit = CalamaresUtils::Partition::SizeUnit; using PartitionSize = CalamaresUtils::Partition::PartitionSize; Q_DECLARE_METATYPE( SizeUnit ) -#include "utils/Logger.h" +class PartitionServiceTests : public QObject +{ + Q_OBJECT +public: + PartitionServiceTests(); + ~PartitionServiceTests() override; -#include +private Q_SLOTS: + void initTestCase(); -QTEST_GUILESS_MAIN( PartitionSizeTests ) + void testUnitComparison_data(); + void testUnitComparison(); -PartitionSizeTests::PartitionSizeTests() {} + void testUnitNormalisation_data(); + void testUnitNormalisation(); -PartitionSizeTests::~PartitionSizeTests() {} + void testFilesystemGS(); +}; + +PartitionServiceTests::PartitionServiceTests() {} + +PartitionServiceTests::~PartitionServiceTests() {} void -PartitionSizeTests::initTestCase() +PartitionServiceTests::initTestCase() { } void -PartitionSizeTests::testUnitComparison_data() +PartitionServiceTests::testUnitComparison_data() { QTest::addColumn< SizeUnit >( "u1" ); QTest::addColumn< SizeUnit >( "u2" ); @@ -71,7 +93,7 @@ original_compare( SizeUnit m_unit, SizeUnit other_m_unit ) } void -PartitionSizeTests::testUnitComparison() +PartitionServiceTests::testUnitComparison() { QFETCH( SizeUnit, u1 ); QFETCH( SizeUnit, u2 ); @@ -98,7 +120,7 @@ constexpr qint64 operator""_qi( unsigned long long m ) } void -PartitionSizeTests::testUnitNormalisation_data() +PartitionServiceTests::testUnitNormalisation_data() { QTest::addColumn< SizeUnit >( "u1" ); QTest::addColumn< int >( "v" ); @@ -131,7 +153,7 @@ PartitionSizeTests::testUnitNormalisation_data() } void -PartitionSizeTests::testUnitNormalisation() +PartitionServiceTests::testUnitNormalisation() { QFETCH( SizeUnit, u1 ); QFETCH( int, v ); @@ -139,3 +161,50 @@ PartitionSizeTests::testUnitNormalisation() QCOMPARE( PartitionSize( v, u1 ).toBytes(), bytes ); } + +void +PartitionServiceTests::testFilesystemGS() +{ + // Some filesystems names, they don't have to be real + const QStringList fsNames { "ext4", "zfs", "berries", "carrot" }; + // Predicate to return whether we consider this FS in use + auto pred = []( const QString& s ) { return !s.startsWith( 'z' ); }; + + // Fill the GS + Calamares::GlobalStorage gs; + for ( const auto& s : fsNames ) + { + useFilesystemGS( gs, s, pred( s ) ); + } + + QVERIFY( gs.contains( "filesystem_use" ) ); + { + const auto map = gs.value( "filesystem_use" ).toMap(); + QCOMPARE( map.count(), fsNames.count() ); + } + + for ( const auto& s : fsNames ) + { + QCOMPARE( isFilesystemUsedGS( gs, s ), pred( s ) ); + } + QCOMPARE( isFilesystemUsedGS( gs, QStringLiteral( "derp" ) ), false ); + QCOMPARE( isFilesystemUsedGS( gs, QString() ), false ); + // But I can set a value for QString! + useFilesystemGS( gs, QString(), true ); + QCOMPARE( isFilesystemUsedGS( gs, QString() ), true ); + // .. and replace it again + useFilesystemGS( gs, QString(), false ); + QCOMPARE( isFilesystemUsedGS( gs, QString() ), false ); + // Now there is one more key + { + const auto map = gs.value( "filesystem_use" ).toMap(); + QCOMPARE( map.count(), fsNames.count() + 1 ); + } +} + + +QTEST_GUILESS_MAIN( PartitionServiceTests ) + +#include "utils/moc-warnings.h" + +#include "Tests.moc" diff --git a/src/libcalamares/partition/Tests.h b/src/libcalamares/partition/Tests.h deleted file mode 100644 index 0d6f77a76..000000000 --- a/src/libcalamares/partition/Tests.h +++ /dev/null @@ -1,33 +0,0 @@ -/* === This file is part of Calamares - === - * - * SPDX-FileCopyrightText: 2019 Adriaan de Groot - * SPDX-License-Identifier: GPL-3.0-or-later - * - * Calamares is Free Software: see the License-Identifier above. - * - * - */ - -#ifndef LIBCALAMARES_PARTITION_TESTS_H -#define LIBCALAMARES_PARTITION_TESTS_H - -#include - -class PartitionSizeTests : public QObject -{ - Q_OBJECT -public: - PartitionSizeTests(); - ~PartitionSizeTests() override; - -private Q_SLOTS: - void initTestCase(); - - void testUnitComparison_data(); - void testUnitComparison(); - - void testUnitNormalisation_data(); - void testUnitNormalisation(); -}; - -#endif