From b2b195404dbeaa62a00726dd3e782be439beba2f Mon Sep 17 00:00:00 2001 From: Hongli Lai Date: Sun, 27 Oct 2024 15:01:37 +0100 Subject: [PATCH] Tests: fix detecting Python on systems with only "python3" command --- test/cxx/Core/ApplicationPool/PoolTest.cpp | 3 ++ test/cxx/TestSupport.cpp | 36 +++++++++++++++++++++- test/cxx/TestSupport.h | 24 +++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/test/cxx/Core/ApplicationPool/PoolTest.cpp b/test/cxx/Core/ApplicationPool/PoolTest.cpp index 313b89ecb5..fe8ba18608 100644 --- a/test/cxx/Core/ApplicationPool/PoolTest.cpp +++ b/test/cxx/Core/ApplicationPool/PoolTest.cpp @@ -23,6 +23,7 @@ namespace tut { Context context; PoolPtr pool; Pool::DebugSupportPtr debug; + string pythonCommand; Ticket ticket; GetCallback callback; SessionPtr currentSession; @@ -47,6 +48,7 @@ namespace tut { context.finalize(); pool = boost::make_shared(&context); pool->initialize(); + pythonCommand = findPythonCommand(); callback.func = _callback; callback.userData = this; @@ -113,6 +115,7 @@ namespace tut { options.spawnMethod = "dummy"; options.appRoot = "stub/rack"; options.appType = "ruby"; + options.python = pythonCommand.c_str(); options.appStartCommand = "ruby start.rb"; options.startupFile = "start.rb"; options.loadShellEnvvars = false; diff --git a/test/cxx/TestSupport.cpp b/test/cxx/TestSupport.cpp index 5955f17ea5..b624a32714 100644 --- a/test/cxx/TestSupport.cpp +++ b/test/cxx/TestSupport.cpp @@ -7,9 +7,11 @@ #include #include #include -#include +#include +#include #include #include +#include #include #include @@ -36,6 +38,38 @@ createInstanceDir(InstanceDirectoryPtr &instanceDir) { instanceDir = boost::make_shared(options); } +string +findCommandInPath(const string &basename) { + const char *path = getenv("PATH"); + if (path == nullptr) { + return string(); + } + + vector directories; + split(path, ':', directories); + + for (const string &dir: directories) { + string fullPath = dir + "/" + basename; + if (fileExists(fullPath)) { + return fullPath; + } + } + + return string(); +} + +string +findPythonCommand() { + string python = findCommandInPath("python"); + if (python.empty()) { + python = findCommandInPath("python3"); + } + if (python.empty()) { + python = findCommandInPath("python2"); + } + return python; +} + void writeUntilFull(int fd) { int flags, ret; diff --git a/test/cxx/TestSupport.h b/test/cxx/TestSupport.h index 872f2127d3..46267e4f3a 100644 --- a/test/cxx/TestSupport.h +++ b/test/cxx/TestSupport.h @@ -106,6 +106,30 @@ extern Json::Value testConfig; */ void createInstanceDir(InstanceDirectoryPtr &instanceDir); +/** + * Find the given command in PATH. Returns the empty string if not found. + * + * @throws FileSystemException + * @throws TimeRetrievalException + * @throws boost::thread_interrupted + */ +string findCommandInPath(const string &basename); + +/** + * Find an appropriate Python command for this system, regardless of whether it's Python 2 or 3. + * Returns something like "/usr/bin/python3" if possible, but on older + * systems without Python 3 it could return something like "/usr/bin/python". + * + * This function primarily exists because newer systems don't have a "python" command anymore, only "python3". + * + * Returns the empty string if not found. + * + * @throws FileSystemException + * @throws TimeRetrievalException + * @throws boost::thread_interrupted + */ +string findPythonCommand(); + /** * Writes zeroes into the given file descriptor its buffer is full (i.e. * the next write will block).