Elements 6.3.3
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
ElementsKernel/tests/src/Configuration_test.cpp

This is an example of how to use the TempDir and TemEnv classes.

#include "ElementsKernel/Configuration.h" // header to test
#include <boost/filesystem/operations.hpp> // for exists, create_directory, is_regular
#include <boost/filesystem/path.hpp> // for operator/, path
#include <boost/test/unit_test.hpp>
#include <algorithm> // for copy_if, for_each
#include <iterator> // for distance
#include <string> // for allocator, string
#include <vector> // for vector
#include "ElementsKernel/Exception.h" // for Exception
#include "ElementsKernel/Path.h" // for Item, join
#include "ElementsKernel/System.h" // for DEFAULT_INSTALL_PREFIX
#include "ElementsKernel/Temporary.h" // for TempDir, TempEnv
using boost::filesystem::exists;
using boost::filesystem::is_regular;
namespace Elements {
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// Begin of the Boost tests
//
//-----------------------------------------------------------------------------
struct Configuration_Fixture {
TempDir m_top_dir;
vector<Path::Item> m_item_list;
vector<Path::Item> m_target_item_list;
vector<Path::Item> m_real_item_list;
vector<Path::Item> m_target_real_item_list;
Configuration_Fixture() : m_top_dir{"Configuration_test-%%%%%%%"} {
using std::copy_if;
m_item_list.emplace_back(m_top_dir.path() / "test1");
m_item_list.emplace_back(m_top_dir.path() / "test1" / "foo");
m_item_list.emplace_back(m_top_dir.path() / "test2");
m_item_list.emplace_back(m_top_dir.path() / "test3");
for_each(m_item_list.cbegin(), m_item_list.cend(), [](Path::Item p) {
boost::filesystem::create_directory(p);
});
m_item_list.emplace_back(m_top_dir.path() / "test4");
m_target_item_list = m_item_list;
m_target_item_list.emplace_back(Path::Item(System::DEFAULT_INSTALL_PREFIX) / "share" / "conf");
m_real_item_list.resize(m_item_list.size());
auto it = copy_if(m_item_list.begin(), m_item_list.end(), m_real_item_list.begin(), [](const Path::Item& p) {
return exists(p);
});
m_real_item_list.erase(it, m_real_item_list.end());
m_target_real_item_list.resize(m_target_item_list.size());
auto it2 = copy_if(m_target_item_list.begin(), m_target_item_list.end(), m_target_real_item_list.begin(),
[](const Path::Item& p) {
return exists(p);
});
m_target_real_item_list.erase(it2, m_target_real_item_list.end());
}
~Configuration_Fixture() {}
};
BOOST_AUTO_TEST_SUITE(Configuration_test)
//-----------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(ConfigurationException_test) {
BOOST_CHECK_THROW(getConfigurationPath("NonExistingFile.conf"), Exception);
}
BOOST_AUTO_TEST_CASE(ConfigurationVariableName_test) {
BOOST_CHECK_EQUAL(getConfigurationVariableName(), "ELEMENTS_CONF_PATH");
}
BOOST_FIXTURE_TEST_CASE(getFromLocations_test, Configuration_Fixture) {
auto env = TempEnv();
env["ELEMENTS_CONF_PATH"] = Path::join(m_item_list);
auto locations = getConfigurationLocations();
BOOST_CHECK_EQUAL_COLLECTIONS(locations.begin(), locations.end(), m_target_item_list.begin(),
m_target_item_list.end());
}
BOOST_FIXTURE_TEST_CASE(getFromLocationsExist_test, Configuration_Fixture) {
auto env = TempEnv();
env["ELEMENTS_CONF_PATH"] = Path::join(m_real_item_list);
auto locations = getConfigurationLocations(true);
BOOST_CHECK_EQUAL_COLLECTIONS(locations.begin(), locations.end(), m_target_real_item_list.begin(),
m_target_real_item_list.end());
}
BOOST_FIXTURE_TEST_CASE(NamespaceAlias_test, Configuration_Fixture) {
{
auto env = TempEnv();
env["ELEMENTS_CONF_PATH"] = Path::join(m_real_item_list);
auto locations = Configuration::getLocations(true);
BOOST_CHECK_EQUAL_COLLECTIONS(locations.begin(), locations.end(), m_target_real_item_list.begin(),
m_target_real_item_list.end());
}
BOOST_CHECK_EQUAL(Configuration::getVariableName(), "ELEMENTS_CONF_PATH");
BOOST_CHECK_THROW(Configuration::getPath("NonExistingFile.conf"), Exception);
}
BOOST_AUTO_TEST_SUITE_END()
//-----------------------------------------------------------------------------
//
// End of the Boost tests
//
//-----------------------------------------------------------------------------
} // namespace Elements
provide functions to retrieve configuration files
defines the base Elements exception class
provide functions to retrieve resources pointed by environment variables
This file is intended to iron out all the differences between systems (currently Linux and MacOSX)
Handling of temporary files, directories and environments.
T cbegin(T... args)
STL class.
T copy_if(T... args)
T distance(T... args)
T emplace_back(T... args)
T cend(T... args)
T erase(T... args)
T for_each(T... args)
ELEMENTS_API std::string getConfigurationVariableName()
retrieve the variable name used for the configuration file lookup
ELEMENTS_API Path::Item getConfigurationPath(const T &file_name, bool raise_exception=true)
ELEMENTS_API std::vector< Path::Item > getConfigurationLocations(bool exist_only=false)
Environment TempEnv
Definition Temporary.h:67
T resize(T... args)
T size(T... args)