feat: add treemap disk space analyzer
version / print-version (push) Successful in 2s

Users need to see at a glance what fills a disk and to remove the largest
entries without digging through a file manager. The treemap makes sizes
comparable, the scan runs off the UI thread, and removing an entry updates
the tree in place instead of rescanning.
This commit is contained in:
BlackAngelTV committed 2026-11-26 18:56:07 +01:00
1 parent 26c9658f0b
commit 6a85eed786
15 files changed
+1461

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
#include "test-helpers.h"
#include <QDir>
#include <QTemporaryDir>
#include <QtTest>
class DiskNodeTest : public QObject {
Q_OBJECT
private slots:
void detachUpdatesAncestors();
void detachKeepsSubtreeIntact();
void detachRefusesTheRoot();
};
void DiskNodeTest::detachUpdatesAncestors()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
QVERIFY(test_helpers::writeFile(dir.filePath("top.bin"), 100));
QVERIFY(QDir().mkpath(dir.filePath("sub")));
QVERIFY(test_helpers::writeFile(dir.filePath("sub/big.bin"), 900));
const std::shared_ptr<DiskNode> tree = test_helpers::scan(dir.path());
QVERIFY(tree);
QCOMPARE(tree->totalSize, quint64(1000));
QCOMPARE(tree->fileCount, quint64(2));
DiskNode *sub = test_helpers::childNamed(*tree, "sub");
QVERIFY(sub);
std::unique_ptr<DiskNode> removed = detachNode(sub);
QVERIFY(removed);
QCOMPARE(tree->totalSize, quint64(100));
QCOMPARE(tree->fileCount, quint64(1));
QCOMPARE(int(tree->children.size()), 1);
}
void DiskNodeTest::detachKeepsSubtreeIntact()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
QVERIFY(QDir().mkpath(dir.filePath("sub/deep")));
QVERIFY(test_helpers::writeFile(dir.filePath("sub/deep/leaf.bin"), 900));
const std::shared_ptr<DiskNode> tree = test_helpers::scan(dir.path());
QVERIFY(tree);
std::unique_ptr<DiskNode> removed = detachNode(test_helpers::childNamed(*tree, "sub"));
QVERIFY(removed);
QCOMPARE(removed->totalSize, quint64(900));
QVERIFY(removed->parent == nullptr);
QVERIFY(test_helpers::childNamed(*removed, "deep") != nullptr);
}
void DiskNodeTest::detachRefusesTheRoot()
{
DiskNode root;
root.kind = NodeKind::Directory;
root.totalSize = 10;
QVERIFY(!detachNode(&root));
}
QTEST_GUILESS_MAIN(DiskNodeTest)
#include "disk-node.test.moc"
+114
View File
@@ -0,0 +1,114 @@
#include "test-helpers.h"
#include <QDir>
#include <QFile>
#include <QTemporaryDir>
#include <QtTest>
class DiskScannerTest : public QObject {
Q_OBJECT
private slots:
void aggregatesNestedSizes();
void sortsChildrenBySize();
void skipsSymlinks();
void reportsMissingDirectory();
void cancellationBeforeStartYieldsNoTree();
};
void DiskScannerTest::aggregatesNestedSizes()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
QVERIFY(test_helpers::writeFile(dir.filePath("top.bin"), 1000));
QVERIFY(QDir().mkpath(dir.filePath("sub/deep")));
QVERIFY(QDir().mkpath(dir.filePath("empty")));
QVERIFY(test_helpers::writeFile(dir.filePath("sub/mid.bin"), 2000));
QVERIFY(test_helpers::writeFile(dir.filePath("sub/deep/leaf.bin"), 3000));
ScanStats stats;
const std::shared_ptr<DiskNode> tree = test_helpers::scan(dir.path(), &stats);
QVERIFY(tree);
QCOMPARE(tree->totalSize, quint64(6000));
QCOMPARE(tree->fileCount, quint64(3));
QCOMPARE(stats.fileCount, quint64(3));
QCOMPARE(stats.dirCount, quint64(3)); // sub, sub/deep, empty
QCOMPARE(stats.totalBytes, quint64(6000));
const DiskNode *sub = test_helpers::childNamed(*tree, "sub");
QVERIFY(sub);
QCOMPARE(sub->totalSize, quint64(5000));
QCOMPARE(sub->fileCount, quint64(2));
const DiskNode *deep = test_helpers::childNamed(*sub, "deep");
QVERIFY(deep);
QCOMPARE(deep->totalSize, quint64(3000));
const DiskNode *empty = test_helpers::childNamed(*tree, "empty");
QVERIFY(empty);
QCOMPARE(empty->totalSize, quint64(0));
QCOMPARE(empty->fileCount, quint64(0));
}
void DiskScannerTest::sortsChildrenBySize()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
QVERIFY(test_helpers::writeFile(dir.filePath("small.bin"), 10));
QVERIFY(test_helpers::writeFile(dir.filePath("large.bin"), 5000));
QVERIFY(test_helpers::writeFile(dir.filePath("medium.bin"), 500));
const std::shared_ptr<DiskNode> tree = test_helpers::scan(dir.path());
QVERIFY(tree);
QCOMPARE(int(tree->children.size()), 3);
QCOMPARE(tree->children.at(0)->name, QStringLiteral("large.bin"));
QCOMPARE(tree->children.at(1)->name, QStringLiteral("medium.bin"));
QCOMPARE(tree->children.at(2)->name, QStringLiteral("small.bin"));
}
void DiskScannerTest::skipsSymlinks()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
const QString target = dir.filePath("target.bin");
QVERIFY(test_helpers::writeFile(target, 1000));
if (!QFile::link(target, dir.filePath("link.bin"))) {
QSKIP("this filesystem does not support symbolic links");
}
const std::shared_ptr<DiskNode> tree = test_helpers::scan(dir.path());
QVERIFY(tree);
QCOMPARE(tree->totalSize, quint64(1000));
QCOMPARE(tree->fileCount, quint64(1));
QVERIFY(test_helpers::childNamed(*tree, "link.bin") == nullptr);
}
void DiskScannerTest::reportsMissingDirectory()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
ScanStats stats;
const std::shared_ptr<DiskNode> tree =
test_helpers::scan(dir.filePath("does-not-exist"), &stats);
QVERIFY(tree);
QCOMPARE(tree->totalSize, quint64(0));
QVERIFY(!stats.errors.isEmpty());
}
void DiskScannerTest::cancellationBeforeStartYieldsNoTree()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
QVERIFY(test_helpers::writeFile(dir.filePath("a.bin"), 10));
DiskScanner scanner(dir.path());
scanner.cancel();
scanner.start();
QVERIFY(scanner.wait(5000));
QVERIFY(!scanner.takeTree());
}
QTEST_GUILESS_MAIN(DiskScannerTest)
#include "disk-scanner.test.moc"
+45
View File
@@ -0,0 +1,45 @@
#pragma once
#include "core/disk-node.h"
#include "core/disk-scanner.h"
#include <QDir>
#include <QFile>
#include <memory>
// Test helpers only: they keep the fixtures readable without hiding behaviour.
namespace test_helpers {
inline bool writeFile(const QString &path, int bytes)
{
QFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
return false;
}
return file.write(QByteArray(bytes, 'x')) == bytes;
}
inline DiskNode *childNamed(const DiskNode &node, const QString &name)
{
for (const auto &child : node.children) {
if (child->name == name) {
return child.get();
}
}
return nullptr;
}
inline std::shared_ptr<DiskNode> scan(const QString &path, ScanStats *stats = nullptr)
{
DiskScanner scanner(path);
scanner.start();
if (!scanner.wait(30000)) {
return nullptr;
}
if (stats) {
*stats = scanner.stats();
}
return scanner.takeTree();
}
} // namespace test_helpers
+84
View File
@@ -0,0 +1,84 @@
#include "core/treemap-layout.h"
#include <QRectF>
#include <QtTest>
class TreemapLayoutTest : public QObject {
Q_OBJECT
private slots:
void emptyInputYieldsNoTiles();
void tilesStayInsideBounds();
void tileAreasAreProportional();
void zeroSizesGetNoTile();
void singleItemFillsTheBounds();
};
void TreemapLayoutTest::emptyInputYieldsNoTiles()
{
QVERIFY(squarify({}, QRectF(0, 0, 400, 300)).isEmpty());
QVERIFY(squarify({0, 0}, QRectF(0, 0, 400, 300)).isEmpty());
QVERIFY(squarify({10, 20}, QRectF(0, 0, 0, 300)).isEmpty());
}
void TreemapLayoutTest::tilesStayInsideBounds()
{
const QRectF bounds(10, 20, 800, 500);
const QVector<qreal> sizes{50, 25, 15, 5, 3, 1, 1};
const QVector<TreemapTile> tiles = squarify(sizes, bounds);
QCOMPARE(tiles.size(), sizes.size());
const QRectF allowed = bounds.adjusted(-0.01, -0.01, 0.01, 0.01);
for (const TreemapTile &tile : tiles) {
QVERIFY(allowed.contains(tile.rect));
QVERIFY(tile.rect.width() > 0.0);
QVERIFY(tile.rect.height() > 0.0);
}
for (int i = 0; i < tiles.size(); ++i) {
for (int j = i + 1; j < tiles.size(); ++j) {
const QRectF overlap = tiles.at(i).rect.intersected(tiles.at(j).rect);
QVERIFY(qFuzzyIsNull(overlap.width() * overlap.height()));
}
}
}
void TreemapLayoutTest::tileAreasAreProportional()
{
const QRectF bounds(0, 0, 800, 600);
const QVector<qreal> sizes{50, 25, 15, 10};
const QVector<TreemapTile> tiles = squarify(sizes, bounds);
QCOMPARE(tiles.size(), sizes.size());
const qreal total = 100.0;
for (const TreemapTile &tile : tiles) {
const qreal expected = bounds.width() * bounds.height() * sizes.at(tile.index) / total;
const qreal actual = tile.rect.width() * tile.rect.height();
QVERIFY2(qAbs(actual - expected) / expected < 0.01,
qPrintable(QStringLiteral("tile %1: expected %2, got %3")
.arg(tile.index)
.arg(expected)
.arg(actual)));
}
}
void TreemapLayoutTest::zeroSizesGetNoTile()
{
const QVector<TreemapTile> tiles = squarify({10, 0, 30}, QRectF(0, 0, 200, 100));
QCOMPARE(tiles.size(), 2);
for (const TreemapTile &tile : tiles) {
QVERIFY(tile.index != 1);
}
}
void TreemapLayoutTest::singleItemFillsTheBounds()
{
const QRectF bounds(0, 0, 320, 200);
const QVector<TreemapTile> tiles = squarify({42}, bounds);
QCOMPARE(tiles.size(), 1);
QVERIFY(qAbs(tiles.at(0).rect.width() - bounds.width()) < 1e-6);
QVERIFY(qAbs(tiles.at(0).rect.height() - bounds.height()) < 1e-6);
}
QTEST_GUILESS_MAIN(TreemapLayoutTest)
#include "treemap-layout.test.moc"