Skip to content

Commit

Permalink
Use common facilities
Browse files Browse the repository at this point in the history
* joinPaths rather than string concatenation
* (env, setenv, unsetenv) where possible

Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Oct 21, 2020
1 parent aff1ac8 commit a4fa69b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 53 deletions.
29 changes: 14 additions & 15 deletions src/ServerConfig_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ TEST(parsePluginsFromString, invalid)
//////////////////////////////////////////////////
TEST(parsePluginsFromFile, valid)
{
auto config = std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/server_valid.config";
auto config = common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/server_valid.config");

auto plugins = parsePluginsFromFile(config);
ASSERT_EQ(3u, plugins.size());
Expand All @@ -103,8 +103,8 @@ TEST(parsePluginsFromFile, valid)
//////////////////////////////////////////////////
TEST(parsePluginsFromFile, invalid)
{
auto config = std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/server_invalid.config";
auto config = common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/server_invalid.config");

// Valid file without valid content
auto plugins = parsePluginsFromFile(config);
Expand All @@ -122,33 +122,32 @@ TEST(parsePluginsFromFile, defaultConfig)
// configuration always parses.
// If more systems are added, then the number needs
// to be adjusted below.
auto config = std::string(PROJECT_SOURCE_PATH) +
"/include/ignition/gazebo/server.config";
auto config = common::joinPaths(PROJECT_SOURCE_PATH,
"/include/ignition/gazebo/server.config");

auto plugins = parsePluginsFromFile(config);
ASSERT_EQ(3u, plugins.size());
}

//////////////////////////////////////////////////
TEST(loadPluginInfo, from_empty_env)
TEST(loadPluginInfo, fromEmptyEnv)
{
// ignition::common::env doesn't respect zero-length
// See ignitionrobotics/ign-common#97
ASSERT_EQ(0, setenv(gazebo::kServerConfigPathEnv, "0", true));
ASSERT_TRUE(common::setenv(gazebo::kServerConfigPathEnv, ""));
auto plugins = loadPluginInfo();

EXPECT_EQ(0u, plugins.size());

unsetenv(gazebo::kServerConfigPathEnv);
EXPECT_TRUE(common::unsetenv(gazebo::kServerConfigPathEnv));
}

//////////////////////////////////////////////////
TEST(loadPluginInfo, from_valid_env)
TEST(loadPluginInfo, fromValidEnv)
{
auto validPath = std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/server_valid2.config";
auto validPath = common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/server_valid2.config");

ASSERT_EQ(0, setenv(gazeob::kServerConfigPathEnv, valid_path.c_str(), true));
ASSERT_TRUE(common::setenv(gazebo::kServerConfigPathEnv, validPath));

auto plugins = loadPluginInfo();
ASSERT_EQ(2u, plugins.size());
Expand All @@ -158,6 +157,6 @@ TEST(loadPluginInfo, from_valid_env)
EXPECT_EQ("TestWorldSystem", plugins.begin()->Filename());
EXPECT_EQ("ignition::gazebo::TestWorldSystem", plugins.begin()->Name());

unsetenv(gazebo::kServerConfigPathEnv);
EXPECT_TRUE(common::unsetenv(gazebo::kServerConfigPathEnv));
}

74 changes: 36 additions & 38 deletions src/SimulationRunner_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ class SimulationRunnerTest : public ::testing::TestWithParam<int>
{
common::Console::SetVerbosity(4);

setenv("IGN_GAZEBO_SYSTEM_PLUGIN_PATH",
(std::string(PROJECT_BINARY_PATH) + "/lib").c_str(), 1);
common::setenv("IGN_GAZEBO_SYSTEM_PLUGIN_PATH",
common::joinPaths(PROJECT_BINARY_PATH, "/lib"));
}
};

