File indexing completed on 2024-05-12 04:41:04

0001 /*
0002     SPDX-FileCopyrightText: 2009 Fabian Wiesel <fabian.wiesel@googlemail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "test_svnrecursiveadd.h"
0008 
0009 #include <QDebug>
0010 #include <QTemporaryDir>
0011 #include <QTest>
0012 
0013 #include <KProcess>
0014 
0015 #include <interfaces/iplugincontroller.h>
0016 #include <tests/autotestshell.h>
0017 #include <tests/testcore.h>
0018 #include <vcs/interfaces/icentralizedversioncontrol.h>
0019 #include <vcs/vcsjob.h>
0020 #include <vcs/vcslocation.h>
0021 
0022 #define PATHETIC    // A little motivator to make things work right :)
0023 #if defined(PATHETIC)
0024 inline QString vcsTestDir0() { return QStringLiteral("testdir0"); }
0025 inline QString vcsTestDir1() { return QStringLiteral("testdir1"); }
0026 inline QString vcsTest_FileName0() { return QStringLiteral("foo"); }
0027 inline QString vcsTest_FileName1() { return QStringLiteral("bar"); }
0028 inline QString keywordText() { return QStringLiteral("text"); }
0029 #else
0030 inline QString vcsTestDir0() { return QStringLiteral("dvcs\t testdir"); }   // Directory containing whitespaces
0031 inline QString vcsTestDir1() { return QStringLiteral("--help"); }           // Starting with hyphen for command-line tools
0032 inline QString vcsTest_FileName0() { return QStringLiteral("foo\t bar"); }
0033 inline QString vcsTest_FileName1() { return QStringLiteral("--help"); }
0034 inline QString keywordText() { return QStringLiteral("Author:\nDate:\nCommit:\n------------------------------------------------------------------------\nr999999 | ehrman | 1989-11-09 18:53:00 +0100 (Thu, 09 Nov 1989) | 1 lines\nthe line\n"); }  // Text containing keywords of the various vcs-programs
0035 #endif
0036 
0037 inline QString simpleText() { return QStringLiteral("It's foo!\n"); }
0038 inline QString simpleAltText() { return QStringLiteral("No, foo()! It's bar()!\n"); }
0039 
0040 #define VERBOSE
0041 #if defined(VERBOSE)
0042 #define TRACE(X) qDebug() << X
0043 #else
0044 #define TRACE(X) { line = line; }
0045 #endif
0046 
0047 using namespace KDevelop;
0048 
0049 void validatingExecJob(VcsJob* j, VcsJob::JobStatus status = VcsJob::JobSucceeded)
0050 {
0051     QVERIFY(j);
0052     // Print the commands in full, for easier bug location
0053 #if 0
0054     if (QLatin1String(j->metaObject()->className()) == "DVcsJob") {
0055         qDebug() << "Command: \"" << ((DVcsJob*)j)->getChildproc()->program() << ((DVcsJob*)j)->getChildproc()->workingDirectory();
0056         qDebug() << "Output: \"" << ((DVcsJob*)j)->output();
0057     }
0058 #endif
0059 
0060     if (!j->exec()) {
0061         qDebug() << "ooops, no exec";
0062         qDebug() << j->errorString();
0063         // On error, wait for key in order to allow manual state inspection
0064 #if 0
0065         char c;
0066         std::cin.read(&c, 1);
0067 #endif
0068     }
0069 
0070     QCOMPARE(j->status(), status);
0071 }
0072 
0073 void verifiedWrite(QUrl const & url, QString const & contents)
0074 {
0075     QFile f(url.path());
0076     QVERIFY(f.open(QIODevice::WriteOnly));
0077     QTextStream filecontents(&f);
0078     filecontents << contents;
0079     filecontents.flush();
0080     f.flush();
0081 }
0082 
0083 
0084 void fillWorkingDirectory(QString const & dirname)
0085 {
0086     QDir dir(dirname);
0087     //we start it after repoInit, so we still have empty dvcs repo
0088     QVERIFY(dir.mkdir(vcsTestDir0()));
0089     QVERIFY(dir.cd(vcsTestDir0()));
0090     QUrl file0 = QUrl::fromLocalFile(dir.absoluteFilePath(vcsTest_FileName0()));
0091     QVERIFY(dir.mkdir(vcsTestDir1()));
0092     QVERIFY(dir.cd(vcsTestDir1()));
0093     QUrl file1 = QUrl::fromLocalFile(dir.absoluteFilePath(vcsTest_FileName1()));
0094     verifiedWrite(file0, simpleText());
0095     verifiedWrite(file1, keywordText());
0096 }
0097 
0098 void TestSvnRecursiveAdd::initTestCase()
0099 {
0100     AutoTestShell::init({"kdevsubversion", "KDevStandardOutputView"});
0101     TestCore::initialize();
0102 }
0103 
0104 void TestSvnRecursiveAdd::cleanupTestCase()
0105 {
0106     TestCore::shutdown();
0107 }
0108 
0109 void TestSvnRecursiveAdd::test()
0110 {
0111     QTemporaryDir reposDir;
0112     KProcess cmd;
0113     cmd.setWorkingDirectory(reposDir.path());
0114     cmd << QStringLiteral("svnadmin") << QStringLiteral("create") << reposDir.path();
0115     QCOMPARE(cmd.execute(10000), 0);
0116     const QList<IPlugin*> plugins = Core::self()->pluginController()->allPluginsForExtension(QStringLiteral("org.kdevelop.IBasicVersionControl"));
0117     IBasicVersionControl* vcs = nullptr;
0118     for (IPlugin* p : plugins) {
0119         qDebug() << "checking plugin" << p;
0120         auto* icentr = p->extension<ICentralizedVersionControl>();
0121         if (!icentr)
0122             continue;
0123         if (icentr->name() == QLatin1String("Subversion")) {
0124             vcs = icentr;
0125             break;
0126         }
0127     }
0128     qDebug() << "ok, got vcs" << vcs;
0129     QVERIFY(vcs);
0130     VcsLocation reposLoc;
0131     reposLoc.setRepositoryServer("file://" + reposDir.path());
0132     QTemporaryDir checkoutDir;
0133     QUrl checkoutLoc = QUrl::fromLocalFile(checkoutDir.path());
0134     qDebug() << "Checking out from " << reposLoc.repositoryServer() << " to " << checkoutLoc;
0135     qDebug() << "creating job";
0136     VcsJob* job = vcs->createWorkingCopy( reposLoc, checkoutLoc );
0137     validatingExecJob(job);
0138     qDebug() << "filling wc";
0139     fillWorkingDirectory(checkoutDir.path());
0140     QUrl addUrl = QUrl::fromLocalFile( checkoutDir.path() + '/' + vcsTestDir0() );
0141     qDebug() << "Recursively adding files at " << addUrl;
0142     validatingExecJob(vcs->add({addUrl}, IBasicVersionControl::Recursive));
0143     qDebug() << "Recursively reverting changes at " << addUrl;
0144     validatingExecJob(vcs->revert({addUrl}, IBasicVersionControl::Recursive));
0145 }
0146 
0147 QTEST_MAIN(TestSvnRecursiveAdd)
0148 
0149 #include "moc_test_svnrecursiveadd.cpp"