Skip to content
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

Print an error message when trying to load SDF files that don't contain a <world> #1998

Merged
merged 4 commits into from
Jun 12, 2023
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
8 changes: 8 additions & 0 deletions src/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ Server::Server(const ServerConfig &_config)
return;
}

if (this->dataPtr->sdfRoot.WorldCount() == 0)
{
ignerr << "SDF file doesn't contain a world. " <<
"If you wish to spawn a model, use the ResourceSpawner GUI plugin " <<
"or the 'world/<world_name>/create' service.\n";
return;
}

// Add record plugin
if (_config.UseLogRecord())
{
Expand Down
39 changes: 39 additions & 0 deletions src/Server_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,45 @@ TEST_P(ServerFixture, AddResourcePaths)
}
}

/////////////////////////////////////////////////
TEST_P(ServerFixture, SdfWithoutWorld)
{
// Initialize logging
std::string logBasePath = common::joinPaths(PROJECT_BINARY_PATH, "tmp");
common::setenv(IGN_HOMEDIR, logBasePath);
std::string path = common::uuid();
ignLogInit(path, "test.log");

// Start server with model SDF file
ServerConfig serverConfig;
serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "models", "sphere", "model.sdf"));

gz::sim::Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));

// Check error message in log file
std::string logFullPath = common::joinPaths(logBasePath, path, "test.log");
std::ifstream ifs(logFullPath.c_str(), std::ios::in);
bool errFound = false;
while ((!errFound) && (!ifs.eof()))
{
std::string line;
std::getline(ifs, line);
std::string errString = "SDF file doesn't contain a world. ";
errString += "If you wish to spawn a model, ";
errString += "use the ResourceSpawner GUI plugin ";
errString += "or the 'world/<world_name>/create' service.";
errFound = (line.find(errString) != std::string::npos);
}
EXPECT_TRUE(errFound);

// Stop logging
ignLogClose();
common::removeAll(logBasePath);
}

// Run multiple times. We want to make sure that static globals don't cause
// problems.
INSTANTIATE_TEST_SUITE_P(ServerRepeat, ServerFixture, ::testing::Range(1, 2));