Skip to content

Commit

Permalink
Search PATH when relative path to executable to launch is specified.
Browse files Browse the repository at this point in the history
See issue commontk#6
  • Loading branch information
jcfr committed Jul 17, 2012
1 parent 9893163 commit a02b1a3
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Base/ctkAppLauncher.cpp
@@ -1,5 +1,6 @@

//Qt includes
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QProcessEnvironment>
Expand Down Expand Up @@ -144,6 +145,27 @@ bool ctkAppLauncherInternal::processApplicationToLaunchArgument()

this->ApplicationToLaunch = this->expandValue(this->ApplicationToLaunch);

if (!this->ApplicationToLaunch.isEmpty())
{
QFileInfo applicationToLaunchInfo(this->ApplicationToLaunch);
if (applicationToLaunchInfo.isRelative())
{
QString path = applicationToLaunchInfo.path() + "/" + applicationToLaunchInfo.baseName();
QString extension = applicationToLaunchInfo.completeSuffix();
#ifdef Q_OS_WIN32
if (extension.isEmpty())
{
extension = "exe";
}
#endif
QString resolvedApplicationToLaunch = this->searchPaths(path, QStringList() << extension);
if (!resolvedApplicationToLaunch.isEmpty())
{
this->ApplicationToLaunch = resolvedApplicationToLaunch;
}
}
}

this->reportInfo(QString("ApplicationToLaunch [%1]").arg(this->ApplicationToLaunch));

// Make sure the program to launch exists
Expand Down Expand Up @@ -263,6 +285,33 @@ bool ctkAppLauncherInternal::disableSplash() const
return false;
}

// --------------------------------------------------------------------------
QString ctkAppLauncherInternal::searchPaths(const QString& executableName, const QStringList& extensions)
{
QProcessEnvironment environment = QProcessEnvironment::systemEnvironment();
QStringList paths = environment.value("PATH").split(this->PathSep);
paths = this->ListOfPaths + paths;
foreach(const QString& path, paths)
{
foreach(const QString& extension, extensions)
{
QString executableNameWithExt = executableName;
if (!extension.isEmpty())
{
executableNameWithExt.append("." + extension);
}
QDir pathAsDir(path);
QString executablePath = pathAsDir.filePath(executableNameWithExt);
this->reportInfo(QString("Checking if [%1] exists in [%2]").arg(executableNameWithExt).arg(path));
if (QFile::exists(executablePath))
{
return executablePath;
}
}
}
return QString();
}

// --------------------------------------------------------------------------
bool ctkAppLauncherInternal::extractLauncherNameAndDir(const QString& applicationFilePath)
{
Expand Down
2 changes: 2 additions & 0 deletions Base/ctkAppLauncher_p.h
Expand Up @@ -66,6 +66,8 @@ class ctkAppLauncherInternal : public QObject
QString splashImagePath()const;
bool disableSplash()const;

QString searchPaths(const QString& programName, const QStringList& extensions);

public slots:

/// Called just after the splashscreen is shown
Expand Down
126 changes: 126 additions & 0 deletions Testing/Cpp/AppLauncherTestLaunchExecutableInPath.cmake
@@ -0,0 +1,126 @@
#
# AppLauncherTestLaunchExecutableInPath
#

include(${TEST_SOURCE_DIR}/AppLauncherTestMacros.cmake)
include(${TEST_BINARY_DIR}/AppLauncherTestPrerequisites.cmake)

# --------------------------------------------------------------------------
# Configure settings file
file(WRITE "${launcher}LauncherSettings.ini" "
[Paths]
1\\path=${application_dir}
size=1
[LibraryPaths]
1\\path=${library_path}
size=1
")

# --------------------------------------------------------------------------
# Debug flags - Set to True to display the command as string
set(PRINT_COMMAND 0)

# --------------------------------------------------------------------------
# TestLaunchExecutableInPath - If launcher search considers PATH as expected, the launcher should
# execute App4Test if specified as command line parameter.
set(command ${launcher_exe} --launcher-no-splash --launch ${application_name} --print-hello-world)
execute_process(
COMMAND ${command}
WORKING_DIRECTORY ${launcher_binary_dir}
ERROR_VARIABLE ev
OUTPUT_VARIABLE ov
RESULT_VARIABLE rv
)

print_command_as_string("${command}")

set(expected_msg "Hello world !")
string(REGEX MATCH ${expected_msg} current_msg "${ov}")
if("${current_msg}" STREQUAL "")
message(FATAL_ERROR "TestLaunchExecutableInPath\n"
" expected_msg:${expected_msg}\n"
" current_msg:${ov}")
endif()


# --------------------------------------------------------------------------
# Configure settings file
file(WRITE "${launcher}LauncherSettings.ini" "
[Application]
path=${application_name}
[Paths]
1\\path=${application_dir}
size=1
[LibraryPaths]
1\\path=${library_path}
size=1
")

# --------------------------------------------------------------------------
# TestLaunchExecutableInPath - If launcher search considers PATH as expected, the launcher should
# execute App4Test if specified in the LauncherSettings.
set(command ${launcher_exe} --launcher-no-splash --print-hello-world)
execute_process(
COMMAND ${command}
WORKING_DIRECTORY ${launcher_binary_dir}
ERROR_VARIABLE ev
OUTPUT_VARIABLE ov
RESULT_VARIABLE rv
)

print_command_as_string("${command}")

set(expected_msg "Hello world !")
string(REGEX MATCH ${expected_msg} current_msg "${ov}")
if("${current_msg}" STREQUAL "")
message(FATAL_ERROR "TestLaunchExecutableInPath\n"
" expected_msg:${expected_msg}\n"
" current_msg:${ov}")
endif()

# --------------------------------------------------------------------------
# Configure settings file

set(application_relative_dir "bin")
if(WIN32)
set(application_relative_dir ${application_relative_dir}/${app4test_build_type})
endif()

file(WRITE "${launcher}LauncherSettings.ini" "
[Application]
path=${application_relative_dir}/${application_name}
[Paths]
1\\path=${app4test_binary_dir}
size=1
[LibraryPaths]
1\\path=${library_path}
size=1
")

# --------------------------------------------------------------------------
# TestLaunchExecutableInPath - If launcher search considers PATH as expected, the launcher should
# execute bin/(Release/)App4Test if specified in the LauncherSettings.
set(command ${launcher_exe} --launcher-no-splash --print-hello-world)
execute_process(
COMMAND ${command}
WORKING_DIRECTORY ${launcher_binary_dir}
ERROR_VARIABLE ev
OUTPUT_VARIABLE ov
RESULT_VARIABLE rv
)

print_command_as_string("${command}")

set(expected_msg "Hello world !")
string(REGEX MATCH ${expected_msg} current_msg "${ov}")
if("${current_msg}" STREQUAL "")
message(FATAL_ERROR "TestLaunchExecutableInPath\n"
" expected_msg:${expected_msg}\n"
" current_msg:${ov}")
endif()
1 change: 1 addition & 0 deletions Testing/Cpp/CMakeLists.txt
Expand Up @@ -72,3 +72,4 @@ applauncher_add_test(AppLauncherTest7 AppLauncherTest6)
applauncher_add_test(AppLauncherTest8 AppLauncherTest7)
applauncher_add_test(AppLauncherTestNoSplashScreen AppLauncherTest8)
applauncher_add_test(AppLauncherTestLauncherDetach AppLauncherTestNoSplashScreen)
applauncher_add_test(AppLauncherTestLaunchExecutableInPath AppLauncherTestLauncherDetach)

0 comments on commit a02b1a3

Please sign in to comment.