Files
FWS-DiskCleaner/tests/disk-scanner.test.cpp
T
BlackAngelTV 6a85eed786
version / print-version (push) Successful in 2s
feat: add treemap disk space analyzer
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.
2026-11-26 18:56:07 +01:00

115 lines
3.5 KiB
C++

#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"