Skip to content

Commit

Permalink
Also check environment when running
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Jun 27, 2023
1 parent 037638e commit 369c2ab
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
39 changes: 33 additions & 6 deletions include/gz/utils/Subprocess.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,46 @@ namespace utils

class Subprocess
{
public: Subprocess(std::vector<std::string> _commandLine):
public: Subprocess(const std::vector<std::string> &_commandLine,
const std::vector<std::string> &_environment = {}):
commandLine(_commandLine),
process(new subprocess_s)
environment(_environment)
{
this->Create();

}

private: void Create()
{
if (this->process)
return;

this->process = new subprocess_s;

std::vector<const char*> commandLineCstr;
for (const auto &val : this->commandLine)
{
commandLineCstr.push_back(val.c_str());
}
commandLineCstr.push_back(nullptr);

auto ret = subprocess_create(commandLineCstr.data(), 0, this->process);
std::vector<const char*> environmentCstr;
for (const auto &val : this->environment)
{
environmentCstr.push_back(val.c_str());
}
environmentCstr.push_back(nullptr);

int ret = -1;
if (this->environment.size())
{
ret = subprocess_create_ex(commandLineCstr.data(), 0, environmentCstr.data(), this->process);
}
else
{
ret = subprocess_create(commandLineCstr.data(), 0, this->process);
}

if (0 != ret)
{
std::cerr << "failed to create subprocess" << std::endl;
Expand Down Expand Up @@ -121,15 +149,14 @@ class Subprocess
std::cerr << "Failed to join subprocess" << std::endl;
}
}
else
{
}

return return_code;
}

protected: std::vector<std::string> commandLine;

protected: std::vector<std::string> environment;

protected: subprocess_s * process {nullptr};
};
} // namespace utils
Expand Down
2 changes: 2 additions & 0 deletions test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ add_executable(subprocess_executable subprocess/subprocess_main.cc )
target_include_directories(subprocess_executable PUBLIC
${PROJECT_SOURCE_DIR}/cli/include/
${PROJECT_SOURCE_DIR}/cli/include/vendored-cli/)
target_link_libraries(subprocess_executable ${PROJECT_LIBRARY_TARGET_NAME})

if(TARGET INTEGRATION_subprocess_TEST)
target_compile_definitions(INTEGRATION_subprocess_TEST PRIVATE
"SUBPROCESS_EXECUTABLE_PATH=\"$<TARGET_FILE:subprocess_executable>\"")

endif()

add_subdirectory(implptr)
10 changes: 8 additions & 2 deletions test/integration/subprocess/subprocess_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string>
#include <thread>

#include <gz/utils/Environment.hh>
#include <gz/utils/cli/CLI.hpp>

class OutputSink
Expand Down Expand Up @@ -53,10 +54,10 @@ int main(int argc, char **argv)
std::string output;
app.add_option("--output", output, "output destination");

int iters;
int iters = 0;
app.add_option("--iterations", iters, "number of iterations to run");

int iter_ms;
int iter_ms = 0;
app.add_option("--iteration-ms", iter_ms, "length of one iteration");

CLI11_PARSE(app, argc, argv);
Expand All @@ -68,4 +69,9 @@ int main(int argc, char **argv)
std::this_thread::sleep_for(std::chrono::milliseconds(iter_ms));
}

std::string env_var;
if(gz::utils::env("ENV_VAR", env_var))
{
sink.Write("ENV_VAR=" + env_var);
}
}
41 changes: 40 additions & 1 deletion test/integration/subprocess_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TEST(Subprocess, CreateValid)
auto proc = Subprocess({kExecutablePath, "--help"});

// Sleep for just a bit to guarantee executable finishes
std::this_thread::sleep_for(std::chrono::milliseconds(5));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
EXPECT_FALSE(proc.Alive());

auto cout = proc.Stdout();
Expand Down Expand Up @@ -92,3 +92,42 @@ TEST(Subprocess, Cerr)
EXPECT_TRUE(cout.empty());
EXPECT_FALSE(cerr.empty());
}

/////////////////////////////////////////////////
TEST(Subprocess, Environment)
{
{
auto proc = Subprocess({kExecutablePath, "--output=cout"},
{"ENV_VAR=foobar"});
// Block until the executable is done
auto ret = proc.Join();
EXPECT_EQ(0u, ret);

auto cout = proc.Stdout();
EXPECT_NE(std::string::npos, cout.find("ENV_VAR=foobar"));
}

{
auto proc = Subprocess({kExecutablePath, "--output=cerr"},
{"ENV_VAR=foobar2"});
// Block until the executable is done
auto ret = proc.Join();
EXPECT_EQ(0u, ret);

auto cerr = proc.Stderr();
EXPECT_NE(std::string::npos, cerr.find("ENV_VAR=foobar2"));
}

{
auto proc = Subprocess({kExecutablePath},
{"ENV_VAR=foobar3"});
// Block until the executable is done
auto ret = proc.Join();
EXPECT_EQ(0u, ret);

auto cout = proc.Stdout();
auto cerr = proc.Stderr();
EXPECT_EQ(std::string::npos, cerr.find("ENV_VAR=foobar3"));
EXPECT_EQ(std::string::npos, cout.find("ENV_VAR=foobar3"));
}
}

0 comments on commit 369c2ab

Please sign in to comment.