Skip to content

Prevent creation of spurious <plugin> elements when saving worlds #1192

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 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
2 changes: 1 addition & 1 deletion src/LevelManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void LevelManager::ReadLevelPerformerInfo()

sdf::ElementPtr pluginElem;
// Get the ignition::gazebo plugin element
for (auto plugin = worldElem->GetElement("plugin"); plugin;
for (auto plugin = worldElem->FindElement("plugin"); plugin;
plugin = plugin->GetNextElement("plugin"))
{
if (plugin->Get<std::string>("name") == kPluginName)
Expand Down
5 changes: 2 additions & 3 deletions src/SimulationRunner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -959,16 +959,15 @@ void SimulationRunner::LoadLoggingPlugins(const ServerConfig &_config)
void SimulationRunner::LoadPlugins(const Entity _entity,
const sdf::ElementPtr &_sdf)
{
sdf::ElementPtr pluginElem = _sdf->GetElement("plugin");
sdf::ElementPtr pluginElem = _sdf->FindElement("plugin");
while (pluginElem)
{
auto filename = pluginElem->Get<std::string>("filename");
auto name = pluginElem->Get<std::string>("name");
// No error message for the 'else' case of the following 'if' statement
// because SDF create a default <plugin> element even if it's not
// specified. An error message would result in spamming
// the console. \todo(nkoenig) Fix SDF should so that elements are not
// automatically added.
// the console.
if (filename != "__default__" && name != "__default__")
{
this->LoadPlugin(_entity, filename, name, pluginElem);
Expand Down
46 changes: 46 additions & 0 deletions src/SimulationRunner_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,52 @@ TEST_P(SimulationRunnerTest, GenerateWorldSdf)
EXPECT_EQ(3u, world->ModelCount());
}

/////////////////////////////////////////////////
/// Helper function to recursively check for plugins with filename and name
/// attributes set to "__default__"
testing::AssertionResult checkForSpuriousPlugins(sdf::ElementPtr _elem)
{
auto plugin = _elem->FindElement("plugin");
if (nullptr != plugin &&
plugin->Get<std::string>("filename") == "__default__" &&
plugin->Get<std::string>("name") == "__default__")
{
return testing::AssertionFailure() << _elem->ToString("");
}
for (auto child = _elem->GetFirstElement(); child;
child = child->GetNextElement())
{
auto result = checkForSpuriousPlugins(child);
if (!result)
return result;
}
return testing::AssertionSuccess();
}

/////////////////////////////////////////////////
TEST_P(SimulationRunnerTest, GeneratedSdfHasNoSpuriousPlugins)
{
// Load SDF file
sdf::Root root;
root.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "shapes.sdf"));

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

// Create simulation runner
auto systemLoader = std::make_shared<SystemLoader>();
SimulationRunner runner(root.WorldByIndex(0), systemLoader);

msgs::SdfGeneratorConfig req;
msgs::StringMsg genWorldSdf;
EXPECT_TRUE(runner.GenerateWorldSdf(req, genWorldSdf));
EXPECT_FALSE(genWorldSdf.data().empty());

sdf::Root newRoot;
newRoot.LoadSdfString(genWorldSdf.data());
EXPECT_TRUE(checkForSpuriousPlugins(newRoot.Element()));
}

// Run multiple times. We want to make sure that static globals don't cause
// problems.
INSTANTIATE_TEST_SUITE_P(ServerRepeat, SimulationRunnerTest,
Expand Down