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

Use a custom Fuel cache location for tests #213

Merged
merged 4 commits into from
Jun 24, 2020
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
20 changes: 19 additions & 1 deletion src/SdfGenerator_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "ignition/gazebo/components/World.hh"
#include "ignition/gazebo/test_config.hh"

#include "helpers/UniqueTestDirectoryEnv.hh"

#include "SdfGenerator.hh"

using namespace ignition;
Expand Down Expand Up @@ -142,9 +144,14 @@ class ElementUpdateFixture : public ::testing::Test
{
ignition::common::Console::SetVerbosity(4);

fuel_tools::ClientConfig config;
config.SetCacheLocation(test::UniqueTestDirectoryEnv::Path());
this->fuelClient = std::make_unique<fuel_tools::FuelClient>(config);

auto fuelCb = [&](const std::string &_uri)
{
auto out = fuel_tools::fetchResource(_uri);
auto out =
fuel_tools::fetchResourceWithClient(_uri, *this->fuelClient.get());
if (!out.empty())
{
this->includeUriMap[out] = _uri;
Expand Down Expand Up @@ -195,6 +202,7 @@ class ElementUpdateFixture : public ::testing::Test
public: std::unique_ptr<SdfEntityCreator> creator;
public: msgs::SdfGeneratorConfig sdfGenConfig;
public: sdf_generator::IncludeUriMap includeUriMap;
public: std::unique_ptr<fuel_tools::FuelClient> fuelClient;
};

/////////////////////////////////////////////////
Expand Down Expand Up @@ -854,3 +862,13 @@ TEST_F(GenerateWorldFixture, ModelsInline)
EXPECT_TRUE(isSubset(this->root.Element(), newRoot.Element()));
}
}

/////////////////////////////////////////////////
/// Main
int main(int _argc, char **_argv)
{
::testing::InitGoogleTest(&_argc, _argv);
::testing::AddGlobalTestEnvironment(
new test::UniqueTestDirectoryEnv("sdf_gen_test_cache"));
return RUN_ALL_TESTS();
}
101 changes: 101 additions & 0 deletions test/helpers/UniqueTestDirectoryEnv.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2020 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_GAZEBO_TEST_HELPERS_CUSTOMCACHEENV_HH_
#define IGNITION_GAZEBO_TEST_HELPERS_CUSTOMCACHEENV_HH_

#include <gtest/gtest.h>

#include <ignition/common/Console.hh>
#include <ignition/common/Filesystem.hh>
#include <ignition/gazebo/test_config.hh>

namespace ignition
{
namespace gazebo
{
namespace test
{
/// \brief Helper class to be used in internal tests. It sets up a unique test
/// directory inside the cmake build directory (PROJECT_BINARY_PATH). The name
/// of the directory is specified in the constructor argument, but if another
/// directory with the same name is found, a new one with a numeric suffix will
/// be created ensuring that the new name is unique. This class can
/// be used, for example, for setting custom cache locations for tests that
/// download Fuel models.
///
/// An instance of the Environment has to be added to gtest using the
/// `::testing::AddGlobalTestEnvironment` function.
///
/// ## Usage
///
/// // In the test file
/// int main(int _argc, char **_argv)
/// {
/// ::testing::InitGoogleTest(&_argc, _argv);
/// ::testing::AddGlobalTestEnvironment(
/// new ignition::gazebo::test::UniqueTestDirectoryEnv("custom_dir_name"));
/// return RUN_ALL_TESTS();
/// }
/// gtest is responsible for the instance, so there is no need to delete it.
class UniqueTestDirectoryEnv : public ::testing::Environment
{
/// \brief Constructor
/// \param[in] _dirName Directory name based on which a unique path is created
public: explicit UniqueTestDirectoryEnv(const std::string &_dirName)
{
// We don't assert here because the assertion in the constructor won't be
// caught by gtest.
if (_dirName.empty())
{
ignerr << "_dirName cannot be empty\n";
}
else
{
UniqueTestDirectoryEnv::Path() = common::uniqueDirectoryPath(
common::joinPaths(PROJECT_BINARY_PATH, _dirName));
}
}

public: void SetUp() override
{
ASSERT_FALSE(UniqueTestDirectoryEnv::Path().empty())
<< "UniqueTestDirectoryEnv is not configured properly";

common::createDirectory(UniqueTestDirectoryEnv::Path());
ASSERT_TRUE(common::exists(UniqueTestDirectoryEnv::Path()));
}

public: void TearDown() override
{
if (!UniqueTestDirectoryEnv::Path().empty())
{
common::removeAll(UniqueTestDirectoryEnv::Path());
UniqueTestDirectoryEnv::Path() = "";
}
}

public: static std::string &Path()
{
static std::string dirPath = "";
return dirPath;
}
};
}
}
}
#endif

Loading