More updater fixage

Preserve --dir parameter after updating
Allow more than one copy of a command line parameter in MultiMC
Linux runner script no longer changes current directory, which allows '--dir .'
Fixed unit tests, removed the obsolete one (for some legacy updater command line params that were also removed)
[fixes 63127704]
This commit is contained in:
Petr Mrázek 2014-01-05 16:47:12 +01:00
parent a64eebf8eb
commit 0dcf694c87
14 changed files with 47 additions and 98 deletions

View File

@ -37,7 +37,7 @@
using namespace Util::Commandline;
MultiMC::MultiMC(int &argc, char **argv, const QString &data_dir_override)
MultiMC::MultiMC(int &argc, char **argv, bool root_override)
: QApplication(argc, argv), m_version{VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD,
VERSION_CHANNEL, VERSION_BUILD_TYPE}
{
@ -111,14 +111,7 @@ MultiMC::MultiMC(int &argc, char **argv, const QString &data_dir_override)
QString adjustedBy;
// change directory
QString dirParam = args["dir"].toString();
if (!data_dir_override.isEmpty())
{
// the override is used for tests (although dirparam would be enough...)
// TODO: remove the need for this extra logic
adjustedBy += "Test override " + data_dir_override;
dataPath = data_dir_override;
}
else if (!dirParam.isEmpty())
if (!dirParam.isEmpty())
{
// the dir param. it makes multimc data path point to whatever the user specified
// on command line
@ -130,6 +123,7 @@ MultiMC::MultiMC(int &argc, char **argv, const QString &data_dir_override)
dataPath = applicationDirPath();
adjustedBy += "Fallback to binary path " + dataPath;
}
if(!ensureFolderPathExists(dataPath) || !QDir::setCurrent(dataPath))
{
// BAD STUFF. WHAT DO?
@ -139,6 +133,11 @@ MultiMC::MultiMC(int &argc, char **argv, const QString &data_dir_override)
return;
}
if (root_override)
{
rootPath = binPath;
}
else
{
#ifdef Q_OS_LINUX
QDir foo(PathCombine(binPath, ".."));
@ -539,7 +538,7 @@ void MultiMC::installUpdates(const QString updateFilesDir, UpdateFlags flags)
#else
#error Unsupported operating system.
#endif
QStringList args;
// ./updater --install-dir $INSTALL_DIR --package-dir $UPDATEFILES_DIR --script
// $UPDATEFILES_DIR/file_list.xml --wait $PID --mode main
@ -550,8 +549,10 @@ void MultiMC::installUpdates(const QString updateFilesDir, UpdateFlags flags)
if(flags & DryRun)
args << "--dry-run";
if (flags & RestartOnFinish)
{
args << "--finish-cmd" << finishCmd;
args << "--finish-dir" << data();
}
QLOG_INFO() << "Running updater with command" << updaterBinary << args.join(" ");
QFile::setPermissions(updaterBinary, (QFileDevice::Permission)0x7755);

View File

@ -58,7 +58,7 @@ public:
};
public:
MultiMC(int &argc, char **argv, const QString &root = QString());
MultiMC(int &argc, char **argv, bool root_override = false);
virtual ~MultiMC();
std::shared_ptr<SettingsObject> settings()

View File

@ -286,11 +286,11 @@ QHash<QString, QVariant> Parser::parse(QStringList argv)
// we were expecting an argument
{
QString name = expecting.first();
/*
if (map.contains(name))
throw ParsingError(
QString("Option %2%1 was given multiple times").arg(name, optionPrefix));
*/
map[name] = QVariant(arg);
expecting.removeFirst();
@ -316,10 +316,11 @@ QHash<QString, QVariant> Parser::parse(QStringList argv)
if (m_options.contains(name))
{
/*
if (map.contains(name))
throw ParsingError(QString("Option %2%1 was given multiple times")
.arg(name, optionPrefix));
*/
OptionDef *option = m_options[name];
if (option->type == otSwitch)
map[name] = true;
@ -367,11 +368,11 @@ QHash<QString, QVariant> Parser::parse(QStringList argv)
throw ParsingError(QString("Unknown flag %2%1").arg(flag, flagPrefix));
OptionDef *option = m_flags[flag];
/*
if (map.contains(option->name))
throw ParsingError(QString("Option %2%1 was given multiple times")
.arg(option->name, optionPrefix));
*/
if (option->type == otSwitch)
map[option->name] = true;
else // if (option->type == otOption)

View File

@ -46,6 +46,11 @@ void UpdateInstaller::setFinishCmd(const std::string& cmd)
m_finishCmd = cmd;
}
void UpdateInstaller::setFinishDir(const std::string &dir)
{
m_finishDir = dir;
}
std::list<std::string> UpdateInstaller::updaterArgs() const
{
std::list<std::string> args;
@ -63,6 +68,11 @@ std::list<std::string> UpdateInstaller::updaterArgs() const
{
args.push_back("--dry-run");
}
if (m_finishDir.size())
{
args.push_back("--dir");
args.push_back(m_finishDir);
}
return args;
}
@ -420,6 +430,11 @@ void UpdateInstaller::restartMainApp()
if (!command.empty())
{
if(!m_finishDir.empty())
{
args.push_back("--dir");
args.push_back(m_finishDir);
}
LOG(Info,"Starting main application " + command);
if(!m_dryRun)
{

View File

@ -34,6 +34,7 @@ class UpdateInstaller
void setAutoClose(bool autoClose);
void setDryRun(bool dryRun);
void setFinishCmd(const std::string& cmd);
void setFinishDir(const std::string& dir);
void setObserver(UpdateObserver* observer);
@ -62,6 +63,7 @@ class UpdateInstaller
std::string m_packageDir;
std::string m_backupDir;
std::string m_finishCmd;
std::string m_finishDir;
PLATFORM_PID m_waitPid = 0;
UpdateScript* m_script = nullptr;
UpdateObserver* m_observer = nullptr;

View File

@ -40,6 +40,7 @@ void UpdaterOptions::parse(int argc, char** argv)
parser.setOption("install-dir");
parser.setOption("package-dir");
parser.setOption("finish-cmd");
parser.setOption("finish-dir");
parser.setOption("script");
parser.setOption("wait");
parser.setOption("mode");
@ -74,6 +75,10 @@ void UpdaterOptions::parse(int argc, char** argv)
{
finishCmd = parser.getValue("finish-cmd");
}
if (parser.getValue("finish-dir"))
{
finishDir = parser.getValue("finish-dir");
}
showVersion = parser.getFlag("version");
forceElevated = parser.getFlag("force-elevated");

View File

@ -15,6 +15,7 @@ class UpdaterOptions
std::string packageDir;
std::string scriptPath;
std::string finishCmd;
std::string finishDir;
PLATFORM_PID waitPid;
std::string logFile;
bool showVersion;

View File

@ -138,7 +138,8 @@ int main(int argc, char** argv)
+ ", wait-pid: " + intToStr(options.waitPid)
+ ", script-path: " + options.scriptPath
+ ", mode: " + intToStr(options.mode)
+ ", finish-cmd: " + options.finishCmd);
+ ", finish-cmd: " + options.finishCmd
+ ", finish-dir: " + options.finishDir);
installer.setMode(options.mode);
installer.setInstallDir(options.installDir);
@ -148,6 +149,7 @@ int main(int argc, char** argv)
installer.setForceElevated(options.forceElevated);
installer.setAutoClose(options.autoClose);
installer.setFinishCmd(options.finishCmd);
installer.setFinishDir(options.finishDir);
installer.setDryRun(options.dryRun);
if (options.mode == UpdateInstaller::Main)

View File

@ -44,5 +44,4 @@ macro(ADD_UPDATER_TEST CLASS)
endmacro()
add_updater_test(TestParseScript)
add_updater_test(TestUpdaterOptions)
add_updater_test(TestFileUtils)

View File

@ -1,68 +0,0 @@
#include "TestUpdaterOptions.h"
#include "FileUtils.h"
#include "Platform.h"
#include "TestUtils.h"
#include "UpdaterOptions.h"
#include <string.h>
#include <stdlib.h>
void TestUpdaterOptions::testOldFormatArgs()
{
const int argc = 6;
char* argv[argc];
argv[0] = strdup("updater");
std::string currentDir("CurrentDir=");
const char* appDir = 0;
// CurrentDir is the path to the directory containing the main
// Mendeley Desktop binary, on Linux and Mac this differs from
// the root of the install directory
#ifdef PLATFORM_LINUX
appDir = "/tmp/path-to-app/lib/mendeleydesktop/libexec/";
FileUtils::mkpath(appDir);
#elif defined(PLATFORM_MAC)
appDir = "/tmp/path-to-app/Contents/MacOS/";
FileUtils::mkpath(appDir);
#elif defined(PLATFORM_WINDOWS)
appDir = "C:/path/to/app/";
#endif
currentDir += appDir;
argv[1] = strdup(currentDir.c_str());
argv[2] = strdup("TempDir=/tmp/updater");
argv[3] = strdup("UpdateScriptFileName=/tmp/updater/file_list.xml");
argv[4] = strdup("AppFileName=/path/to/app/theapp");
argv[5] = strdup("PID=123456");
UpdaterOptions options;
options.parse(argc,argv);
TEST_COMPARE(options.mode,UpdateInstaller::Setup);
#ifdef PLATFORM_LINUX
TEST_COMPARE(options.installDir,"/tmp/path-to-app");
#elif defined(PLATFORM_MAC)
// /tmp is a symlink to /private/tmp on Mac
TEST_COMPARE(options.installDir,"/private/tmp/path-to-app");
#else
TEST_COMPARE(options.installDir,"C:/path/to/app/");
#endif
TEST_COMPARE(options.packageDir,"/tmp/updater");
TEST_COMPARE(options.scriptPath,"/tmp/updater/file_list.xml");
TEST_COMPARE(options.waitPid,123456);
for (int i=0; i < argc; i++)
{
free(argv[i]);
}
}
int main(int,char**)
{
TestList<TestUpdaterOptions> tests;
tests.addTest(&TestUpdaterOptions::testOldFormatArgs);
return TestUtils::runTest(tests);
}

View File

@ -1,8 +0,0 @@
#pragma once
class TestUpdaterOptions
{
public:
void testOldFormatArgs();
};

View File

@ -15,7 +15,6 @@ fi
MMC_DIR="$(dirname "$(readlink -f "$0")")"
cd "${MMC_DIR}"
echo "MultiMC Dir: ${MMC_DIR}"
# Set up env

View File

@ -39,7 +39,7 @@ int main(int argc, char *argv[]) \
{ \
char *argv_[] = { argv[0] _MMC_EXTRA_ARGV }; \
int argc_ = 1 + _MMC_EXTRA_ARGC; \
MultiMC app(argc_, argv_/*, QDir::temp().absoluteFilePath("MultiMC_Test")*/); \
MultiMC app(argc_, argv_, true); \
app.setAttribute(Qt::AA_Use96Dpi, true); \
TestObject tc; \
return QTest::qExec(&tc, argc, argv); \

View File

@ -254,7 +254,7 @@ slots:
pathOrig = path = "MultiMC.app/Foo/Bar/Baz";
qDebug() << "Proper OSX path: " << path;
result = DownloadUpdateTask::fixPathForOSX(path);
QCOMPARE(path, QString("../../Foo/Bar/Baz"));
QCOMPARE(path, QString("Foo/Bar/Baz"));
QCOMPARE(result, true);
// Bad OSX path