From 5a888c9d88e8e2f0d51ca2a5c32d74525b332ecf Mon Sep 17 00:00:00 2001 From: Henrique-BO Date: Sat, 27 May 2023 11:01:08 +0200 Subject: [PATCH 1/4] Add error message when trying to load model as world Signed-off-by: Henrique-BO --- src/Server.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Server.cc b/src/Server.cc index deb622cf5d..a3595885dd 100644 --- a/src/Server.cc +++ b/src/Server.cc @@ -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"; + return; + } + // Add record plugin if (_config.UseLogRecord()) { From 611932cbe324b9ae2b5e1bf75fda0f8c61043a72 Mon Sep 17 00:00:00 2001 From: Henrique-BO Date: Sun, 28 May 2023 16:10:04 +0200 Subject: [PATCH 2/4] Add SdfWithoutWorld test to Server Signed-off-by: Henrique-BO --- src/Server_TEST.cc | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Server_TEST.cc b/src/Server_TEST.cc index a2ff63ae55..57f9cc34e9 100644 --- a/src/Server_TEST.cc +++ b/src/Server_TEST.cc @@ -1088,6 +1088,41 @@ 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(std::string(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::ifstream ifs(common::joinPaths(logBasePath, path, "test.log").c_str(), std::ios::in); + bool errFound = false; + while ((!errFound) && (!ifs.eof())) + { + std::string line; + std::getline(ifs, line); + errFound = (line.find("SDF file doesn't contain a world.") != 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)); From 9059a2b3073f4de18920775245ed66dba432a62b Mon Sep 17 00:00:00 2001 From: Henrique-BO Date: Mon, 29 May 2023 16:23:32 +0200 Subject: [PATCH 3/4] Use joinPaths and fix codecheck Signed-off-by: Henrique-BO --- src/Server_TEST.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Server_TEST.cc b/src/Server_TEST.cc index 57f9cc34e9..033031e226 100644 --- a/src/Server_TEST.cc +++ b/src/Server_TEST.cc @@ -1099,21 +1099,23 @@ TEST_P(ServerFixture, SdfWithoutWorld) // Start server with model SDF file ServerConfig serverConfig; - serverConfig.SetSdfFile(std::string(PROJECT_SOURCE_PATH) + - "/test/worlds/models/sphere/model.sdf"); + 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::ifstream ifs(common::joinPaths(logBasePath, path, "test.log").c_str(), std::ios::in); + 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); - errFound = (line.find("SDF file doesn't contain a world.") != std::string::npos); + std::string errString = "SDF file doesn't contain a world."; + errFound = (line.find(errString) != std::string::npos); } EXPECT_TRUE(errFound); @@ -1122,7 +1124,6 @@ TEST_P(ServerFixture, SdfWithoutWorld) 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)); From bee313056ead1586e3bb6f7005be0c8afc460fae Mon Sep 17 00:00:00 2001 From: Henrique-BO Date: Wed, 7 Jun 2023 22:46:49 +0200 Subject: [PATCH 4/4] Update error message Signed-off-by: Henrique-BO --- src/Server.cc | 4 +++- src/Server_TEST.cc | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Server.cc b/src/Server.cc index a3595885dd..51ad4b2e0c 100644 --- a/src/Server.cc +++ b/src/Server.cc @@ -184,7 +184,9 @@ Server::Server(const ServerConfig &_config) if (this->dataPtr->sdfRoot.WorldCount() == 0) { - ignerr << "SDF file doesn't contain a world.\n"; + ignerr << "SDF file doesn't contain a world. " << + "If you wish to spawn a model, use the ResourceSpawner GUI plugin " << + "or the 'world//create' service.\n"; return; } diff --git a/src/Server_TEST.cc b/src/Server_TEST.cc index 033031e226..4a16d723db 100644 --- a/src/Server_TEST.cc +++ b/src/Server_TEST.cc @@ -1114,7 +1114,10 @@ TEST_P(ServerFixture, SdfWithoutWorld) { std::string line; std::getline(ifs, line); - std::string errString = "SDF file doesn't contain a world."; + 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//create' service."; errFound = (line.find(errString) != std::string::npos); } EXPECT_TRUE(errFound);