File indexing completed on 2024-05-19 15:46:09

0001 /*
0002     This file was inspired by KDevelop's git plugin
0003     SPDX-FileCopyrightText: 2008 Evgeniy Ivanov <powerfox@kde.ru>
0004 
0005     Adapted for Perforce
0006     SPDX-FileCopyrightText: 2011 Morten Danielsen Volden <mvolden2@gmail.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "test_perforce.h"
0012 
0013 #include <QTest>
0014 #include <QDir>
0015 #include <QDirIterator>
0016 #include <QStandardPaths>
0017 
0018 #include <tests/autotestshell.h>
0019 #include <tests/testcore.h>
0020 
0021 #include <vcs/vcsjob.h>
0022 #include <vcs/vcsannotation.h>
0023 
0024 #include <perforceplugin.h>
0025 
0026 #define VERIFYJOB(j) \
0027     QVERIFY(j); QVERIFY(j->exec()); QVERIFY((j)->status() == VcsJob::JobSucceeded)
0028 
0029 using namespace KDevelop;
0030 
0031 PerforcePluginTest::PerforcePluginTest()
0032     : tempDir{QDir::tempPath()}
0033     , perforceTestBaseDirNoSlash{tempDir + QLatin1String("/kdevPerforce_testdir")}
0034     , perforceTestBaseDir{perforceTestBaseDirNoSlash + QLatin1Char('/')}
0035     , perforceConfigFileName{QStringLiteral("p4config.txt")}
0036     , perforceSrcDir{perforceTestBaseDir + QLatin1String("src/")}
0037     , perforceTest_FileName{QStringLiteral("testfile")}
0038     , perforceTest_FileName2{QStringLiteral("foo")}
0039     , perforceTest_FileName3{QStringLiteral("bar")}
0040 {
0041 }
0042 
0043 void PerforcePluginTest::initTestCase()
0044 {
0045     AutoTestShell::init({QStringLiteral("kdevperforce")});
0046     TestCore::initialize();
0047     m_plugin = new PerforcePlugin(TestCore::self());
0048 
0049     /// During test we are setting the executable the plugin uses to our own stub
0050     QDirIterator it(QString::fromUtf8(P4_BINARY_DIR), QStringList() << QStringLiteral("*"),
0051                     QDir::Dirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
0052     QStringList pathsToSearch;
0053     while (it.hasNext()) {
0054         it.next();
0055         pathsToSearch << it.filePath();
0056     }
0057     QString p4stubPath = QStandardPaths::findExecutable(QStringLiteral("p4clientstub"), pathsToSearch);
0058     qDebug() << "found p4stub executable :" << p4stubPath;
0059     QVERIFY(!p4stubPath.isEmpty());
0060 
0061     m_plugin->m_perforceExecutable = p4stubPath;
0062 }
0063 
0064 void PerforcePluginTest::cleanupTestCase()
0065 {
0066     delete m_plugin;
0067     TestCore::shutdown();
0068 }
0069 
0070 
0071 void PerforcePluginTest::init()
0072 {
0073     removeTempDirsIfAny();
0074     createNewTempDirs();
0075 }
0076 
0077 void PerforcePluginTest::createNewTempDirs()
0078 {
0079     // Now create the basic directory structure
0080     QDir tmpdir(tempDir);
0081     tmpdir.mkdir(perforceTestBaseDir);
0082     //we start it after repoInit, so we still have empty repo
0083     QFile f(perforceTestBaseDir + perforceConfigFileName);
0084     if (f.open(QIODevice::WriteOnly)) {
0085         QTextStream input(&f);
0086         input << "P4PORT=127.0.0.1:1666\n";
0087         input << "P4USER=mvo\n";
0088         input << "P4CLIENT=testbed\n";
0089     }
0090     f.close();
0091 
0092     //Put a file here because the annotate and update function will check for that
0093     QFile g(perforceTestBaseDir + perforceTest_FileName);
0094     if (g.open(QIODevice::WriteOnly)) {
0095         QTextStream input(&g);
0096         input << "HELLO WORLD";
0097     }
0098     g.close();
0099 
0100     tmpdir.mkdir(perforceSrcDir);
0101 }
0102 
0103 
0104 void PerforcePluginTest::removeTempDirsIfAny()
0105 {
0106     QDir dir(perforceTestBaseDir);
0107     if (dir.exists() && !dir.removeRecursively())
0108         qDebug() << "QDir::removeRecursively(" << perforceTestBaseDir << ") returned false";
0109 }
0110 
0111 
0112 void PerforcePluginTest::cleanup()
0113 {
0114     removeTempDirsIfAny();
0115 }
0116 
0117 void PerforcePluginTest::testAdd()
0118 {
0119     VcsJob* j = m_plugin->add(QList<QUrl>({ QUrl::fromLocalFile(perforceTestBaseDir + perforceTest_FileName) } ));
0120     VERIFYJOB(j);
0121 }
0122 
0123 void PerforcePluginTest::testEdit()
0124 {
0125     VcsJob* j = m_plugin->edit(QList<QUrl>( { QUrl::fromLocalFile(perforceTestBaseDir + perforceTest_FileName) } ));
0126     VERIFYJOB(j);
0127 }
0128 
0129 void PerforcePluginTest::testEditMultipleFiles()
0130 {
0131     QList<QUrl> filesForEdit;
0132     filesForEdit.push_back(QUrl::fromLocalFile(perforceTestBaseDir + perforceTest_FileName));
0133     filesForEdit.push_back(QUrl::fromLocalFile(perforceTestBaseDir + perforceTest_FileName2));
0134     filesForEdit.push_back(QUrl::fromLocalFile(perforceTestBaseDir + perforceTest_FileName3));
0135     VcsJob* j = m_plugin->edit(filesForEdit);
0136     VERIFYJOB(j);
0137 }
0138 
0139 
0140 void PerforcePluginTest::testStatus()
0141 {
0142     VcsJob* j = m_plugin->status(QList<QUrl>( { QUrl::fromLocalFile(perforceTestBaseDirNoSlash) } ));
0143     VERIFYJOB(j);
0144 }
0145 
0146 void PerforcePluginTest::testAnnotate()
0147 {
0148     VcsJob* j = m_plugin->annotate(QUrl( QUrl::fromLocalFile(perforceTestBaseDir + perforceTest_FileName) ));
0149     VERIFYJOB(j);
0150 }
0151 
0152 void PerforcePluginTest::testHistory()
0153 {
0154     VcsJob* j = m_plugin->log(QUrl( QUrl::fromLocalFile(perforceTestBaseDir + perforceTest_FileName) ));
0155     VERIFYJOB(j);
0156 }
0157 
0158 void PerforcePluginTest::testRevert()
0159 {
0160     VcsJob* j = m_plugin->revert(QList<QUrl>( { QUrl::fromLocalFile(perforceTestBaseDir + perforceTest_FileName) } ));
0161     VERIFYJOB(j);
0162 }
0163 
0164 void PerforcePluginTest::testUpdateFile()
0165 {
0166     VcsJob* j = m_plugin->update(QList<QUrl>( { QUrl::fromLocalFile(perforceTestBaseDir + perforceTest_FileName) } ));
0167     VERIFYJOB(j);
0168 }
0169 
0170 void PerforcePluginTest::testUpdateDir()
0171 {
0172     VcsJob* j = m_plugin->update(QList<QUrl>( { QUrl::fromLocalFile(perforceTestBaseDirNoSlash) } ));
0173     VERIFYJOB(j);
0174 }
0175 
0176 void PerforcePluginTest::testCommit()
0177 {
0178     QString commitMsg(QStringLiteral("this is the commit message"));
0179     VcsJob* j = m_plugin->commit(commitMsg, QList<QUrl>( { QUrl::fromLocalFile(perforceTestBaseDirNoSlash) }  ));
0180     VERIFYJOB(j);
0181 }
0182 
0183 void PerforcePluginTest::testDiff()
0184 {
0185     VcsRevision srcRevision;
0186     srcRevision.setRevisionValue(QVariant(1), VcsRevision::GlobalNumber);
0187     VcsRevision dstRevision;
0188     dstRevision.setRevisionValue(QVariant(2), VcsRevision::GlobalNumber);
0189 
0190     VcsJob* j = m_plugin->diff( QUrl::fromLocalFile(perforceTestBaseDir + perforceTest_FileName), srcRevision, dstRevision);
0191     VERIFYJOB(j);
0192 }
0193 
0194 
0195 QTEST_MAIN(PerforcePluginTest)
0196 
0197 #include "moc_test_perforce.cpp"