Expand All @@ -111,8 +111,8 @@ TEST_P(SimulationRunnerTest, CreateEntities)
{
// Load SDF file
sdf::Root root;
root.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/shapes.sdf");
root.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/shapes.sdf"));

ASSERT_EQ(1u, root.WorldCount());

Expand Down Expand Up @@ -524,8 +524,8 @@ TEST_P(SimulationRunnerTest, CreateLights)
{
// Load SDF file
sdf::Root root;
root.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/lights.sdf");
root.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/lights.sdf"));

ASSERT_EQ(1u, root.WorldCount());

Expand Down Expand Up @@ -794,8 +794,8 @@ TEST_P(SimulationRunnerTest, CreateJointEntities)
{
// Load SDF file
sdf::Root root;
root.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/demo_joint_types.sdf");
root.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/demo_joint_types.sdf"));

ASSERT_EQ(1u, root.WorldCount());

Expand Down Expand Up @@ -935,8 +935,8 @@ TEST_P(SimulationRunnerTest, Time)
{
// Load SDF file
sdf::Root root;
root.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/shapes.sdf");
root.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/shapes.sdf"));

ASSERT_EQ(1u, root.WorldCount());

Expand Down Expand Up @@ -1057,8 +1057,8 @@ TEST_P(SimulationRunnerTest, LoadPlugins)
{
// Load SDF file
sdf::Root root;
root.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/plugins.sdf");
root.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/plugins.sdf"));

ASSERT_EQ(1u, root.WorldCount());

Expand Down Expand Up @@ -1140,18 +1140,17 @@ TEST_P(SimulationRunnerTest, LoadPlugins)
}

/////////////////////////////////////////////////
TEST_P(SimulationRunnerTest, LoadServer_NoPlugins)
TEST_P(SimulationRunnerTest, LoadServerNoPlugins)
{
sdf::Root rootWithout;
rootWithout.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/plugins_empty.sdf");
rootWithout.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/plugins_empty.sdf"));
ASSERT_EQ(1u, rootWithout.WorldCount());

// ServerConfig will fall back to environment variable
auto config = std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/server_valid2.config";
ASSERT_EQ(0, setenv(gazebo::kServerConfigPathEnv,
config.c_str(), true));
auto config = common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/server_valid2.config");
ASSERT_EQ(0, common::setenv(gazebo::kServerConfigPathEnv, config));
ServerConfig serverConfig;

// Create simulation runner
Expand All @@ -1166,14 +1165,14 @@ TEST_P(SimulationRunnerTest, LoadServer_NoPlugins)
TEST_P(SimulationRunnerTest, LoadServerConfigPlugins)
{
sdf::Root rootWithout;
rootWithout.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/plugins_empty.sdf");
rootWithout.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/plugins_empty.sdf"));
ASSERT_EQ(1u, rootWithout.WorldCount());

// Create a server configuration with plugins
// No fallback expected
auto config = std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/server_valid.config";
auto config = common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/server_valid.config");

auto plugins = parsePluginsFromFile(config);
ASSERT_EQ(3u, plugins.size());
Expand Down Expand Up @@ -1266,29 +1265,28 @@ TEST_P(SimulationRunnerTest, LoadServerConfigPlugins)
TEST_P(SimulationRunnerTest, LoadPluginsDefault)
{
sdf::Root rootWithout;
rootWithout.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/plugins_empty.sdf");
rootWithout.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/plugins_empty.sdf"));
ASSERT_EQ(1u, rootWithout.WorldCount());

auto config = std::string(PROJECT_SOURCE_PATH) +
"/inclue/ignition/gazebo/server.config";
ASSERT_EQ(0, setenv(gazebo::kServerConfigPathEnv,
config.c_str(), true));
auto config = common::joinPaths(PROJECT_SOURCE_PATH,
"/inclue/ignition/gazebo/server.config");
ASSERT_EQ(0, common::setenv(gazebo::kServerConfigPathEnv, config));

// Create simulation runner
auto systemLoader = std::make_shared<SystemLoader>();
SimulationRunner runner(rootWithout.WorldByIndex(0), systemLoader);
ASSERT_EQ(3u, runner.SystemCount());
unsetenv(gazebo::kServerConfigPathEnv);
common::unsetenv(gazebo::kServerConfigPathEnv);
}

/////////////////////////////////////////////////
TEST_P(SimulationRunnerTest, LoadPluginsEvent)
{
// Load SDF file without plugins
sdf::Root rootWithout;
rootWithout.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/shapes.sdf");
rootWithout.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/shapes.sdf"));
ASSERT_EQ(1u, rootWithout.WorldCount());

// Create simulation runner
Expand Down Expand Up @@ -1322,8 +1320,8 @@ TEST_P(SimulationRunnerTest, LoadPluginsEvent)

// Load SDF file with plugins
sdf::Root rootWith;
rootWith.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/plugins.sdf");
rootWith.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/plugins.sdf"));
ASSERT_EQ(1u, rootWith.WorldCount());

// Emit plugin loading event
Expand Down Expand Up @@ -1380,8 +1378,8 @@ TEST_P(SimulationRunnerTest, GuiInfo)
{
// Load SDF file
sdf::Root root;
root.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/shapes.sdf");
root.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/shapes.sdf"));

ASSERT_EQ(1u, root.WorldCount());

Expand Down Expand Up @@ -1417,8 +1415,8 @@ TEST_P(SimulationRunnerTest, GenerateWorldSdf)
{
// Load SDF file
sdf::Root root;
root.Load(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/shapes.sdf");
root.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"/test/worlds/shapes.sdf"));

ASSERT_EQ(1u, root.WorldCount());

Expand Down

0 comments on commit a4fa69b

Please sign in to comment.