gnucash/libgnucash/core-utils/test/gtest-path-utilities.cpp
John Ralls 66817bb997 Rework directory determination in CMake builds.
Sets paths for finding componenents depending on the state of ENABLE_BINRELOC,
GNC_UNINSTALLED, GNC_BUILDDIR and whether any install paths have been set
outside of CMAKE_INSTALL_PREFIX.

GNUInstallDirs changes the name of CMAKE_INSTALL_LIBDIR depending on the
operating system and distro. When CMAKE_INSTALL_PREFIX is /usr,
/usr/local, or any subdirectory of /opt it also changes
CMAKE_INSTALL_FULL_SYSCONFDIR to /etc. An earlier commit by Aaron Laws
mirrors the name of CMAKE_INSTALL_LIBDIR to the build library directory.

It's possible for builders to set any of the install directories
anywhere they please.

Setting any directory outside of CMAKE_INSTALL_PREFIX breaks Binreloc so
the toplevel CMakeLists.txt now detects that and disables Binreloc.

If Binreloc is enabled then all path queries use it to find paths. This
works in the build directory because the gnucash executable and all of
the test programs are in build_directory/bin and LIBDIR, DATADIR, and
SYSCONFDIR can be found in the same root path.

If Binreloc is disabled then in order to build or run programs from the
build directory one must set GNC_UNINSTALLED and set GNC_BUILDDIR to the
absolute path of the build directory. When those are set GNC_BUILDDIR
replaces CMAKE_INSTALL_PREFIX in all paths that are subdirectories of
CMAKE_INSTALL_PREFIX; paths that are not in CMAKE_INSTALL_PREFIX are
appended whole to GNC_BUILDDIR. This process is constent between CMake
and gnc_path_get_foo. GnuCash is unlikely to run from a DESTDIR without
Binreloc.
2017-12-05 17:25:52 -08:00

126 lines
3.4 KiB
C++

extern "C"
{
#include <config.h>
#include <glib.h>
#include <gncla-dir.h>
#include <gnc-path.h>
#include <binreloc.h>
#include <gnc-filepath-utils.h>
}
#include <gtest/gtest.h>
struct PathTest : public testing::Test
{
PathTest() : m_prefix{nullptr} {}
void SetUp()
{
#ifdef ENABLE_BINRELOC
gnc_gbr_init(nullptr);
#endif
char *builddir = g_strdup(g_getenv("GNC_BUILDDIR"));
if (builddir)
m_prefix = builddir;
else
m_prefix = g_get_current_dir();
}
void TearDown()
{
if (m_prefix)
g_free(m_prefix);
}
char *m_prefix;
};
TEST_F(PathTest, gnc_path_get_prefix)
{
#ifdef ENABLE_BINRELOC
EXPECT_STREQ(gnc_path_get_prefix(), m_prefix);
#else
g_setenv("GNC_UNINSTALLED", "1", TRUE);
g_setenv("GNC_BUILDDIR", m_prefix, 1);
EXPECT_STREQ(gnc_path_get_prefix(), m_prefix);
g_unsetenv("GNC_UNINSTALLED");
g_unsetenv("GNC_BUILDDIR");
EXPECT_STREQ(gnc_path_get_prefix(), PREFIX);
#endif
}
TEST_F(PathTest, gnc_path_get_bindir)
{
gchar *dirname = gnc_file_path_relative_part(PREFIX, BINDIR);
gchar *binpath = g_build_filename(m_prefix, dirname, NULL);
g_free(dirname);
#ifdef ENABLE_BINRELOC
EXPECT_STREQ(gnc_path_get_bindir(), binpath);
g_free(binpath);
#else
g_setenv("GNC_UNINSTALLED", "1", TRUE);
g_setenv("GNC_BUILDDIR", m_prefix, 1);
EXPECT_STREQ(gnc_path_get_bindir(), binpath);
g_free(binpath);
g_unsetenv("GNC_UNINSTALLED");
g_unsetenv("GNC_BUILDDIR");
EXPECT_STREQ(gnc_path_get_bindir(), BINDIR);
#endif
}
TEST_F(PathTest, gnc_path_get_libdir)
{
gchar *dirname = gnc_file_path_relative_part(PREFIX, LIBDIR);
gchar *libpath = g_build_filename(m_prefix, dirname, NULL);
g_free(dirname);
#ifdef ENABLE_BINRELOC
EXPECT_STREQ(gnc_path_get_libdir(), libpath);
g_free(libpath);
#else
g_setenv("GNC_UNINSTALLED", "1", TRUE);
g_setenv("GNC_BUILDDIR", m_prefix, 1);
EXPECT_STREQ(gnc_path_get_libdir(), libpath);
g_free(libpath);
g_unsetenv("GNC_UNINSTALLED");
g_unsetenv("GNC_BUILDDIR");
EXPECT_STREQ(gnc_path_get_libdir(), LIBDIR);
#endif
}
TEST_F(PathTest, gnc_path_get_datadir)
{
gchar *dirname = gnc_file_path_relative_part(PREFIX, DATADIR);
gchar *datapath = g_build_filename(m_prefix, dirname, NULL);
g_free(dirname);
#ifdef ENABLE_BINRELOC
EXPECT_STREQ(gnc_path_get_datadir(), datapath);
g_free(datapath);
#else
g_setenv("GNC_UNINSTALLED", "1", TRUE);
g_setenv("GNC_BUILDDIR", m_prefix, 1);
EXPECT_STREQ(gnc_path_get_datadir(), datapath);
g_free(datapath);
g_unsetenv("GNC_UNINSTALLED");
g_unsetenv("GNC_BUILDDIR");
EXPECT_STREQ(gnc_path_get_datadir(), DATADIR);
#endif
}
TEST_F(PathTest, gnc_path_get_sysconfdir)
{
gchar *dirname = gnc_file_path_relative_part(PREFIX, SYSCONFDIR);
gchar *sysconfpath = g_build_filename(m_prefix, dirname, "gnucash", NULL);
g_free(dirname);
#ifdef ENABLE_BINRELOC
EXPECT_STREQ(gnc_path_get_pkgsysconfdir(), sysconfpath);
g_free(sysconfpath);
#else
g_setenv("GNC_UNINSTALLED", "1", TRUE);
g_setenv("GNC_BUILDDIR", m_prefix, 1);
EXPECT_STREQ(gnc_path_get_pkgsysconfdir(), sysconfpath);
g_free(sysconfpath);
g_unsetenv("GNC_UNINSTALLED");
g_unsetenv("GNC_BUILDDIR");
sysconfpath = g_build_filename(SYSCONFDIR, "gnucash", NULL);
EXPECT_STREQ(gnc_path_get_pkgsysconfdir(), sysconfpath);
g_free(sysconfpath);
#endif
}