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
......
......@@ -34,6 +34,12 @@
#include <QFutureInterface>
#include <QProcess>
#include <QStringList>
#include <utils/qtcassert.h>
#ifdef WITH_TESTS
#include <QTest>
#include <utils/fileutils.h>
#endif
namespace ClearCase {
namespace Internal {
......@@ -44,60 +50,120 @@ ClearCaseSync::ClearCaseSync(ClearCasePlugin *plugin, QSharedPointer<StatusMap>
{
}
QStringList ClearCaseSync::updateStatusHotFiles(const QString &viewRoot,
const bool isDynamic, int &total)
{
QStringList hotFiles;
// find all files whose permissions changed OR hijacked files
// (might have become checked out)
const StatusMap::Iterator send = m_statusMap->end();
for (StatusMap::Iterator it = m_statusMap->begin(); it != send; ++it) {
const QFileInfo fi(viewRoot, it.key());
const bool permChanged = it.value().permissions != fi.permissions();
if (permChanged || it.value().status == FileStatus::Hijacked) {
hotFiles.append(it.key());
it.value().status = FileStatus::Unknown;
++total;
} else if (isDynamic && !fi.isWritable()) { // assume a read only file is checked in
it.value().status = FileStatus::CheckedIn;
++total;
}
}
return hotFiles;
}
void ClearCaseSync::updateStatus(const QDir &viewRootDir, const bool isDynamic,
const QStringList &files)
{
foreach (const QString &file, files) {
if (isDynamic) { // assume a read only file is checked in
const QFileInfo fi(viewRootDir, file);
if (!fi.isWritable())
m_plugin->setStatus(fi.absoluteFilePath(), FileStatus::CheckedIn, false);
} else {
m_plugin->setStatus(viewRootDir.absoluteFilePath(file), FileStatus::Unknown, false);
}
}
}
void ClearCaseSync::processLine(const QDir &viewRootDir, const QString &buffer)
{
const int atatpos = buffer.indexOf(QLatin1String("@@"));
if (atatpos == -1)
return;
// find first whitespace. anything before that is not interesting
const int wspos = buffer.indexOf(QRegExp(QLatin1String("\\s")));
const QString absFile =
viewRootDir.absoluteFilePath(
QDir::fromNativeSeparators(buffer.left(atatpos)));
QTC_CHECK(QFile(absFile).exists());
QString ccState;
const QRegExp reState(QLatin1String("^\\s*\\[[^\\]]*\\]")); // [hijacked]; [loaded but missing]
if (reState.indexIn(buffer, wspos + 1, QRegExp::CaretAtOffset) != -1) {
ccState = reState.cap();
if (ccState.indexOf(QLatin1String("hijacked")) != -1)
m_plugin->setStatus(absFile, FileStatus::Hijacked, true);
else if (ccState.indexOf(QLatin1String("loaded but missing")) != -1)
m_plugin->setStatus(absFile, FileStatus::Missing, false);
}
else if (buffer.lastIndexOf(QLatin1String("CHECKEDOUT"), wspos) != -1)
m_plugin->setStatus(absFile, FileStatus::CheckedOut, true);
// don't care about checked-in files not listed in project
else if (m_statusMap->contains(absFile))
m_plugin->setStatus(absFile, FileStatus::CheckedIn, true);
}
void ClearCaseSync::updateTotalFilesCount(const QString view, ClearCaseSettings settings,
const int processed)
{
settings = m_plugin->settings(); // Might have changed while task was running
settings.totalFiles[view] = processed;
m_plugin->setSettings(settings);
}
void ClearCaseSync::updateStatusForNotManagedFiles(const QStringList &files)
{
foreach (const QString &file, files) {
QString absFile = QFileInfo(file).absoluteFilePath();
if (!m_statusMap->contains(absFile))
m_plugin->setStatus(absFile, FileStatus::NotManaged, false);
}
}
void ClearCaseSync::run(QFutureInterface<void> &future, QStringList &files)
{
ClearCaseSettings settings = m_plugin->settings();
if (settings.disableIndexer)
return;
const QString program = settings.ccBinaryPath;
if (program.isEmpty())
return;
int total = files.size();
const bool hot = (total < 10);
int totalFileCount = files.size();
const bool hot = (totalFileCount < 10);
int processed = 0;
QString view = m_plugin->currentView();
if (view.isEmpty())
emit updateStreamAndView();
if (!hot)
total = settings.totalFiles.value(view, total);
totalFileCount = settings.totalFiles.value(view, totalFileCount);
// refresh activities list
if (m_plugin->isUcm())
m_plugin->refreshActivities();
if (settings.disableIndexer)
return;
const bool isDynamic = m_plugin->isDynamic();
const QString viewRoot = m_plugin->viewRoot();
const QDir viewRootDir(viewRoot);
QStringList args(QLatin1String("ls"));
if (hot) {
// find all files whose permissions changed OR hijacked files
// (might have become checked out)
const StatusMap::Iterator send = m_statusMap->end();
for (StatusMap::Iterator it = m_statusMap->begin(); it != send; ++it) {
const QFileInfo fi(viewRoot, it.key());
const bool permChanged = it.value().permissions != fi.permissions();
if (permChanged || it.value().status == FileStatus::Hijacked) {
files.append(it.key());
it.value().status = FileStatus::Unknown;
++total;
} else if (isDynamic && !fi.isWritable()) { // assume a read only file is checked in
it.value().status = FileStatus::CheckedIn;
++total;
}
}
files << updateStatusHotFiles(viewRoot, isDynamic, totalFileCount);
args << files;
} else {
foreach (const QString &file, files) {
if (isDynamic) { // assume a read only file is checked in
const QFileInfo fi(viewRootDir, file);
if (!fi.isWritable())
m_plugin->setStatus(fi.absoluteFilePath(), FileStatus::CheckedIn, false);
} else {
m_plugin->setStatus(viewRootDir.absoluteFilePath(file), FileStatus::Unknown, false);
}
}
updateStatus(viewRootDir, isDynamic, files);
args << QLatin1String("-recurse");
QStringList vobs;
......@@ -111,7 +177,7 @@ void ClearCaseSync::run(QFutureInterface<void> &future, QStringList &files)
// adding 1 for initial sync in which total is not accurate, to prevent finishing
// (we don't want it to become green)
future.setProgressRange(0, total + 1);
future.setProgressRange(0, totalFileCount + 1);
QProcess process;
process.setWorkingDirectory(viewRoot);
......@@ -124,55 +190,92 @@ void ClearCaseSync::run(QFutureInterface<void> &future, QStringList &files)
process.bytesAvailable() && !future.isCanceled())
{
const QString line = QString::fromLocal8Bit(process.readLine().constData());
buffer += line;
if (buffer.endsWith(QLatin1Char('\n')) || process.atEnd()) {
const int atatpos = buffer.indexOf(QLatin1String("@@"));
if (atatpos != -1) { // probably managed file
// find first whitespace. anything before that is not interesting
const int wspos = buffer.indexOf(QRegExp(QLatin1String("\\s")));
const QString absFile =
viewRootDir.absoluteFilePath(
QDir::fromNativeSeparators(buffer.left(atatpos)));
QString ccState;
const QRegExp reState(QLatin1String("^\\s*\\[[^\\]]*\\]")); // [hijacked]; [loaded but missing]
if (reState.indexIn(buffer, wspos + 1, QRegExp::CaretAtOffset) != -1) {
ccState = reState.cap();
if (ccState.indexOf(QLatin1String("hijacked")) != -1)
m_plugin->setStatus(absFile, FileStatus::Hijacked, true);
else if (ccState.indexOf(QLatin1String("loaded but missing")) != -1)
m_plugin->setStatus(absFile, FileStatus::Missing, false);
}
else if (buffer.lastIndexOf(QLatin1String("CHECKEDOUT"), wspos) != -1)
m_plugin->setStatus(absFile, FileStatus::CheckedOut, true);
// don't care about checked-in files not listed in project
else if (m_statusMap->contains(absFile))
m_plugin->setStatus(absFile, FileStatus::CheckedIn, true);
}
processLine(viewRootDir, buffer);
buffer.clear();
future.setProgressValue(qMin(total, ++processed));
future.setProgressValue(qMin(totalFileCount, ++processed));
}
}
}
if (!future.isCanceled()) {
foreach (const QString &file, files) {
QString absFile = QFileInfo(file).absoluteFilePath();
if (!m_statusMap->contains(absFile))
m_plugin->setStatus(absFile, FileStatus::NotManaged, false);
}
future.setProgressValue(total + 1);
if (!hot) {
settings = m_plugin->settings(); // Might have changed while task was running
settings.totalFiles[view] = processed;
m_plugin->setSettings(settings);
}
updateStatusForNotManagedFiles(files);
future.setProgressValue(totalFileCount + 1);
if (!hot)
updateTotalFilesCount(view, settings, processed);
}
if (process.state() == QProcess::Running)
process.kill();
process.waitForFinished();
}
#ifdef WITH_TESTS
namespace {
class TempFile
{
public:
TempFile(const QString &fileName)
: m_fileName(fileName)
{
Utils::FileSaver srcSaver(fileName);
srcSaver.write(QByteArray());
srcSaver.finalize();
}
QString fileName() const { return m_fileName; }
~TempFile()
{
QVERIFY(QFile::remove(m_fileName));
}
private:
const QString m_fileName;
};
}
void ClearCaseSync::verifyParseStatus(const QString &fileName,
const QString &cleartoolLsLine,
const FileStatus::Status status)
{
QCOMPARE(m_statusMap->count(), 0);
processLine(QDir(QLatin1String("/")), cleartoolLsLine);
if (status == FileStatus::CheckedIn) {
// The algorithm doesn't store checked in files in the index, unless it was there already
QCOMPARE(m_statusMap->count(), 0);
QCOMPARE(m_statusMap->contains(fileName), false);
m_plugin->setStatus(fileName, FileStatus::Unknown, false);
processLine(QDir(QLatin1String("/")), cleartoolLsLine);
}
QCOMPARE(m_statusMap->count(), 1);
QCOMPARE(m_statusMap->contains(fileName), true);
QCOMPARE(m_statusMap->value(fileName).status, status);
QCOMPARE(m_statusMap->contains(QLatin1String(("notexisting"))), false);
}
void ClearCaseSync::verifyFileNotManaged()
{
QCOMPARE(m_statusMap->count(), 0);
TempFile temp(QDir::currentPath() + QLatin1String("/notmanaged.cpp"));
const QString fileName = temp.fileName();
updateStatusForNotManagedFiles(QStringList(fileName));
QCOMPARE(m_statusMap->count(), 1);
QCOMPARE(m_statusMap->contains(fileName), true);
QCOMPARE(m_statusMap->value(fileName).status, FileStatus::NotManaged);
}
#endif
} // namespace Internal
} // namespace ClearCase
......@@ -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