Commit 6277bdbf authored by Knut Petter Svendsen's avatar Knut Petter Svendsen

ClearCase: Refactor and add tests

Refactored code to make it more testable, and added tests. When running
WITH_TESTS the TestCases will make the plugin fake ClearTool such that
ClearTool is not needed to run the current tests.

Change-Id: I49b50a667309cf337a07ef20dabb4801c680700d
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent 0d37b545
......@@ -57,6 +57,10 @@ Core::Id ClearCaseControl::id() const
bool ClearCaseControl::isConfigured() const
{
#ifdef WITH_TESTS
if (m_plugin->isFakeCleartool())
return true;
#endif
const QString binary = m_plugin->settings().ccBinaryPath;
if (binary.isEmpty())
return false;
......
......@@ -186,6 +186,9 @@ ClearCasePlugin::ClearCasePlugin() :
m_submitActionTriggered(false),
m_activityMutex(new QMutex),
m_statusMap(new StatusMap)
#ifdef WITH_TESTS
,m_fakeClearTool(false)
#endif
{
qRegisterMetaType<ClearCase::Internal::FileStatus::Status>("ClearCase::Internal::FileStatus::Status");
}
......@@ -483,12 +486,14 @@ bool ClearCasePlugin::initialize(const QStringList & /*arguments */, QString *er
clearcaseMenu->addSeparator(globalcontext);
m_diffActivityAction = new QAction(tr("Diff A&ctivity..."), this);
m_diffActivityAction->setEnabled(false);
command = ActionManager::registerAction(m_diffActivityAction, CMD_ID_DIFF_ACTIVITY, globalcontext);
connect(m_diffActivityAction, SIGNAL(triggered()), this, SLOT(diffActivity()));
clearcaseMenu->addAction(command);
m_commandLocator->appendCommand(command);
m_checkInActivityAction = new Utils::ParameterAction(tr("Ch&eck In Activity"), tr("Chec&k In Activity \"%1\"..."), Utils::ParameterAction::EnabledWithParameter, this);
m_checkInActivityAction->setEnabled(false);
command = ActionManager::registerAction(m_checkInActivityAction, CMD_ID_CHECKIN_ACTIVITY, globalcontext);
connect(m_checkInActivityAction, SIGNAL(triggered()), this, SLOT(startCheckInActivity()));
command->setAttribute(Command::CA_UpdateText);
......@@ -1760,7 +1765,13 @@ QString ClearCasePlugin::vcsGetRepositoryURL(const QString & /*directory*/)
///
bool ClearCasePlugin::managesDirectory(const QString &directory, QString *topLevel /* = 0 */) const
{
#ifdef WITH_TESTS
// If running with tests and fake ClearTool is enabled, then pretend we manage every directory
QString topLevelFound = m_fakeClearTool ? directory : findTopLevel(directory);
#else
QString topLevelFound = findTopLevel(directory);
#endif
if (topLevel)
*topLevel = topLevelFound;
return !topLevelFound.isEmpty();
......@@ -2142,6 +2153,166 @@ void ClearCasePlugin::testLogResolving()
"src/plugins/clearcase/clearcaseeditor.h@@/main/branch1/branch2/9",
"src/plugins/clearcase/clearcaseeditor.h@@/main/branch1/branch2/8");
}
void ClearCasePlugin::initTestCase()
{
m_tempFile = QDir::currentPath() + QLatin1String("/cc_file.cpp");
Utils::FileSaver srcSaver(m_tempFile);
srcSaver.write(QByteArray());
srcSaver.finalize();
}
void ClearCasePlugin::cleanupTestCase()
{
QVERIFY(QFile::remove(m_tempFile));
}
void ClearCasePlugin::testFileStatusParsing_data()
{
QTest::addColumn<QString>("filename");
QTest::addColumn<QString>("cleartoolLsLine");
QTest::addColumn<int>("status");
QTest::newRow("CheckedOut")
<< m_tempFile
<< QString(m_tempFile + QLatin1String("@@/main/branch1/CHECKEDOUT from /main/branch1/0 Rule: CHECKEDOUT"))
<< static_cast<int>(FileStatus::CheckedOut);
QTest::newRow("CheckedIn")
<< m_tempFile
<< QString(m_tempFile + QLatin1String("@@/main/9 Rule: MY_LABEL_1.6.4 [-mkbranch branch1]"))
<< static_cast<int>(FileStatus::CheckedIn);
QTest::newRow("Hijacked")
<< m_tempFile
<< QString(m_tempFile + QLatin1String("@@/main/9 [hijacked] Rule: MY_LABEL_1.5.33 [-mkbranch myview1]"))
<< static_cast<int>(FileStatus::Hijacked);
QTest::newRow("Missing")
<< m_tempFile
<< QString(m_tempFile + QLatin1String("@@/main/9 [loaded but missing] Rule: MY_LABEL_1.5.33 [-mkbranch myview1]"))
<< static_cast<int>(FileStatus::Missing);
}
void ClearCasePlugin::testFileStatusParsing()
{
ClearCasePlugin *plugin = ClearCasePlugin::instance();
plugin->m_statusMap = QSharedPointer<StatusMap>(new StatusMap);
QFETCH(QString, filename);
QFETCH(QString, cleartoolLsLine);
QFETCH(int, status);
ClearCaseSync ccSync(plugin, plugin->m_statusMap);
ccSync.verifyParseStatus(filename, cleartoolLsLine, static_cast<FileStatus::Status>(status));
}
void ClearCasePlugin::testFileNotManaged()
{
ClearCasePlugin *plugin = ClearCasePlugin::instance();
plugin->m_statusMap = QSharedPointer<StatusMap>(new StatusMap);
ClearCaseSync ccSync(plugin, plugin->m_statusMap);
ccSync.verifyFileNotManaged();
}
namespace {
/**
* @brief Convenience class which also properly cleans up editors
*/
class TestCase
{
public:
TestCase(const QString &fileName) :
m_fileName(fileName) ,
m_editor(0)
{
ClearCasePlugin::instance()->setFakeCleartool(true);
Utils::FileSaver srcSaver(fileName);
srcSaver.write(QByteArray());
srcSaver.finalize();
m_editor = Core::EditorManager::openEditor(fileName);
QCoreApplication::processEvents(); // process any pending events
}
ViewData dummyViewData() const
{
ViewData viewData;
viewData.name = QLatin1String("fake_view");
viewData.root = QDir::currentPath();
viewData.isUcm = false;
return viewData;
}
~TestCase()
{
Core::EditorManager::closeEditor(m_editor, false);
QCoreApplication::processEvents(); // process any pending events
QVERIFY(QFile::remove(m_fileName));
ClearCasePlugin::instance()->setFakeCleartool(false);
}
private:
QString m_fileName;
Core::IEditor *m_editor;
};
}
void ClearCasePlugin::testStatusActions_data()
{
QTest::addColumn<int>("status");
QTest::addColumn<bool>("checkOutAction");
QTest::addColumn<bool>("undoCheckOutAction");
QTest::addColumn<bool>("undoHijackAction");
QTest::addColumn<bool>("checkInCurrentAction");
QTest::addColumn<bool>("addFileAction");
QTest::addColumn<bool>("checkInActivityAction");
QTest::addColumn<bool>("diffActivityAction");
QTest::newRow("Unknown") << static_cast<int>(FileStatus::Unknown)
<< true << true << true << true << true << false << false;
QTest::newRow("CheckedOut") << static_cast<int>(FileStatus::CheckedOut)
<< false << true << false << true << false << false << false;
QTest::newRow("CheckedIn") << static_cast<int>(FileStatus::CheckedIn)
<< true << false << false << false << false << false << false;
QTest::newRow("NotManaged") << static_cast<int>(FileStatus::NotManaged)
<< false << false << false << false << true << false << false;
}
void ClearCasePlugin::testStatusActions()
{
const QString fileName = QDir::currentPath() + QLatin1String("/clearcase_file.cpp");
TestCase testCase(fileName);
m_viewData = testCase.dummyViewData();
QFETCH(int, status);
FileStatus::Status tempStatus = static_cast<FileStatus::Status>(status);
// special case: file should appear as "Unknown" since there is no entry in the index
// and we don't want to explicitly set the status for this test case
if (tempStatus != FileStatus::Unknown)
setStatus(fileName, tempStatus, true);
QFETCH(bool, checkOutAction);
QFETCH(bool, undoCheckOutAction);
QFETCH(bool, undoHijackAction);
QFETCH(bool, checkInCurrentAction);
QFETCH(bool, addFileAction);
QFETCH(bool, checkInActivityAction);
QFETCH(bool, diffActivityAction);
QCOMPARE(m_checkOutAction->isEnabled(), checkOutAction);
QCOMPARE(m_undoCheckOutAction->isEnabled(), undoCheckOutAction);
QCOMPARE(m_undoHijackAction->isEnabled(), undoHijackAction);
QCOMPARE(m_checkInCurrentAction->isEnabled(), checkInCurrentAction);
QCOMPARE(m_addFileAction->isEnabled(), addFileAction);
QCOMPARE(m_checkInActivityAction->isEnabled(), checkInActivityAction);
QCOMPARE(m_diffActivityAction->isEnabled(), diffActivityAction);
}
#endif
} // namespace Internal
......
......@@ -167,6 +167,10 @@ public:
bool ccCheckUcm(const QString &viewname, const QString &workingDir) const;
bool managesFile(const QString &workingDirectory, const QString &fileName) const;
#ifdef WITH_TESTS
inline void setFakeCleartool(const bool b = true) { m_fakeClearTool = b; }
inline bool isFakeCleartool() const { return m_fakeClearTool; }
#endif
public slots:
void vcsAnnotate(const QString &workingDir, const QString &file,
......@@ -199,9 +203,16 @@ private slots:
void closing();
void updateStatusActions();
#ifdef WITH_TESTS
void initTestCase();
void cleanupTestCase();
void testDiffFileResolving_data();
void testDiffFileResolving();
void testLogResolving();
void testFileStatusParsing_data();
void testFileStatusParsing();
void testFileNotManaged();
void testStatusActions_data();
void testStatusActions();
#endif
protected:
......@@ -282,6 +293,10 @@ private:
QSharedPointer<StatusMap> m_statusMap;
static ClearCasePlugin *m_clearcasePluginInstance;
#ifdef WITH_TESTS
bool m_fakeClearTool;
QString m_tempFile;
#endif
};
} // namespace Internal
......
This diff is collapsed.
......@@ -42,12 +42,25 @@ public:
explicit ClearCaseSync(ClearCasePlugin *plugin, QSharedPointer<StatusMap> statusMap);
void run(QFutureInterface<void> &future, QStringList &files);
QStringList updateStatusHotFiles(const QString &viewRoot, const bool isDynamic, int &total);
void updateStatus(const QDir &viewRootDir, const bool isDynamic, const QStringList &files);
void processLine(const QDir &viewRootDir, const QString &buffer);
void updateTotalFilesCount(const QString view, ClearCaseSettings settings, const int processed);
void updateStatusForNotManagedFiles(const QStringList &files);
signals:
void updateStreamAndView();
private:
ClearCasePlugin *m_plugin;
QSharedPointer<StatusMap> m_statusMap;
public slots:
#ifdef WITH_TESTS
void verifyParseStatus(const QString &fileName, const QString &cleartoolLsLine,
const FileStatus::Status);
void verifyFileNotManaged();
#endif
};
} // namespace Internal
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment