File indexing completed on 2024-04-14 03:50:04

0001 /*
0002     Copyright 2016 Harald Sitter <sitter@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) version 3, or any
0008     later version accepted by the membership of KDE e.V. (or its
0009     successor approved by the membership of KDE e.V.), which shall
0010     act as a proxy defined in Section 6 of version 3 of the license.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Lesser General Public License for more details.
0016 
0017     You should have received a copy of the GNU Lesser General Public
0018     License along with this library. If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <QObject>
0022 #include <QProcess>
0023 #include <QStandardPaths>
0024 #include <QTest>
0025 
0026 #include "testhelpers.h"
0027 
0028 class DupeTest : public QObject
0029 {
0030     Q_OBJECT
0031 
0032     QStringList splitOnUnescapedSpace(const QString &line)
0033     {
0034         QStringList ret;
0035         const int lineLength = line.length();
0036         int start = 0;
0037         for (int pos = 0; pos < lineLength; ++pos) {
0038             const QChar ch = line[pos];
0039             if (ch == QLatin1Char('\\')) {
0040                 ++pos;
0041                 continue;
0042             } else if (ch == QLatin1Char(' ')) {
0043                 ret.append(line.mid(start, pos - start));
0044                 start = pos + 1;
0045             }
0046         }
0047         if (start < lineLength) {
0048             ret.append(line.mid(start));
0049         }
0050         return ret;
0051     }
0052 
0053     void readLines(QProcess &proc)
0054     {
0055         QString line;
0056         while (proc.canReadLine() || proc.waitForReadyRead()) {
0057             line = proc.readLine();
0058             failListContent(splitOnUnescapedSpace(line.simplified()), "The following files are duplicates but not links:\n");
0059         }
0060     }
0061 
0062     void dupesForDirectory(const QString &path)
0063     {
0064         const QString exec = QStandardPaths::findExecutable(QStringLiteral("fdupes"));
0065         QVERIFY(!exec.isEmpty());
0066         QProcess proc;
0067         proc.setProgram(exec);
0068         proc.setArguments(QStringList() << QStringLiteral("--recurse") << QStringLiteral("--sameline") << QStringLiteral("--nohidden") << path);
0069         proc.start();
0070         proc.waitForStarted();
0071         readLines(proc);
0072     }
0073 
0074 private Q_SLOTS:
0075     void test_duplicates()
0076     {
0077         if (QStandardPaths::findExecutable(QStringLiteral("fdupes")).isEmpty()) {
0078 #ifdef Q_OS_UNIX
0079             // Fail and skip. This is a fairly relevant test, so it not running is a warning really.
0080             QFAIL("this test needs the fdupes binary (1.51+) to run");
0081 #else
0082             // On Windows let's just skip it
0083             QSKIP("this test needs the fdupes binary (1.51+) to run");
0084 #endif
0085         }
0086         for (auto dir : ICON_DIRS) {
0087             dupesForDirectory(PROJECT_SOURCE_DIR + QStringLiteral("/") + dir);
0088         }
0089     }
0090 };
0091 
0092 QTEST_GUILESS_MAIN(DupeTest)
0093 
0094 #include "dupetest.moc"