Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix env parsing by placing it before executable parsing #81

Merged
merged 2 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ bool ManagerPrivate::ParseConfig(const std::string &_config)
ignerr << "Invalid config file,m issing <ignition> element\n";
return false;
}
// Keep the environment variables in memory. See manpage for putenv.
this->envs = this->ParseEnvs(root);
this->SetEnvs(this->envs);

// Parse and create all the <executable> elements.
this->ParseExecutables(root);
Expand All @@ -529,10 +532,6 @@ bool ManagerPrivate::ParseConfig(const std::string &_config)
if (this->master)
this->ParseExecutableWrappers(root);

// Keep the environment variables in memory. See manpage for putenv.
this->envs = this->ParseEnvs(root);
this->SetEnvs(this->envs);

// Parse and create all the <plugin> elements.
if (this->master)
{
Expand Down
105 changes: 105 additions & 0 deletions src/Manager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,46 @@

#include <gtest/gtest.h>
#include <ignition/common/Console.hh>
#include <ignition/common/Filesystem.hh>

#include <ignition/utilities/ExtraTestMacros.hh>

#include <sys/stat.h>

#include "Manager.hh"

static constexpr char kTestScriptPath[] = "/tmp/ign-launch.sh";

/////////////////////////////////////////////////
bool RemoveTestScript()
{
// Remove the file if it already exists
if (ignition::common::isFile(kTestScriptPath))
{
if (!ignition::common::removeFile(kTestScriptPath))
{
return false;
}
}
return true;
}

/////////////////////////////////////////////////
bool WriteTestScript()
{
if (!RemoveTestScript())
return false;

// Write a simple script and mark it executable
std::ofstream ofs(kTestScriptPath);
ofs << R"(#!/usr/bin/env bash
echo $TEST_VAR
touch $TEST_VAR
)";
chmod(kTestScriptPath, S_IRWXU);
return true;
}

/////////////////////////////////////////////////
TEST(Ignition_TEST, RunEmptyConfig)
{
Expand Down Expand Up @@ -81,6 +119,73 @@ TEST(Ignition_TEST, RunLs)
EXPECT_TRUE(mgr.RunConfig(config));
}


/////////////////////////////////////////////////
TEST(Ignition_TEST, IGN_UTILS_TEST_DISABLED_ON_WIN32(RunEnvPre))
{
// Test that environment is applied regardless of order
std::string testPath = "/tmp/ign-launch-env-test-pre";

if (ignition::common::isFile(testPath))
{
ASSERT_TRUE(ignition::common::removeFile(testPath));
}

ASSERT_TRUE(WriteTestScript());

std::string config = R"(
<ignition version='1.0'>
<env>
<name>TEST_VAR</name>
<value>)" + testPath + R"(</value>
</env>
<executable name='touch'>
<command>/tmp/ign-launch.sh</command>
</executable>
</ignition>
)";

ignition::launch::Manager mgr;

EXPECT_TRUE(mgr.RunConfig(config));
EXPECT_TRUE(ignition::common::isFile(testPath));
EXPECT_TRUE(ignition::common::removeFile(testPath));
EXPECT_TRUE(RemoveTestScript());
}

/////////////////////////////////////////////////
TEST(Ignition_TEST, IGN_UTILS_TEST_DISABLED_ON_WIN32(RunEnvPost))
{
// Test that environment is applied regardless of order
std::string testPath = "/tmp/ign-launch-env-test-post";

if (ignition::common::isFile(testPath))
{
ASSERT_TRUE(ignition::common::removeFile(testPath));
}

ASSERT_TRUE(WriteTestScript());

std::string config = R"(
<ignition version='1.0'>
<executable name='touch'>
<command>/tmp/ign-launch.sh</command>
</executable>
<env>
<name>TEST_VAR</name>
<value>)" + testPath + R"(</value>
</env>
</ignition>
)";

ignition::launch::Manager mgr;

EXPECT_TRUE(mgr.RunConfig(config));
EXPECT_TRUE(ignition::common::isFile(testPath));
EXPECT_TRUE(ignition::common::removeFile(testPath));
EXPECT_TRUE(RemoveTestScript());
}

/////////////////////////////////////////////////
int main(int argc, char **argv)
{
Expand Down