summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
authorDaniel Playfair Cal <daniel.playfair.cal@gmail.com>2019-01-07 22:33:15 +1100
committerDaniel Playfair Cal <daniel.playfair.cal@gmail.com>2019-02-12 19:47:56 +1100
commit8cbaee1c341b97d81fc597a4571b459baaac5c11 (patch)
tree1a1d58e7110fb231f0ea04d9b8626ec283e4a3c2 /engine
parent38e625da7f47e457d150efedc598437e2e867ef7 (diff)
downloaddconf-8cbaee1c341b97d81fc597a4571b459baaac5c11.tar.gz
Tests: replace usage of dlsym with separate modules containing functions that need to be mocked out
Diffstat (limited to 'engine')
-rw-r--r--engine/dconf-engine-mockable.c39
-rw-r--r--engine/dconf-engine-mockable.h30
-rw-r--r--engine/dconf-engine-profile.c9
-rw-r--r--engine/meson.build20
4 files changed, 93 insertions, 5 deletions
diff --git a/engine/dconf-engine-mockable.c b/engine/dconf-engine-mockable.c
new file mode 100644
index 0000000..dce2f43
--- /dev/null
+++ b/engine/dconf-engine-mockable.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright © 2019 Daniel Playfair Cal
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the licence, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Daniel Playfair Cal <daniel.playfair.cal@gmail.com>
+ */
+
+/**
+ * This module contains the production implementations of methods used in
+ * dconf_shm that need to be mocked out for tests.
+ *
+ * In some cases, external methods are wrapped with a different name. This is
+ * done so that it is not necessary to redefine the external functions in
+ * unit tests in order to mock them out, and therefore easy to also call the
+ * non mocked versions in tests if necessary.
+ */
+
+#include "config.h"
+
+#include "dconf-engine-mockable.h"
+
+
+FILE *
+dconf_engine_fopen (const char *pathname, const char *mode)
+{
+ return fopen (pathname, mode);
+}
diff --git a/engine/dconf-engine-mockable.h b/engine/dconf-engine-mockable.h
new file mode 100644
index 0000000..091f6d3
--- /dev/null
+++ b/engine/dconf-engine-mockable.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright © 2019 Daniel Playfair Cal
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the licence, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Daniel Playfair Cal <daniel.playfair.cal@gmail.com>
+ */
+
+#ifndef __dconf_engine_mockable_h__
+#define __dconf_engine_mockable_h__
+
+#include <gio/gio.h>
+#include <stdio.h>
+
+G_GNUC_INTERNAL
+FILE *dconf_engine_fopen (const char *pathname,
+ const char *mode);
+
+#endif
diff --git a/engine/dconf-engine-profile.c b/engine/dconf-engine-profile.c
index b204bb4..6474248 100644
--- a/engine/dconf-engine-profile.c
+++ b/engine/dconf-engine-profile.c
@@ -21,6 +21,7 @@
#include "config.h"
#include "dconf-engine-profile.h"
+#include "dconf-engine-mockable.h"
#include <string.h>
#include <stdio.h>
@@ -207,7 +208,7 @@ dconf_engine_open_profile_file (const gchar *profile)
gchar *filename;
filename = g_build_filename (prefix, "dconf/profile", profile, NULL);
- fp = fopen (filename, "r");
+ fp = dconf_engine_fopen (filename, "r");
/* If it wasn't ENOENT then we don't want to continue on to check
* other paths. Fail immediately.
@@ -236,7 +237,7 @@ dconf_engine_open_mandatory_profile (void)
memcpy (path, MANDATORY_DIR, mdlen);
snprintf (path + mdlen, 20, "%u", (guint) getuid ());
- return fopen (path, "r");
+ return dconf_engine_fopen (path, "r");
}
static FILE *
@@ -253,7 +254,7 @@ dconf_engine_open_runtime_profile (void)
memcpy (path, runtime_dir, rdlen);
memcpy (path + rdlen, RUNTIME_PROFILE, sizeof RUNTIME_PROFILE);
- return fopen (path, "r");
+ return dconf_engine_fopen (path, "r");
}
DConfEngineSource **
@@ -311,7 +312,7 @@ dconf_engine_profile_open (const gchar *profile,
if (profile[0] != '/')
file = dconf_engine_open_profile_file (profile);
else
- file = fopen (profile, "r");
+ file = dconf_engine_fopen (profile, "r");
}
if (file != NULL)
diff --git a/engine/meson.build b/engine/meson.build
index d1a959d..ca46b60 100644
--- a/engine/meson.build
+++ b/engine/meson.build
@@ -1,4 +1,4 @@
-sources = files(
+testable_sources = files(
'dconf-engine.c',
'dconf-engine-profile.c',
'dconf-engine-source.c',
@@ -8,6 +8,10 @@ sources = files(
'dconf-engine-source-system.c',
)
+sources = testable_sources + files(
+ 'dconf-engine-mockable.c',
+)
+
engine_deps = [
libdconf_common_dep,
libgvdb_dep,
@@ -26,3 +30,17 @@ libdconf_engine_dep = declare_dependency(
dependencies: engine_deps,
link_with: libdconf_engine,
)
+
+libdconf_engine_test = static_library(
+ 'dconf-engine-test',
+ sources: testable_sources,
+ include_directories: top_inc,
+ dependencies: engine_deps + [libdconf_shm_dep],
+ c_args: dconf_c_args,
+ pic: true,
+)
+
+libdconf_engine_test_dep = declare_dependency(
+ dependencies: engine_deps,
+ link_with: libdconf_engine_test,
+)