Squish: Move some helper functions

Allow to reuse them.

Change-Id: Ia13f7f6c7a40066fd7ae46389adc7c9228161a4a
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Stenger
2022-10-07 09:19:47 +02:00
parent d74c5301ff
commit 5c4441a760
6 changed files with 57 additions and 31 deletions

View File

@@ -5,6 +5,7 @@
#include "opensquishsuitesdialog.h" #include "opensquishsuitesdialog.h"
#include "squishconstants.h" #include "squishconstants.h"
#include "squishplugin.h"
#include "squishsettings.h" #include "squishsettings.h"
#include "squishtesttreemodel.h" #include "squishtesttreemodel.h"
#include "squishtools.h" #include "squishtools.h"

View File

@@ -86,6 +86,20 @@ SquishSettings::SquishSettings()
}); });
} }
Utils::FilePath SquishSettings::scriptsPath(Language language) const
{
Utils::FilePath scripts = squishPath.filePath().pathAppended("scriptmodules");
switch (language) {
case Language::Python: scripts = scripts.pathAppended("python"); break;
case Language::Perl: scripts = scripts.pathAppended("perl"); break;
case Language::JavaScript: scripts = scripts.pathAppended("javascript"); break;
case Language::Ruby: scripts = scripts.pathAppended("ruby"); break;
case Language::Tcl: scripts = scripts.pathAppended("tcl"); break;
}
return scripts.isReadableDir() ? scripts : Utils::FilePath();
}
SquishSettingsPage::SquishSettingsPage(SquishSettings *settings) SquishSettingsPage::SquishSettingsPage(SquishSettings *settings)
{ {
setId("A.Squish.General"); setId("A.Squish.General");

View File

@@ -17,6 +17,8 @@ QT_END_NAMESPACE
namespace Squish { namespace Squish {
namespace Internal { namespace Internal {
enum class Language;
class SquishServerSettings : public Utils::AspectContainer class SquishServerSettings : public Utils::AspectContainer
{ {
public: public:
@@ -39,6 +41,8 @@ class SquishSettings : public Utils::AspectContainer
public: public:
SquishSettings(); SquishSettings();
Utils::FilePath scriptsPath(Language language) const;
Utils::StringAspect squishPath; Utils::StringAspect squishPath;
Utils::StringAspect licensePath; Utils::StringAspect licensePath;
Utils::StringAspect serverHost; Utils::StringAspect serverHost;

View File

@@ -161,28 +161,12 @@ void SquishTestTreeItemDelegate::setEditorData(QWidget *editor, const QModelInde
static_cast<Utils::FancyLineEdit *>(editor)->setText(index.data().toString()); static_cast<Utils::FancyLineEdit *>(editor)->setText(index.data().toString());
} }
static Utils::FilePath scriptsPath(const Utils::FilePath &squishPath, Language language) static bool copyScriptTemplates(const SuiteConf &suiteConf, const Utils::FilePath &destination)
{
Utils::FilePath scripts = squishPath.pathAppended("scriptmodules");
switch (language) {
case Language::Python: scripts = scripts.pathAppended("python"); break;
case Language::Perl: scripts = scripts.pathAppended("perl"); break;
case Language::JavaScript: scripts = scripts.pathAppended("javascript"); break;
case Language::Ruby: scripts = scripts.pathAppended("ruby"); break;
case Language::Tcl: scripts = scripts.pathAppended("tcl"); break;
}
return scripts;
}
static bool copyScriptTemplates(const SuiteConf &suiteConf,
const Utils::FilePath &destination)
{ {
const SquishSettings *s = SquishPlugin::squishSettings(); const SquishSettings *s = SquishPlugin::squishSettings();
QTC_ASSERT(s, return false); QTC_ASSERT(s, return false);
// copy template files // copy template files
Utils::FilePath squishPath = s->squishPath.filePath(); const Utils::FilePath squishPath = s->squishPath.filePath();
Utils::FilePath scripts = scriptsPath(squishPath, suiteConf.language());
bool ok = destination.ensureWritableDir(); bool ok = destination.ensureWritableDir();
QTC_ASSERT(ok, return false); QTC_ASSERT(ok, return false);
@@ -191,24 +175,16 @@ static bool copyScriptTemplates(const SuiteConf &suiteConf,
const QString extension = suiteConf.scriptExtension(); const QString extension = suiteConf.scriptExtension();
const QString testStr = scripted ? QString("script_som_template") : QString("script_template"); const QString testStr = scripted ? QString("script_som_template") : QString("script_template");
const Utils::FilePath scripts = s->scriptsPath(suiteConf.language());
const Utils::FilePath test = scripts.pathAppended(testStr + extension); const Utils::FilePath test = scripts.pathAppended(testStr + extension);
const Utils::FilePath testFile = destination.pathAppended("test" + extension); const Utils::FilePath testFile = destination.pathAppended("test" + extension);
QTC_ASSERT(testFile.exists(), return false);
ok = test.copyFile(testFile); ok = test.copyFile(testFile);
QTC_ASSERT(ok, return false); QTC_ASSERT(ok, return false);
if (scripted) { if (scripted)
const Utils::FilePath destinationObjectMap = destination.parentDir() ok = suiteConf.ensureObjectMapExists();
.pathAppended("shared/scripts/names" + extension); return ok;
if (destinationObjectMap.exists())
return true;
const Utils::FilePath objectMap = scripts.pathAppended("objectmap_template" + extension);
ok = destinationObjectMap.parentDir().ensureWritableDir();
QTC_ASSERT(ok, return false);
ok = objectMap.copyFile(destinationObjectMap);
QTC_ASSERT(ok, return false);
}
return true;
} }
void SquishTestTreeItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, void SquishTestTreeItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,

View File

@@ -3,6 +3,9 @@
#include "suiteconf.h" #include "suiteconf.h"
#include "squishplugin.h"
#include "squishsettings.h"
#include <coreplugin/documentmanager.h> #include <coreplugin/documentmanager.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
@@ -284,5 +287,31 @@ SuiteConf SuiteConf::readSuiteConf(const Utils::FilePath &suiteConfPath)
return suiteConf; return suiteConf;
} }
bool SuiteConf::ensureObjectMapExists() const
{
if (m_objectMapStyle != "script") {
const Utils::FilePath objectMap = objectMapPath();
bool ok = objectMap.parentDir().ensureWritableDir();
ok |= objectMap.ensureExistingFile();
return ok;
}
const Utils::FilePath scripts = SquishPlugin::squishSettings()->scriptsPath(language());
QTC_ASSERT(scripts.exists(), return false);
const QString extension = scriptExtension();
const Utils::FilePath destinationObjectMap = m_filePath.parentDir()
.pathAppended("shared/scripts/names" + extension);
if (destinationObjectMap.exists()) // do not overwrite existing
return true;
const Utils::FilePath objectMap = scripts.pathAppended("objectmap_template" + extension);
bool ok = destinationObjectMap.parentDir().ensureWritableDir();
QTC_ASSERT(ok, return false);
ok = objectMap.copyFile(destinationObjectMap);
QTC_ASSERT(ok, return false);
return ok;
}
} // namespace Internal } // namespace Internal
} // namespace Squish } // namespace Squish

View File

@@ -36,6 +36,8 @@ public:
void addTestCase(const QString &testCase); void addTestCase(const QString &testCase);
QStringList usedTestCases() const; QStringList usedTestCases() const;
bool ensureObjectMapExists() const;
private: private:
void setLanguage(const QString &language); void setLanguage(const QString &language);