File indexing completed on 2024-05-05 08:54:38
0001 /****************************************************************************** 0002 * Permission to use, copy, modify, and distribute the software 0003 * and its documentation for any purpose and without fee is hereby 0004 * granted, provided that the above copyright notice appear in all 0005 * copies and that both that the copyright notice and this 0006 * permission notice and warranty disclaimer appear in supporting 0007 * documentation, and that the name of the author not be used in 0008 * advertising or publicity pertaining to distribution of the 0009 * software without specific, written prior permission. 0010 * 0011 * The author disclaim all warranties with regard to this 0012 * software, including all implied warranties of merchantability 0013 * and fitness. In no event shall the author be liable for any 0014 * special, indirect or consequential damages or any damages 0015 * whatsoever resulting from loss of use, data or profits, whether 0016 * in an action of contract, negligence or other tortious action, 0017 * arising out of or in connection with the use or performance of 0018 * this software. 0019 */ 0020 0021 #include "TestHelpers.h" 0022 0023 #include "qgitrepository.h" 0024 #include "qgitremote.h" 0025 0026 #include <QPointer> 0027 #include <QDir> 0028 0029 using namespace LibQGit2; 0030 0031 class TestRepository : public TestBase 0032 { 0033 Q_OBJECT 0034 public: 0035 TestRepository() : 0036 branchName("new_branch") 0037 {} 0038 0039 private slots: 0040 virtual void init(); 0041 virtual void cleanup(); 0042 0043 void testRemoteUrlChanging(); 0044 void testLookingUpRevision(); 0045 void testCreateBranch(); 0046 void testDeleteBranch(); 0047 void testShouldIgnore(); 0048 void testIdentitySetting(); 0049 0050 private: 0051 const QString branchName; 0052 QPointer<Repository> repo; 0053 }; 0054 0055 void TestRepository::init() 0056 { 0057 TestBase::init(); 0058 0059 QVERIFY(!repo); 0060 repo = new Repository; 0061 QVERIFY(repo); 0062 } 0063 0064 void TestRepository::cleanup() 0065 { 0066 QVERIFY(repo); 0067 delete repo; 0068 QVERIFY(!repo); 0069 0070 TestBase::cleanup(); 0071 } 0072 0073 void TestRepository::testRemoteUrlChanging() 0074 { 0075 repo->init(testdir); 0076 0077 const QString remoteName("origin"); 0078 repo->remoteAdd(remoteName, HttpRemoteUrl); 0079 repo->remoteAdd(remoteName, GitRemoteUrl, true); 0080 0081 QScopedPointer<Remote> remote(repo->remote(remoteName)); 0082 QCOMPARE(remote->url(), GitRemoteUrl); 0083 } 0084 0085 void TestRepository::testLookingUpRevision() 0086 { 0087 repo->open(ExistingRepository); 0088 0089 Object object = repo->lookupRevision("HEAD^{tree}"); 0090 QCOMPARE(Object::TreeType, object.type()); 0091 } 0092 0093 void TestRepository::testCreateBranch() 0094 { 0095 initTestRepo(); 0096 repo->open(testdir); 0097 0098 OId head = repo->head().target(); 0099 QCOMPARE(repo->createBranch(branchName).target(), head); 0100 QCOMPARE(repo->lookupShorthandRef(branchName).target(), head); 0101 } 0102 0103 void TestRepository::testDeleteBranch() 0104 { 0105 initTestRepo(); 0106 repo->open(testdir); 0107 repo->createBranch(branchName); 0108 0109 repo->deleteBranch(branchName); 0110 EXPECT_THROW(repo->lookupShorthandRef(branchName), Exception); 0111 } 0112 0113 void TestRepository::testShouldIgnore() 0114 { 0115 initTestRepo(); 0116 repo->open(testdir); 0117 0118 const QString ignoredFileName("Makefile"); 0119 const QString includedFileName("notignored.txt"); 0120 0121 // Relative paths 0122 QVERIFY(repo->shouldIgnore(ignoredFileName)); 0123 QVERIFY(!repo->shouldIgnore(includedFileName)); 0124 0125 QDir dir(testdir); 0126 QString testDirName(dir.dirName()); 0127 0128 // Absolute paths 0129 QVERIFY(repo->shouldIgnore(dir.absoluteFilePath(ignoredFileName))); 0130 QVERIFY(!repo->shouldIgnore(dir.absoluteFilePath(includedFileName))); 0131 0132 dir.cdUp(); 0133 0134 // Path containing .. but still leading up to the repository 0135 QVERIFY(!repo->shouldIgnore(dir.absoluteFilePath(dir.absolutePath() + "/../" + dir.dirName() + "/" + testDirName + "/" + includedFileName))); 0136 0137 // Absolute path outside the repository 0138 EXPECT_THROW(repo->shouldIgnore(dir.absoluteFilePath(ignoredFileName)), Exception); 0139 } 0140 0141 namespace LibQGit2 { 0142 0143 bool operator==(const Repository::Identity &lhs, const Repository::Identity &rhs) 0144 { 0145 return lhs.name == rhs.name && lhs.email == rhs.email; 0146 } 0147 0148 } 0149 0150 void TestRepository::testIdentitySetting() 0151 { 0152 const Repository::Identity id{"name", "email"}; 0153 repo->init(testdir); 0154 repo->setIdentity(id); 0155 QCOMPARE(repo->identity(), id); 0156 } 0157 0158 QTEST_MAIN(TestRepository) 0159 0160 #include "Repository.moc"