Skip to content

Commit

Permalink
Tests: fix detecting Python on systems with only "python3" command
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarWidget committed Nov 1, 2024
1 parent 681391f commit b2b1954
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions test/cxx/Core/ApplicationPool/PoolTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace tut {
Context context;
PoolPtr pool;
Pool::DebugSupportPtr debug;
string pythonCommand;
Ticket ticket;
GetCallback callback;
SessionPtr currentSession;
Expand All @@ -47,6 +48,7 @@ namespace tut {
context.finalize();
pool = boost::make_shared<Pool>(&context);
pool->initialize();
pythonCommand = findPythonCommand();
callback.func = _callback;
callback.userData = this;

Expand Down Expand Up @@ -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;
Expand Down
36 changes: 35 additions & 1 deletion test/cxx/TestSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <cassert>
#include <vector>
#include <cstdlib>
#include <FileTools/FileManip.h>
#include <SystemTools/UserDatabase.h>
#include <StrIntTools/StrIntUtils.h>
#include <Utils/ScopeGuard.h>
#include <jsoncpp/json.h>

Expand All @@ -36,6 +38,38 @@ createInstanceDir(InstanceDirectoryPtr &instanceDir) {
instanceDir = boost::make_shared<InstanceDirectory>(options);
}

string
findCommandInPath(const string &basename) {
const char *path = getenv("PATH");
if (path == nullptr) {
return string();
}

vector<string> 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;
Expand Down
24 changes: 24 additions & 0 deletions test/cxx/TestSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit b2b1954

Please sign in to comment.