Tests: simple listing of attached devices

This commit is contained in:
Adriaan de Groot 2017-10-02 14:13:47 -04:00
parent c4ae5d680c
commit 63b501f41d
2 changed files with 117 additions and 1 deletions

View File

@ -20,7 +20,6 @@ add_library(testhelpers OBJECT helpers.cpp)
macro (kpm_test name) macro (kpm_test name)
add_executable(${name} ${ARGN} $<TARGET_OBJECTS:testhelpers>) add_executable(${name} ${ARGN} $<TARGET_OBJECTS:testhelpers>)
target_link_libraries(${name} kpmcore) target_link_libraries(${name} kpmcore)
add_test(${name} ${name})
endmacro() endmacro()
### ###
@ -32,4 +31,14 @@ if(TARGET pmdummybackendplugin)
endif() endif()
if(TARGET pmlibpartedbackendplugin) if(TARGET pmlibpartedbackendplugin)
add_test(NAME testinit-parted COMMAND testinit $<TARGET_FILE:pmlibpartedbackendplugin>) add_test(NAME testinit-parted COMMAND testinit $<TARGET_FILE:pmlibpartedbackendplugin>)
else()
return() # All the rest really needs a working backend
endif() endif()
set(BACKEND $<TARGET_FILE:pmlibpartedbackendplugin>)
###
#
# Listing devices, partitions
kpm_test(testlist testlist.cpp)
add_test(NAME testlist COMMAND testlist ${BACKEND})

107
test/testlist.cpp Normal file
View File

@ -0,0 +1,107 @@
/*************************************************************************
* Copyright 2017 by Adriaan de Groot <groot@kde.org> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
*************************************************************************/
// SPDX-License-Identifier: GPL-3.0+
// Lists devices
#include "helpers.h"
#include "backend/corebackend.h"
#include "backend/corebackendmanager.h"
#include "core/device.h"
#include "core/partition.h"
#include "util/capacity.h"
#include <QDebug>
#include <QList>
using PartitionList = QList<Partition *>;
// Recursive helper for flatten(), adds partitions that
// are children of @p n to the list @p l.
void _flatten(PartitionList& l, PartitionNode *n)
{
for (const auto &p : n->children()) {
l.append(p);
if (p->roles().has(PartitionRole::Extended)) {
_flatten(l, p);
}
}
}
/**
* Recursively walk the partition table and collect all the partitions
* in it (also in extended partitions). Produces a sorted list.
*/
PartitionList flatten(PartitionTable *table)
{
PartitionList l;
_flatten(l, table);
std::sort(l.begin(), l.end(),
[](const Partition* p1, const Partition* p2) { return p1->number() < p2->number(); });
return l;
}
int main( int argc, char **argv )
{
if (argc != 2)
{
KPMCoreInitializer i;
if (!i.isValid())
return 1;
}
else
{
KPMCoreInitializer i( argv[1] );
if (!i.isValid())
return 1;
}
auto backend = CoreBackendManager::self()->backend();
if (!backend)
{
qWarning() << "Could not get backend.";
return 1;
}
auto devices = backend->scanDevices();
qDebug() << "Found" << devices.length() << "devices.";
for (const auto pdev : devices)
{
qDebug() << "Device @" << (void *)pdev;
qDebug() << " " << pdev->prettyName();
const auto partitiontable = pdev->partitionTable();
qDebug() << " Partition Table @" << (void *)partitiontable << '('
<< (partitiontable ? partitiontable->typeName() : QLatin1String("null")) << ')';
const auto partitionlist = flatten(partitiontable);
for (const auto &p : qAsConst(partitionlist))
qDebug() << " "
<< p->number()
<< p->partitionPath()
<< p->label()
<< Capacity::formatByteSize(p->capacity())
<< p->fileSystem().name();
}
return 0;
}