Skip to content

Commit

Permalink
Support for bazel in Garden (#328)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <michael@openrobotics.org>
Signed-off-by: Michael Carroll <mjcarroll@intrinsic.ai>
Co-authored-by: Nate Koenig <natekoenig@gmail.com>
  • Loading branch information
mjcarroll and nkoenig authored Jun 21, 2023
1 parent f9fd649 commit 9036b58
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 134 deletions.
110 changes: 110 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

#--------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
95 changes: 37 additions & 58 deletions src/ClientConfig_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,16 @@
#include <string>
#include <gz/common/Console.hh>
#include <gz/common/Filesystem.hh>
#include <gz/common/TempDirectory.hh>
#include <gz/common/testing/TestPaths.hh>
#include <gz/common/Util.hh>

#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.
Expand All @@ -54,34 +42,42 @@ 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<gz::common::TempDirectory> 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());
}

/////////////////////////////////////////////////
/// \brief Servers can be added
TEST(ClientConfig, ServersCanBeAdded)
TEST_F(ClientConfigTest, ServersCanBeAdded)
{
ClientConfig config;
ServerConfig srv;
Expand All @@ -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());
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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,
Expand All @@ -290,7 +269,7 @@ TEST(ClientConfig, UserAgent)
}

/////////////////////////////////////////////////
TEST(ServerConfig, ApiKey)
TEST_F(ServerConfigTest, ApiKey)
{
ServerConfig config;
EXPECT_TRUE(config.ApiKey().empty());
Expand All @@ -303,7 +282,7 @@ TEST(ServerConfig, ApiKey)
}

/////////////////////////////////////////////////
TEST(ClientConfig, AsString)
TEST_F(ClientConfigTest, AsString)
{
common::Console::SetVerbosity(4);
{
Expand Down Expand Up @@ -366,7 +345,7 @@ TEST(ClientConfig, AsString)
}

/////////////////////////////////////////////////
TEST(ClientConfig, AsPrettyString)
TEST_F(ClientConfigTest, AsPrettyString)
{
common::Console::SetVerbosity(4);

Expand Down Expand Up @@ -394,7 +373,7 @@ TEST(ClientConfig, AsPrettyString)
}

/////////////////////////////////////////////////
TEST(ServerConfig, Url)
TEST_F(ServerConfigTest, Url)
{
// Invalid URL string
{
Expand Down
Loading

0 comments on commit 9036b58

Please sign in to comment.