File indexing completed on 2024-04-21 05:42:26

0001 // clang-format off
0002 /*
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2019-2020 Michael Reeves reeves.87@gmail.com
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 // clang-format on
0009 
0010 #include <QTest>
0011 #include <QtGlobal>
0012 
0013 #include "../CvsIgnoreList.h"
0014 #include "MocIgnoreFile.h"
0015 
0016 class IgnoreListTestInterface: public CvsIgnoreList
0017 {
0018   public:
0019     bool isEmpty(const QString& dir) const
0020     {
0021         return m_ignorePatterns.find(dir) == m_ignorePatterns.end();
0022     }
0023 
0024     QStringList getExactMatchList(const QString& dir) const
0025     {
0026         const auto ignorePatternsIt = m_ignorePatterns.find(dir);
0027         return ignorePatternsIt != m_ignorePatterns.end() ? ignorePatternsIt->second.m_exactPatterns : QStringList();
0028     }
0029 };
0030 
0031 class CvsIgnoreListTest : public QObject
0032 {
0033     const QString defaultPatterns = QString::fromLatin1(". .. core RCSLOG tags TAGS RCS SCCS .make.state "
0034                                                         ".nse_depinfo #* .#* cvslog.* ,* CVS CVS.adm .del-* *.a *.olb *.o *.obj "
0035                                                         "*.so *.Z *~ *.old *.elc *.ln *.bak *.BAK *.orig *.rej *.exe _$* *$");
0036     Q_OBJECT
0037   private Q_SLOTS:
0038     void initTestCase()
0039     {
0040         MocIgnoreFile file;
0041         IgnoreListTestInterface test;
0042         //sanity check defaults
0043         QVERIFY(test.isEmpty(file.absoluteFilePath()));
0044         //MocIgnoreFile should be emulating a readable local file.
0045         QVERIFY(file.isLocal());
0046         QVERIFY(file.exists());
0047         QVERIFY(file.isReadable());
0048         QCOMPARE(file.fileName(), QStringLiteral(".cvsignore"));
0049     }
0050 
0051     void addEntriesFromString()
0052     {
0053         IgnoreListTestInterface test;
0054         QString testDir("dir");
0055         QString testString = ". .. core RCSLOG tags TAGS RCS SCCS .make.state";
0056         test.addEntriesFromString(testDir, testString);
0057         QVERIFY(!test.getExactMatchList(testDir).isEmpty());
0058         QVERIFY(test.getExactMatchList(testDir) == testString.split(' '));
0059     }
0060 
0061     void matches()
0062     {
0063         CvsIgnoreList test;
0064 
0065         QString testDir("dir");
0066         QString testString = ". .. core RCSLOG tags TAGS RCS SCCS .make.state *.so _$*";
0067         test.addEntriesFromString(testDir, testString);
0068 
0069         QVERIFY(test.matches(testDir, ".", false));
0070         QVERIFY(test.matches(testDir, ".", true));
0071         QVERIFY(!test.matches(testDir, "cores core", true));
0072         QVERIFY(test.matches(testDir, "core", true));
0073         QVERIFY(!test.matches(testDir, "Core", true));
0074         QVERIFY(test.matches(testDir, "Core", false));
0075         QVERIFY(!test.matches(testDir, "a", false));
0076         QVERIFY(test.matches(testDir, "core", false));
0077         //ends with .so
0078         QVERIFY(test.matches(testDir, "sdf3.so", true));
0079         QVERIFY(!test.matches(testDir, "sdf3.to", true));
0080         QVERIFY(test.matches(testDir, "*.so", true));
0081         QVERIFY(test.matches(testDir, "sdf4.So", false));
0082         QVERIFY(!test.matches(testDir, "sdf4.So", true));
0083         //starts with _$ddsf
0084         QVERIFY(test.matches(testDir, "_$ddsf", true));
0085         //Should only match exact strings not partial ones
0086         QVERIFY(!test.matches(testDir, "sdf4.so ", true));
0087         QVERIFY(!test.matches(testDir, " _$ddsf", true));
0088 
0089         testString = "*.*";
0090         test = CvsIgnoreList();
0091         test.addEntriesFromString(testDir, "*.*");
0092 
0093         QVERIFY(test.matches(testDir, "k.K", false));
0094         QVERIFY(test.matches(testDir, "*.K", false));
0095         QVERIFY(test.matches(testDir, "*.*", false));
0096         QVERIFY(!test.matches(testDir, "*+*", false));
0097         QVERIFY(!test.matches(testDir, "asd", false));
0098         //The following are matched by the above
0099         QVERIFY(test.matches(testDir, "a k.k", false));
0100         QVERIFY(test.matches(testDir, "k.k v", false));
0101         QVERIFY(test.matches(testDir, " k.k", false));
0102         QVERIFY(test.matches(testDir, "k.k ", false));
0103 
0104         // Test matches from a different dir
0105         QString otherDir("other_dir");
0106         QVERIFY(!test.matches(otherDir, "sdf3.so", true));
0107     }
0108 
0109     void testDefaults()
0110     {
0111         CvsIgnoreList test;
0112         CvsIgnoreList expected;
0113         MocIgnoreFile file;
0114         DirectoryList dirList;
0115 
0116         /*
0117             Verify default init. For this to work we must:
0118                 1. Unset CVSIGNORE
0119                 2. Insure no patterns are read from a .cvsignore file.
0120             MocCvsIgnore emulates a blank cvs file by default insuring the second condition.
0121         */
0122         test = CvsIgnoreList();
0123         //
0124         qunsetenv("CVSIGNORE");
0125 
0126         QString testDir("dir");
0127         expected.addEntriesFromString(testDir, defaultPatterns);
0128 
0129         test.enterDir(testDir, dirList);
0130         QVERIFY(test.m_ignorePatterns[testDir].m_endPatterns == expected.m_ignorePatterns[testDir].m_endPatterns);
0131         QVERIFY(test.m_ignorePatterns[testDir].m_exactPatterns == expected.m_ignorePatterns[testDir].m_exactPatterns);
0132         QVERIFY(test.m_ignorePatterns[testDir].m_startPatterns == expected.m_ignorePatterns[testDir].m_startPatterns);
0133         QVERIFY(test.m_ignorePatterns[testDir].m_generalPatterns == expected.m_ignorePatterns[testDir].m_generalPatterns);
0134     }
0135 };
0136 
0137 QTEST_MAIN(CvsIgnoreListTest);
0138 
0139 #include "CvsIgnoreListTest.moc"