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 3 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
6 changes: 6 additions & 0 deletions src/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ Server::Server(const ServerConfig &_config)
return;
}

if (this->dataPtr->sdfRoot.WorldCount() == 0)
{
ignerr << "SDF file doesn't contain a world.\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful to add that if the user wants to spawn a model, they could use the ResourceSpawner GUI plugin or the world/<world name/create service.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in bee3130.

return;
}

// Add record plugin
if (_config.UseLogRecord())
{
Expand Down
36 changes: 36 additions & 0 deletions src/Server_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,42 @@ 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.";
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));