diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 00000000..0bd4aa6a --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,110 @@ +load( + "@gz//bazel/skylark:build_defs.bzl", + "GZ_FEATURES", + "GZ_ROOT", + "GZ_VISIBILITY", + "gz_configure_header", + "gz_export_header", + "gz_include_header", +) +load( + "@gz//bazel/lint:lint.bzl", + "add_lint_tests", +) + +package( + default_visibility = GZ_VISIBILITY, + features = GZ_FEATURES, +) + +licenses(["notice"]) # Apache-2.0 + +exports_files(["LICENSE"]) + +gz_configure_header( + name = "fuel_tools_config_hh", + src = "include/gz/fuel_tools/config.hh.in", + cmakelists = ["CMakeLists.txt"], + defines = { + # These definitions are unused, + # this is merely to suppress generator warnings + "CMAKE_INSTALL_PREFIX": "unused", + }, + package = "fuel_tools", +) + +gz_export_header( + name = "include/gz/fuel_tools/Export.hh", + export_base = "GZ_FUEL_TOOLS", + lib_name = "gz-fuel_tools", + visibility = ["//visibility:private"], +) + +public_headers_no_gen = glob([ + "include/gz/fuel_tools/*.hh", +]) + +private_headers = glob(["src/*.hh"]) + +sources = glob( + ["src/*.cc"], + exclude = [ + "src/gz.cc", + "src/*_TEST.cc", + ], +) + +gz_include_header( + name = "fuel_tools_hh_genrule", + out = "include/gz/fuel_tools.hh", + hdrs = public_headers_no_gen + [ + "include/gz/fuel_tools/config.hh", + "include/gz/fuel_tools/Export.hh", + ], +) + +public_headers = public_headers_no_gen + [ + "include/gz/fuel_tools/config.hh", + "include/gz/fuel_tools/Export.hh", + "include/gz/fuel_tools.hh", +] + +cc_library( + name = "fuel_tools", + srcs = sources + private_headers, + hdrs = public_headers, + includes = ["include"], + deps = [ + GZ_ROOT + "common", + GZ_ROOT + "msgs", + "@curl", + "@jsoncpp", + "@yaml", + "@zip", + ], +) + +test_sources = glob( + include = ["src/*_TEST.cc"], + exclude = [ + "src/gz_TEST.cc", + "src/gz_src_TEST.cc", + ], +) + +[cc_test( + name = src.replace("/", "_").replace(".cc", "").replace("src_", ""), + srcs = [src], + env = { + "GZ_BAZEL": "1", + "GZ_BAZEL_PATH": "fuel_tools", + }, + deps = [ + ":fuel_tools", + GZ_ROOT + "common/testing", + "@gtest", + "@gtest//:gtest_main", + ], +) for src in test_sources] + +add_lint_tests() diff --git a/CMakeLists.txt b/CMakeLists.txt index 16920633..f6b06899 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,7 @@ set(GZ_UTILS_VER ${gz-utils2_VERSION_MAJOR}) #-------------------------------------- # Find gz-common -gz_find_package(gz-common5 REQUIRED PRIVATE) +gz_find_package(gz-common5 REQUIRED PRIVATE COMPONENTS testing) set(GZ_COMMON_VER ${gz-common5_VERSION_MAJOR}) #-------------------------------------- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aad437d8..c2ec97ac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -70,6 +70,7 @@ gz_build_tests(TYPE UNIT TEST_LIST test_targets LIB_DEPS gz-common${GZ_COMMON_VER}::gz-common${GZ_COMMON_VER} + gz-common${GZ_COMMON_VER}::testing TINYXML2::TINYXML2 ) diff --git a/src/ClientConfig_TEST.cc b/src/ClientConfig_TEST.cc index ea672e27..efff93c0 100644 --- a/src/ClientConfig_TEST.cc +++ b/src/ClientConfig_TEST.cc @@ -20,28 +20,16 @@ #include #include #include +#include +#include #include + +#include "gz/fuel_tools/config.hh" #include "gz/fuel_tools/ClientConfig.hh" -#include "test_config.hh" using namespace gz; using namespace fuel_tools; -///////////////////////////////////////////////// -/// \brief Helper to remove file according to OS, while Windows -/// has this issue: -/// https://github.com/gazebosim/gz-common/issues/51 -/// \todo(anyone) Remove this once Windows issue is solved. -/// \param[in] _path Path to file to be removed. -void removeFileTemp(const std::string &_path) -{ -#ifndef _WIN32 - EXPECT_TRUE(gz::common::removeFile(_path)); -#else - gz::common::removeFile(_path); -#endif -} - ///////////////////////////////////////////////// /// \brief Get home directory. /// \return Home directory or empty string if home wasn't found. @@ -54,26 +42,34 @@ std::string homePath() #else gz::common::env("USERPROFILE", homePath); #endif - return homePath; } ///////////////////////////////////////////////// -/// \brief Get cache directory. -/// \return Cache directory -/// \ToDo: Move this function to gz::common::Filesystem -std::string cachePath() +class ClientConfigTest: public ::testing::Test { -#ifndef _WIN32 - return std::string("/tmp/gz/fuel"); -#else - return std::string("C:\\Windows\\Temp"); -#endif -} + public: void SetUp() override + { + gz::common::Console::SetVerbosity(4); + tempDir = gz::common::testing::MakeTestTempDirectory(); + ASSERT_TRUE(tempDir->Valid()) << tempDir->Path(); + + gz::common::chdir(tempDir->Path()); + } + + public: std::string cachePath() + { + return this->tempDir->Path(); + } + + public: std::shared_ptr tempDir; +}; + +class ServerConfigTest: public ClientConfigTest {}; ///////////////////////////////////////////////// /// \brief Initially only the default server in config -TEST(ClientConfig, InitiallyDefaultServers) +TEST_F(ClientConfigTest, InitiallyDefaultServers) { ClientConfig config; EXPECT_EQ(2u, config.Servers().size()); @@ -81,7 +77,7 @@ TEST(ClientConfig, InitiallyDefaultServers) ///////////////////////////////////////////////// /// \brief Servers can be added -TEST(ClientConfig, ServersCanBeAdded) +TEST_F(ClientConfigTest, ServersCanBeAdded) { ClientConfig config; ServerConfig srv; @@ -94,7 +90,7 @@ TEST(ClientConfig, ServersCanBeAdded) ///////////////////////////////////////////////// /// \brief We can load the default configuration file. -TEST(ClientConfig, CustomDefaultConfiguration) +TEST_F(ClientConfigTest, CustomDefaultConfiguration) { ClientConfig config; ASSERT_EQ(2u, config.Servers().size()); @@ -110,7 +106,7 @@ TEST(ClientConfig, CustomDefaultConfiguration) ///////////////////////////////////////////////// /// \brief We can load custom settings in a configuration file. -TEST(ClientConfig, CustomConfiguration) +TEST_F(ClientConfigTest, CustomConfiguration) { ClientConfig config; @@ -147,13 +143,11 @@ TEST(ClientConfig, CustomConfiguration) config.Servers().back().Url().Str()); EXPECT_EQ(cachePath(), config.CacheLocation()); - // Remove the configuration file. - removeFileTemp(testPath); } ///////////////////////////////////////////////// /// \brief A server contains an already used URL. -TEST(ClientConfig, RepeatedServerConfiguration) +TEST_F(ClientConfigTest, RepeatedServerConfiguration) { ClientConfig config; @@ -178,14 +172,11 @@ TEST(ClientConfig, RepeatedServerConfiguration) ofs.close(); EXPECT_TRUE(config.LoadConfig(testPath)); - - // Remove the configuration file. - removeFileTemp(testPath); } ///////////////////////////////////////////////// /// \brief A server without URL is not valid. -TEST(ClientConfig, NoServerUrlConfiguration) +TEST_F(ClientConfigTest, NoServerUrlConfiguration) { ClientConfig config; @@ -203,14 +194,11 @@ TEST(ClientConfig, NoServerUrlConfiguration) ofs.close(); EXPECT_FALSE(config.LoadConfig(testPath)); - - // Remove the configuration file. - removeFileTemp(testPath); } ///////////////////////////////////////////////// /// \brief A server with an empty URL is not valid. -TEST(ClientConfig, EmptyServerUrlConfiguration) +TEST_F(ClientConfigTest, EmptyServerUrlConfiguration) { ClientConfig config; @@ -228,14 +216,11 @@ TEST(ClientConfig, EmptyServerUrlConfiguration) ofs.close(); EXPECT_FALSE(config.LoadConfig(testPath)); - - // Remove the configuration file. - removeFileTemp(testPath); } ///////////////////////////////////////////////// /// \brief The "cache" option requires to set "path". -TEST(ClientConfig, NoCachePathConfiguration) +TEST_F(ClientConfigTest, NoCachePathConfiguration) { ClientConfig config; @@ -250,14 +235,11 @@ TEST(ClientConfig, NoCachePathConfiguration) ofs.close(); EXPECT_FALSE(config.LoadConfig(testPath)); - - // Remove the configuration file. - removeFileTemp(testPath); } ///////////////////////////////////////////////// /// \brief The path parameter cannot be empty. -TEST(ClientConfig, EmptyCachePathConfiguration) +TEST_F(ClientConfigTest, EmptyCachePathConfiguration) { ClientConfig config; @@ -273,13 +255,10 @@ TEST(ClientConfig, EmptyCachePathConfiguration) ofs.close(); EXPECT_FALSE(config.LoadConfig(testPath)); - - // Remove the configuration file. - removeFileTemp(testPath); } ///////////////////////////////////////////////// -TEST(ClientConfig, UserAgent) +TEST_F(ClientConfigTest, UserAgent) { ClientConfig config; EXPECT_EQ("IgnitionFuelTools-" GZ_FUEL_TOOLS_VERSION_FULL, @@ -290,7 +269,7 @@ TEST(ClientConfig, UserAgent) } ///////////////////////////////////////////////// -TEST(ServerConfig, ApiKey) +TEST_F(ServerConfigTest, ApiKey) { ServerConfig config; EXPECT_TRUE(config.ApiKey().empty()); @@ -303,7 +282,7 @@ TEST(ServerConfig, ApiKey) } ///////////////////////////////////////////////// -TEST(ClientConfig, AsString) +TEST_F(ClientConfigTest, AsString) { common::Console::SetVerbosity(4); { @@ -366,7 +345,7 @@ TEST(ClientConfig, AsString) } ///////////////////////////////////////////////// -TEST(ClientConfig, AsPrettyString) +TEST_F(ClientConfigTest, AsPrettyString) { common::Console::SetVerbosity(4); @@ -394,7 +373,7 @@ TEST(ClientConfig, AsPrettyString) } ///////////////////////////////////////////////// -TEST(ServerConfig, Url) +TEST_F(ServerConfigTest, Url) { // Invalid URL string { diff --git a/src/FuelClient_TEST.cc b/src/FuelClient_TEST.cc index 38385bcd..ade73331 100644 --- a/src/FuelClient_TEST.cc +++ b/src/FuelClient_TEST.cc @@ -26,15 +26,7 @@ #include "gz/fuel_tools/Result.hh" #include "gz/fuel_tools/WorldIdentifier.hh" -#include "test_config.hh" - -#ifdef _WIN32 -#include -#define ChangeDirectory _chdir -#else -#include -#define ChangeDirectory chdir -#endif +#include using namespace gz; using namespace gz::fuel_tools; @@ -126,12 +118,21 @@ void createLocalWorld(ClientConfig &_conf) } ///////////////////////////////////////////////// -class FuelClientTest : public ::testing::Test +class FuelClientTest: public ::testing::Test { public: void SetUp() override { gz::common::Console::SetVerbosity(4); + tempDir = gz::common::testing::MakeTestTempDirectory(); + ASSERT_TRUE(tempDir->Valid()) << tempDir->Path(); + + gz::common::chdir(tempDir->Path()); + ASSERT_FALSE(common::exists("test_cache")); + ASSERT_TRUE(common::createDirectories("test_cache")); + ASSERT_TRUE(common::isDirectory("test_cache")); } + + public: std::shared_ptr tempDir; }; ///////////////////////////////////////////////// @@ -414,10 +415,6 @@ INSTANTIATE_TEST_SUITE_P( // https://github.com/gazebosim/gz-fuel-tools/issues/105 TEST_P(FuelClientDownloadTest, DownloadModel) { - // Configure to use binary path as cache - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - common::removeAll("test_cache"); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig config; config.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); @@ -621,10 +618,6 @@ TEST_P(FuelClientDownloadTest, DownloadModel) // https://github.com/gazebosim/gz-fuel-tools/issues/106 TEST_F(FuelClientTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(ModelDependencies)) { - // Configure to use binary path as cache - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - common::removeAll("test_cache"); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig config; config.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); @@ -697,10 +690,6 @@ TEST_F(FuelClientTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(ModelDependencies)) // See https://github.com/gazebosim/gz-fuel-tools/issues/231 TEST_F(FuelClientTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(CachedModel)) { - // Configure to use binary path as cache and populate it - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - common::removeAll("test_cache"); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig config; config.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); createLocalModel(config); @@ -1089,11 +1078,6 @@ TEST_F(FuelClientTest, ParseWorldFileUrl) // https://github.com/gazebosim/gz-fuel-tools/issues/105 TEST_F(FuelClientTest, DownloadWorld) { - // Configure to use binary path as cache - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - common::removeAll("test_cache"); - ASSERT_TRUE(common::createDirectories("test_cache")); - ServerConfig server; server.SetUrl(common::URI( "https://fuel.gazebosim.org")); @@ -1167,10 +1151,6 @@ TEST_F(FuelClientTest, DownloadWorld) // https://github.com/gazebosim/gz-fuel-tools/issues/106 TEST_F(FuelClientTest, CachedWorld) { - // Configure to use binary path as cache and populate it - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - common::removeAll("test_cache"); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig config; config.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); createLocalWorld(config); diff --git a/src/Interface_TEST.cc b/src/Interface_TEST.cc index e389b4ee..3d5435c4 100644 --- a/src/Interface_TEST.cc +++ b/src/Interface_TEST.cc @@ -23,30 +23,34 @@ #include "gz/fuel_tools/FuelClient.hh" #include "gz/fuel_tools/Interface.hh" -#include "test_config.hh" - -#ifdef _WIN32 -#include -#define ChangeDirectory _chdir -#else -#include -#define ChangeDirectory chdir -#endif +#include using namespace gz; using namespace gz::fuel_tools; +///////////////////////////////////////////////// +class InterfaceTest: public ::testing::Test +{ + public: void SetUp() override + { + gz::common::Console::SetVerbosity(4); + tempDir = gz::common::testing::MakeTestTempDirectory(); + ASSERT_TRUE(tempDir->Valid()) << tempDir->Path(); + + gz::common::chdir(tempDir->Path()); + ASSERT_FALSE(common::exists("test_cache")); + ASSERT_TRUE(common::createDirectories("test_cache")); + ASSERT_TRUE(common::isDirectory("test_cache")); + } + + public: std::shared_ptr tempDir; +}; + ///////////////////////////////////////////////// // Protocol "https" not supported or disabled in libcurl for Windows // https://github.com/gazebosim/gz-fuel-tools/issues/105 -TEST(Interface, FetchResources) +TEST_F(InterfaceTest, FetchResources) { - common::Console::SetVerbosity(4); - - // Configure to use binary path as cache - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - common::removeAll("test_cache"); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig config; config.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); diff --git a/src/LocalCache_TEST.cc b/src/LocalCache_TEST.cc index 15b93384..4e9844b4 100644 --- a/src/LocalCache_TEST.cc +++ b/src/LocalCache_TEST.cc @@ -22,21 +22,13 @@ #include #include #include +#include #include #include "gz/fuel_tools/ClientConfig.hh" #include "gz/fuel_tools/WorldIdentifier.hh" #include "LocalCache.hh" -#include "test_config.hh" - -#ifdef _WIN32 -#include -#define ChangeDirectory _chdir -#else -#include -#define ChangeDirectory chdir -#endif using namespace gz; using namespace fuel_tools; @@ -219,7 +211,16 @@ class LocalCacheTest : public ::testing::Test public: void SetUp() override { gz::common::Console::SetVerbosity(4); + tempDir = gz::common::testing::MakeTestTempDirectory(); + ASSERT_TRUE(tempDir->Valid()) << tempDir->Path(); + + gz::common::chdir(tempDir->Path()); + ASSERT_FALSE(common::exists("test_cache")); + ASSERT_TRUE(common::createDirectories("test_cache")); + ASSERT_TRUE(common::isDirectory("test_cache")); } + + public: std::shared_ptr tempDir; }; ///////////////////////////////////////////////// @@ -227,9 +228,6 @@ class LocalCacheTest : public ::testing::Test // See https://github.com/gazebosim/gz-fuel-tools/issues/231 TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(AllModels)) { - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - EXPECT_TRUE(common::removeAll("test_cache")); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig conf; conf.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); createLocal6Models(conf); @@ -256,9 +254,6 @@ TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(AllModels)) // See https://github.com/gazebosim/gz-fuel-tools/issues/307 TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(MatchingModels)) { - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - EXPECT_TRUE(common::removeAll("test_cache")); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig conf; conf.Clear(); conf.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); @@ -303,9 +298,6 @@ TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(MatchingModels)) // See https://github.com/gazebosim/gz-fuel-tools/issues/307 TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(MatchingModel)) { - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - EXPECT_TRUE(common::removeAll("test_cache")); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig conf; conf.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); createLocal6Models(conf); @@ -360,9 +352,6 @@ TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(MatchingModel)) // See https://github.com/gazebosim/gz-fuel-tools/issues/307 TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(AllWorlds)) { - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - EXPECT_TRUE(common::removeAll("test_cache")); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig conf; conf.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); createLocal6Worlds(conf); @@ -393,9 +382,6 @@ TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(AllWorlds)) // See https://github.com/gazebosim/gz-fuel-tools/issues/307 TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(MatchingWorlds)) { - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - EXPECT_TRUE(common::removeAll("test_cache")); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig conf; conf.Clear(); conf.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); @@ -428,9 +414,6 @@ TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(MatchingWorlds)) // See https://github.com/gazebosim/gz-fuel-tools/issues/307 TEST_F(LocalCacheTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(MatchingWorld)) { - ASSERT_EQ(0, ChangeDirectory(PROJECT_BINARY_PATH)); - EXPECT_TRUE(common::removeAll("test_cache")); - ASSERT_TRUE(common::createDirectories("test_cache")); ClientConfig conf; conf.SetCacheLocation(common::joinPaths(common::cwd(), "test_cache")); createLocal6Worlds(conf); diff --git a/src/RestClient_TEST.cc b/src/RestClient_TEST.cc index b88c93b9..748794b1 100644 --- a/src/RestClient_TEST.cc +++ b/src/RestClient_TEST.cc @@ -18,7 +18,6 @@ #include #include #include "gz/fuel_tools/RestClient.hh" -#include "test_config.hh" ///////////////////////////////////////////////// TEST(RestClient, UserAgent) diff --git a/test/BUILD.bazel b/test/BUILD.bazel new file mode 100644 index 00000000..5a263079 --- /dev/null +++ b/test/BUILD.bazel @@ -0,0 +1,6 @@ +load( + "@gz//bazel/lint:lint.bzl", + "add_lint_tests", +) + +add_lint_tests()