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_svnimport.h"
0008 
0009 #include <QTest>
0010 #include <QDebug>
0011 #include <QLoggingCategory>
0012 
0013 #include <QTemporaryDir>
0014 #include <KProcess>
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 VERBOSE
0023 #if defined(VERBOSE)
0024 #define TRACE(X) qDebug() << X
0025 #else
0026 #define TRACE(X) { line = line; }
0027 #endif
0028 
0029 using namespace KDevelop;
0030 
0031 void validatingExecJob(VcsJob* j, VcsJob::JobStatus status = VcsJob::JobSucceeded)
0032 {
0033     QVERIFY(j);
0034 
0035     if (!j->exec()) {
0036         qDebug() << j->errorString();
0037         // On error, wait for key in order to allow manual state inspection
0038     }
0039     QCOMPARE(j->status(), status);
0040 }
0041 
0042 void setupLocalRepository( const QString& name, VcsLocation & reposLoc )
0043 {
0044     KProcess cmd;
0045     cmd.setWorkingDirectory(name);
0046     cmd << QStringLiteral("svnadmin") << QStringLiteral("create") << name;
0047     QCOMPARE(cmd.execute(10000), 0);
0048 
0049     reposLoc.setRepositoryServer("file://" + name );
0050 }
0051 
0052 void setupSampleProject( const QString& name, const QString& content )
0053 {
0054     QFile sampleFile( name + "/sample.file" );
0055     sampleFile.open( QIODevice::WriteOnly );
0056     sampleFile.write( content.toUtf8() );
0057     sampleFile.close();
0058 }
0059 
0060 void TestSvnImport::initTestCase()
0061 {
0062     QLoggingCategory::setFilterRules(QStringLiteral("*.debug=false\ndefault.debug=true\nkdevelop.plugins.svn.debug=true\n"));
0063     AutoTestShell::init({QStringLiteral("kdevsubversion"), QStringLiteral("KDevStandardOutputView")});
0064     TestCore::initialize();
0065 
0066     const QList<IPlugin*> plugins = Core::self()->pluginController()->allPluginsForExtension(QStringLiteral("org.kdevelop.IBasicVersionControl"));
0067     for (IPlugin* p : plugins) {
0068         qDebug() << "checking plugin" << p;
0069         auto* icentr = p->extension<ICentralizedVersionControl>();
0070         if (!icentr)
0071             continue;
0072         if (icentr->name() == QLatin1String("Subversion")) {
0073             vcs = icentr;
0074             break;
0075         }
0076     }
0077     qDebug() << "ok, got vcs" << vcs;
0078     QVERIFY(vcs);
0079 }
0080 
0081 void TestSvnImport::cleanupTestCase()
0082 {
0083     TestCore::shutdown();
0084 }
0085 
0086 void TestSvnImport::testBasic()
0087 {
0088     QTemporaryDir reposDir;
0089     VcsLocation reposLoc;
0090     setupLocalRepository( reposDir.path(), reposLoc );
0091 
0092     QTemporaryDir projectDir;
0093     QString origcontent = QStringLiteral("This is a Test");
0094     setupSampleProject( projectDir.path(), origcontent );
0095 
0096     VcsJob* job = vcs->import( QStringLiteral("import test"), QUrl::fromLocalFile( projectDir.path() ), reposLoc );
0097     validatingExecJob(job);
0098 
0099     QTemporaryDir checkoutDir;
0100     validateImport( reposLoc.repositoryServer(), checkoutDir, origcontent );
0101 }
0102 
0103 void TestSvnImport::testImportWithMissingDirs()
0104 {
0105     QTemporaryDir reposDir;
0106     VcsLocation reposLoc;
0107     setupLocalRepository( reposDir.path(), reposLoc );
0108 
0109     QTemporaryDir projectDir;
0110     QString origcontent = QStringLiteral("This is a Test");
0111     setupSampleProject( projectDir.path(), origcontent );
0112 
0113     reposLoc.setRepositoryServer( reposLoc.repositoryServer() + "/foobar/" + QDir( projectDir.path() ).dirName() );
0114     VcsJob* job = vcs->import( QStringLiteral("import test"), QUrl::fromLocalFile( projectDir.path() ), reposLoc );
0115     validatingExecJob(job);
0116 
0117     QTemporaryDir checkoutDir;
0118     validateImport( reposLoc.repositoryServer(), checkoutDir, origcontent );
0119 }
0120 
0121 void TestSvnImport::testImportIntoDir()
0122 {
0123     QTemporaryDir reposDir;
0124     VcsLocation reposLoc;
0125     setupLocalRepository( reposDir.path(), reposLoc );
0126 
0127     QTemporaryDir projectDir;
0128     QString origcontent = QStringLiteral("This is a Test");
0129     setupSampleProject( projectDir.path(), origcontent );
0130 
0131     reposLoc.setRepositoryServer( reposLoc.repositoryServer() + '/' + QDir( projectDir.path() ).dirName() );
0132     VcsJob* job = vcs->import( QStringLiteral("import test"), QUrl::fromLocalFile( projectDir.path() ), reposLoc );
0133     validatingExecJob(job);
0134 
0135     QTemporaryDir checkoutDir;
0136     validateImport( reposLoc.repositoryServer(), checkoutDir, origcontent );
0137 }
0138 
0139 void TestSvnImport::validateImport( const QString& repourl, QTemporaryDir& checkoutdir, const QString& origcontent )
0140 {
0141     VcsLocation reposLoc;
0142     reposLoc.setRepositoryServer( repourl );
0143     VcsJob* job = vcs->createWorkingCopy( reposLoc, QUrl::fromLocalFile(checkoutdir.path()) );
0144     validatingExecJob(job);
0145 
0146     QFile newfile( checkoutdir.path() + "/sample.file" );
0147     QVERIFY(newfile.exists());
0148     QVERIFY(newfile.open(QIODevice::ReadOnly));
0149     QCOMPARE(QString::fromUtf8( newfile.readAll() ), origcontent);
0150 }
0151 
0152 QTEST_MAIN(TestSvnImport)
0153 
0154 #include "moc_test_svnimport.cpp"