19
19
#include <unity/scopes/internal/Utils.h>
20
20
#include <unity/scopes/ScopeExceptions.h>
21
21
#include <unity/UnityExceptions.h>
22
#include <unity/util/ResourcePtr.h>
23
24
#include <boost/filesystem.hpp>
29
31
#include <sys/stat.h>
195
vector<string> split_exec_args(string const& id, string const& custom_exec)
197
if (custom_exec.empty())
199
throw unity::InvalidArgumentException("Invalid empty executable for scope: '" + id + "'");
203
vector<string> result;
205
// Split command into program and args
206
if (wordexp(custom_exec.c_str(), &exp, WRDE_NOCMD) == 0)
208
util::ResourcePtr<wordexp_t*, decltype(&wordfree)> free_guard(&exp, wordfree);
209
for (size_t i = 0; i < exp.we_wordc; ++i )
211
auto argument = string(exp.we_wordv[i]);
212
if(argument.find_first_of(' ') != std::string::npos)
214
// This argument contains spaces, enclose it in quotation marks
215
result.emplace_back("\"" + argument + "\"");
219
result.emplace_back(argument);
225
throw unity::InvalidArgumentException("Invalid executable for scope: '" + id + "'");
231
string convert_exec_rel_to_abs(string const& id, boost::filesystem::path const& scope_dir, string const& custom_exec)
235
// Loop through each argument of the scope runner command and ensure all path args are absolute
236
auto custom_exec_args = split_exec_args(id, custom_exec);
237
for (auto custom_exec_arg : custom_exec_args)
239
boost::filesystem::path argument(custom_exec_arg);
240
if (argument.is_relative())
242
// First look inside the arch-specific directory
243
if (boost::filesystem::exists(scope_dir / DEB_HOST_MULTIARCH / argument))
245
// Append the argument as a relative path
246
result += (scope_dir / DEB_HOST_MULTIARCH / argument).native() + " ";
248
// Next try in the non arch-aware directory
249
else if (boost::filesystem::exists(scope_dir / argument))
251
// Append the argument as a relative path
252
result += (scope_dir / argument).native() + " ";
254
// If this is the first argument (program name) it must exist, throw here
255
else if (result.empty())
257
throw unity::InvalidArgumentException(
258
"Nonexistent scope runner executable: '" + custom_exec_arg
259
+ "' for scope: '" + id + "'");
261
// Otherwise just append the argument as is
264
result += custom_exec_arg + " ";
269
result += custom_exec_arg + " ";
272
result.resize(result.size() - 1);
193
276
} // namespace internal
195
278
} // namespace scopes