~ubuntu-branches/ubuntu/quantal/kdevplatform/quantal-proposed

« back to all changes in this revision

Viewing changes to plugins/git/tests/initTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-24 00:06:18 UTC
  • mfrom: (0.3.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20101024000618-7otebin77mfcmt3b
Tags: 1.1.0-0ubuntu1
* New upstream release
  - Bump build-dependencies
  - Build against libboost-serialization1.42-dev
  - Update kdevplatform1-libs.install
  - Update kdevplatform-dev.install

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   This file was partly taken from KDevelop's cvs plugin                 *
 
3
 *   Copyright 2007 Robert Gruber <rgruber@users.sourceforge.net>          *
 
4
 *                                                                         *
 
5
 *   Adapted for Git                                                       *
 
6
 *   Copyright 2008 Evgeniy Ivanov <powerfox@kde.ru>                       *
 
7
 *                                                                         *
 
8
 *   This program is free software; you can redistribute it and/or         *
 
9
 *   modify it under the terms of the GNU General Public License as        *
 
10
 *   published by the Free Software Foundation; either version 2 of        *
 
11
 *   the License or (at your option) version 3 or any later version        *
 
12
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 
13
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 
14
 *   defined in Section 14 of version 3 of the license.                    *
 
15
 *                                                                         *
 
16
 *   This program is distributed in the hope that it will be useful,       *
 
17
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
18
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
19
 *   GNU General Public License for more details.                          *
 
20
 *                                                                         *
 
21
 *   You should have received a copy of the GNU General Public License     *
 
22
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 
23
 ***************************************************************************/
 
24
 
 
25
#include "initTest.h"
 
26
 
 
27
#include <qtest_kde.h>
 
28
#include <QtTest/QtTest>
 
29
#include <tests/testcore.h>
 
30
#include <tests/autotestshell.h>
 
31
#include <KUrl>
 
32
#include <KDebug>
 
33
#include <kio/netaccess.h>
 
34
 
 
35
#include <vcs/dvcs/dvcsjob.h>
 
36
#include "../gitplugin.h"
 
37
 
 
38
#define VERIFYJOB(j) \
 
39
QVERIFY(j); QVERIFY(j->exec()); QVERIFY((j)->status() == KDevelop::VcsJob::JobSucceeded)
 
40
 
 
41
const QString tempDir = QDir::tempPath();
 
42
const QString gitTest_BaseDir(tempDir + "/kdevGit_testdir/");
 
43
const QString gitTest_BaseDir2(tempDir + "/kdevGit_testdir2/");
 
44
const QString gitRepo(gitTest_BaseDir + ".git");
 
45
const QString gitSrcDir(gitTest_BaseDir + "src/");
 
46
const QString gitTest_FileName("testfile");
 
47
const QString gitTest_FileName2("foo");
 
48
const QString gitTest_FileName3("bar");
 
49
 
 
50
using namespace KDevelop;
 
51
 
 
52
void GitInitTest::initTestCase()
 
53
{
 
54
    AutoTestShell::init();
 
55
    m_testCore = new KDevelop::TestCore();
 
56
    m_testCore->initialize(KDevelop::Core::NoUi);
 
57
    m_plugin = new GitPlugin(m_testCore);
 
58
    removeTempDirs();
 
59
 
 
60
    // Now create the basic directory structure
 
61
    QDir tmpdir(tempDir);
 
62
    tmpdir.mkdir(gitTest_BaseDir);
 
63
    tmpdir.mkdir(gitSrcDir);
 
64
    tmpdir.mkdir(gitTest_BaseDir2);
 
65
}
 
66
 
 
67
void GitInitTest::cleanupTestCase()
 
68
{
 
69
    delete m_plugin;
 
70
    m_testCore->cleanup();
 
71
    delete m_testCore;
 
72
    if (QFileInfo(gitTest_BaseDir).exists())
 
73
        KIO::NetAccess::del(KUrl(gitTest_BaseDir), 0);
 
74
 
 
75
    if (QFileInfo(gitTest_BaseDir2).exists())
 
76
        KIO::NetAccess::del(KUrl(gitTest_BaseDir2), 0);
 
77
}
 
78
 
 
79
void GitInitTest::repoInit()
 
80
{
 
81
    kDebug() << "Trying to init repo";
 
82
    // make job that creates the local repository
 
83
    VcsJob* j = m_plugin->init(KUrl(gitTest_BaseDir));
 
84
    VERIFYJOB(j);
 
85
 
 
86
    //check if the CVSROOT directory in the new local repository exists now
 
87
    QVERIFY(QFileInfo(gitRepo).exists());
 
88
 
 
89
    //check if isValidDirectory works
 
90
    QVERIFY(m_plugin->isValidDirectory(KUrl(gitTest_BaseDir)));
 
91
    //and for non-git dir, I hope nobody has /tmp under git
 
92
    QVERIFY(!m_plugin->isValidDirectory(KUrl("/tmp")));
 
93
 
 
94
    //we have nothing, so ouput should be empty
 
95
    DVcsJob * j2 = m_plugin->gitRevParse(gitRepo, QStringList(QString("--branches")));
 
96
    QVERIFY(j2);
 
97
    QVERIFY(j2->exec());
 
98
    QString out = j2->output();
 
99
    QVERIFY(j2->output().isEmpty());
 
100
}
 
101
 
 
102
void GitInitTest::addFiles()
 
103
{
 
104
    kDebug() << "Adding files to the repo";
 
105
 
 
106
    //we start it after repoInit, so we still have empty git repo
 
107
    QFile f(gitTest_BaseDir + gitTest_FileName);
 
108
 
 
109
    if (f.open(QIODevice::WriteOnly)) {
 
110
        QTextStream input(&f);
 
111
        input << "HELLO WORLD";
 
112
    }
 
113
 
 
114
    f.close();
 
115
    f.setFileName(gitTest_BaseDir + gitTest_FileName2);
 
116
 
 
117
    if (f.open(QIODevice::WriteOnly)) {
 
118
        QTextStream input(&f);
 
119
        input << "No, bar()!";
 
120
    }
 
121
 
 
122
    f.close();
 
123
 
 
124
    //test git-status exitCode (see DVcsJob::setExitCode).
 
125
    VcsJob* j = m_plugin->status(KUrl::List(gitTest_BaseDir));
 
126
    VERIFYJOB(j);
 
127
 
 
128
    // /tmp/kdevGit_testdir/ and testfile
 
129
    j = m_plugin->add(KUrl::List(gitTest_BaseDir + gitTest_FileName));
 
130
    VERIFYJOB(j);
 
131
 
 
132
    f.setFileName(gitSrcDir + gitTest_FileName3);
 
133
 
 
134
    if (f.open(QIODevice::WriteOnly)) {
 
135
        QTextStream input(&f);
 
136
        input << "No, foo()! It's bar()!";
 
137
    }
 
138
 
 
139
    f.close();
 
140
 
 
141
    //test git-status exitCode again
 
142
    j = m_plugin->status(KUrl::List(gitTest_BaseDir));
 
143
    VERIFYJOB(j);
 
144
 
 
145
    //repository path without trailing slash and a file in a parent directory
 
146
    // /tmp/repo  and /tmp/repo/src/bar
 
147
    j = m_plugin->add(KUrl::List(QStringList(gitSrcDir + gitTest_FileName3)));
 
148
    VERIFYJOB(j);
 
149
 
 
150
    //let's use absolute path, because it's used in ContextMenus
 
151
    j = m_plugin->add(KUrl::List(QStringList(gitTest_BaseDir + gitTest_FileName2)));
 
152
    VERIFYJOB(j);
 
153
 
 
154
    //Now let's create several files and try "git add file1 file2 file3"
 
155
    f.setFileName(gitTest_BaseDir + "file1");
 
156
 
 
157
    if (f.open(QIODevice::WriteOnly)) {
 
158
        QTextStream input(&f);
 
159
        input << "file1";
 
160
    }
 
161
 
 
162
    f.close();
 
163
    f.setFileName(gitTest_BaseDir + "file2");
 
164
 
 
165
    if (f.open(QIODevice::WriteOnly)) {
 
166
        QTextStream input(&f);
 
167
        input << "file2";
 
168
    }
 
169
    f.close();
 
170
    
 
171
    KUrl::List multipleFiles;
 
172
    multipleFiles << (gitTest_BaseDir + "file1");
 
173
    multipleFiles << (gitTest_BaseDir + "file2");
 
174
    j = m_plugin->add(multipleFiles);
 
175
    VERIFYJOB(j);
 
176
}
 
177
 
 
178
void GitInitTest::commitFiles()
 
179
{
 
180
    kDebug() << "Committing...";
 
181
    //we start it after addFiles, so we just have to commit
 
182
    VcsJob* j = m_plugin->commit(QString("Test commit"), KUrl::List(gitTest_BaseDir));
 
183
    VERIFYJOB(j);
 
184
 
 
185
    //test git-status exitCode one more time.
 
186
    j = m_plugin->status(KUrl::List(gitTest_BaseDir));
 
187
    VERIFYJOB(j);
 
188
 
 
189
    //since we commited the file to the "pure" repository, .git/refs/heads/master should exist
 
190
    //TODO: maybe other method should be used
 
191
    QString headRefName(gitRepo + "/refs/heads/master");
 
192
    QVERIFY(QFileInfo(headRefName).exists());
 
193
 
 
194
    //Test the results of the "git add"
 
195
    DVcsJob* jobLs = new DVcsJob(gitTest_BaseDir, m_plugin);
 
196
    *jobLs << "git" << "ls-tree" << "--name-only" << "-r" << "HEAD";
 
197
 
 
198
    if (jobLs->exec() && jobLs->status() == KDevelop::VcsJob::JobSucceeded) {
 
199
        QStringList files = jobLs->output().split("\n");
 
200
        QVERIFY(files.contains(gitTest_FileName));
 
201
        QVERIFY(files.contains(gitTest_FileName2));
 
202
        QVERIFY(files.contains("src/" + gitTest_FileName3));
 
203
    }
 
204
 
 
205
    QString firstCommit;
 
206
 
 
207
    QFile headRef(headRefName);
 
208
 
 
209
    if (headRef.open(QIODevice::ReadOnly)) {
 
210
        QTextStream output(&headRef);
 
211
        output >> firstCommit;
 
212
    }
 
213
    headRef.close();
 
214
 
 
215
    QVERIFY(!firstCommit.isEmpty());
 
216
 
 
217
    kDebug() << "Committing one more time";
 
218
    //let's try to change the file and test "git commit -a"
 
219
    QFile f(gitTest_BaseDir + gitTest_FileName);
 
220
 
 
221
    if (f.open(QIODevice::WriteOnly)) {
 
222
        QTextStream input(&f);
 
223
        input << "Just another HELLO WORLD";
 
224
    }
 
225
 
 
226
    f.close();
 
227
 
 
228
    //add changes
 
229
    j = m_plugin->add(KUrl::List(QStringList(gitTest_BaseDir + gitTest_FileName)));
 
230
    VERIFYJOB(j);
 
231
 
 
232
    j = m_plugin->commit(QString("KDevelop's Test commit2"), KUrl::List(gitTest_BaseDir));
 
233
    VERIFYJOB(j);
 
234
 
 
235
    QString secondCommit;
 
236
 
 
237
    if (headRef.open(QIODevice::ReadOnly)) {
 
238
        QTextStream output(&headRef);
 
239
        output >> secondCommit;
 
240
    }
 
241
    headRef.close();
 
242
 
 
243
    QVERIFY(!secondCommit.isEmpty());
 
244
    QVERIFY(firstCommit != secondCommit);
 
245
 
 
246
}
 
247
 
 
248
// void GitInitTest::cloneRepository()
 
249
// {
 
250
//     kDebug() << "Do not clone people, clone Git repos!";
 
251
//     // make job that clones the local repository, created in the previous test
 
252
//     DVcsJob* j = m_proxy->createWorkingCopy(KUrl(gitTest_BaseDir), KUrl(gitTest_BaseDir2));
 
253
//     QVERIFY( j );
 
254
//
 
255
//     // try to start the job
 
256
//     QVERIFY( j->exec() );
 
257
//
 
258
//     //check if the .git directory in the new local repository exists now
 
259
//     QVERIFY( QFileInfo(QString(gitTest_BaseDir2"kdevGit_testdir/.git/")).exists() );
 
260
// }
 
261
 
 
262
void GitInitTest::testInit()
 
263
{
 
264
    repoInit();
 
265
}
 
266
 
 
267
void GitInitTest::testAdd()
 
268
{
 
269
    addFiles();
 
270
}
 
271
 
 
272
void GitInitTest::testCommit()
 
273
{
 
274
    commitFiles();
 
275
}
 
276
 
 
277
void GitInitTest::testBranching()
 
278
{
 
279
    DVcsJob* j = m_plugin->branch(gitTest_BaseDir);
 
280
    VERIFYJOB(j);
 
281
 
 
282
    QString curBranch = m_plugin->curBranch(gitTest_BaseDir);
 
283
    QCOMPARE(curBranch, QString("master"));
 
284
 
 
285
    QString newBranch("new");
 
286
    j = m_plugin->branch(gitTest_BaseDir, QString("master"), newBranch);
 
287
    VERIFYJOB(j);
 
288
    QVERIFY(m_plugin->branches(gitTest_BaseDir).contains(newBranch));
 
289
 
 
290
    j = m_plugin->switchBranch(gitTest_BaseDir, newBranch);
 
291
    VERIFYJOB(j);
 
292
    QCOMPARE(m_plugin->curBranch(gitTest_BaseDir), newBranch);
 
293
 
 
294
    j = m_plugin->branch(gitTest_BaseDir, QString("master"), QString(), QStringList("-D"));
 
295
    VERIFYJOB(j);
 
296
    QVERIFY(!m_plugin->branches(gitTest_BaseDir).contains(QString("master")));
 
297
}
 
298
 
 
299
void GitInitTest::revHistory()
 
300
{
 
301
    QList<DVcsEvent> commits = m_plugin->getAllCommits(gitTest_BaseDir);
 
302
    QVERIFY(!commits.isEmpty());
 
303
    QStringList logMessages;
 
304
 
 
305
    for (int i = 0; i < commits.count(); ++i)
 
306
        logMessages << commits[i].getLog();
 
307
 
 
308
    QCOMPARE(commits.count(), 2);
 
309
 
 
310
    QCOMPARE(logMessages[0], QString("KDevelop's Test commit2"));  //0 is later than 1!
 
311
 
 
312
    QCOMPARE(logMessages[1], QString("Test commit"));
 
313
 
 
314
    QVERIFY(commits[1].getParents().isEmpty());  //0 is later than 1!
 
315
 
 
316
    QVERIFY(!commits[0].getParents().isEmpty()); //initial commit is on the top
 
317
 
 
318
    QVERIFY(commits[1].getCommit().contains(QRegExp("^\\w{,40}$")));
 
319
 
 
320
    QVERIFY(commits[0].getCommit().contains(QRegExp("^\\w{,40}$")));
 
321
 
 
322
    QVERIFY(commits[0].getParents()[0].contains(QRegExp("^\\w{,40}$")));
 
323
}
 
324
 
 
325
void GitInitTest::removeTempDirs()
 
326
{
 
327
    if (QFileInfo(gitTest_BaseDir).exists())
 
328
        if (!KIO::NetAccess::del(KUrl(gitTest_BaseDir), 0))
 
329
            qDebug() << "KIO::NetAccess::del(" << gitTest_BaseDir << ") returned false";
 
330
 
 
331
    if (QFileInfo(gitTest_BaseDir2).exists())
 
332
        if (!KIO::NetAccess::del(KUrl(gitTest_BaseDir2), 0))
 
333
            qDebug() << "KIO::NetAccess::del(" << gitTest_BaseDir2 << ") returned false";
 
334
}
 
335
 
 
336
QTEST_KDEMAIN(GitInitTest, GUI)
 
337
 
 
338
// #include "gittest.moc"