diff --git a/Makefile b/Makefile index 8b9a919..f0d88bc 100644 --- a/Makefile +++ b/Makefile @@ -71,18 +71,17 @@ dunstify: dunstify.o .PHONY: test test-valgrind test-coverage test: test/test clean-coverage-run - cd test && ./test + ./test/test test-valgrind: test/test - cd ./test \ - && valgrind \ - --suppressions=../.valgrind.suppressions \ - --leak-check=full \ - --show-leak-kinds=definite \ - --errors-for-leak-kinds=definite \ - --num-callers=40 \ - --error-exitcode=123 \ - ./test + valgrind \ + --suppressions=.valgrind.suppressions \ + --leak-check=full \ + --show-leak-kinds=definite \ + --errors-for-leak-kinds=definite \ + --num-callers=40 \ + --error-exitcode=123 \ + ./test/test test-coverage: CFLAGS += -fprofile-arcs -ftest-coverage -O0 test-coverage: test diff --git a/test/icon.c b/test/icon.c index 616bef6..5bd96aa 100644 --- a/test/icon.c +++ b/test/icon.c @@ -10,24 +10,23 @@ #define IS_ICON_PNG(pb) 4 == gdk_pixbuf_get_width(pb) #define IS_ICON_SVG(pb) 16 == gdk_pixbuf_get_width(pb) +extern const char *base; + TEST test_get_pixbuf_from_file_tilde(void) { - char *cwd = g_get_current_dir(); const char *home = g_get_home_dir(); const char *iconpath = ICONPREFIX; - if (0 != strncmp(home, cwd, strlen(home))) { - g_free(cwd); + if (0 != strncmp(home, base, strlen(home))) { SKIPm("Current directory is not a subdirectory from user's home." " Cannot test iconpath tilde expansion.\n"); } - gchar *path = g_build_filename(cwd, iconpath, "valid", "icon1.svg", NULL); + gchar *path = g_build_filename(base, iconpath, "valid", "icon1.svg", NULL); path = string_replace_at(path, 0, strlen(home), "~"); GdkPixbuf *pixbuf = get_pixbuf_from_file(path); g_clear_pointer(&path, g_free); - g_clear_pointer(&cwd, g_free); ASSERT(pixbuf); ASSERTm("The wrong pixbuf is loaded in the icon file.", IS_ICON_SVG(pixbuf)); @@ -37,14 +36,12 @@ TEST test_get_pixbuf_from_file_tilde(void) TEST test_get_pixbuf_from_file_absolute(void) { - char *cwd = g_get_current_dir(); const char *iconpath = ICONPREFIX; - gchar *path = g_build_filename(cwd, iconpath, "valid", "icon1.svg", NULL); + gchar *path = g_build_filename(base, iconpath, "valid", "icon1.svg", NULL); GdkPixbuf *pixbuf = get_pixbuf_from_file(path); g_clear_pointer(&path, g_free); - g_clear_pointer(&cwd, g_free); ASSERT(pixbuf); ASSERTm("The wrong pixbuf is loaded in the icon file.", IS_ICON_SVG(pixbuf)); @@ -94,7 +91,7 @@ TEST test_get_pixbuf_from_icon_onlypng(void) TEST test_get_pixbuf_from_icon_filename(void) { - char *icon = string_append(g_get_current_dir(), "/data/icons/valid.png", NULL); + char *icon = g_strconcat(base, "/data/icons/valid.png", NULL); GdkPixbuf *pixbuf = get_pixbuf_from_icon(icon); ASSERT(pixbuf); ASSERTm("PNG pixbuf isn't loaded", IS_ICON_PNG(pixbuf)); @@ -106,24 +103,23 @@ TEST test_get_pixbuf_from_icon_filename(void) TEST test_get_pixbuf_from_icon_fileuri(void) { - char *curdir = g_get_current_dir(); - char *icon = g_strconcat("file://", curdir,"/data/icons/valid.svg", NULL); + char *icon = g_strconcat("file://", base, "/data/icons/valid.svg", NULL); GdkPixbuf *pixbuf = get_pixbuf_from_icon(icon); ASSERT(pixbuf); ASSERTm("SVG pixbuf isn't loaded", IS_ICON_SVG(pixbuf)); g_clear_pointer(&pixbuf, g_object_unref); g_free(icon); - g_free(curdir); PASS(); } SUITE(suite_icon) { - settings.icon_path = - "." ICONPREFIX "/invalid" - ":." ICONPREFIX "/valid" - ":." ICONPREFIX "/both"; + settings.icon_path = g_strconcat( + base, ICONPREFIX "/invalid" + ":", base, ICONPREFIX "/valid" + ":", base, ICONPREFIX "/both", + NULL); RUN_TEST(test_get_pixbuf_from_file_tilde); RUN_TEST(test_get_pixbuf_from_file_absolute); @@ -134,6 +130,6 @@ SUITE(suite_icon) RUN_TEST(test_get_pixbuf_from_icon_filename); RUN_TEST(test_get_pixbuf_from_icon_fileuri); - settings.icon_path = NULL; + g_clear_pointer(&settings.icon_path, g_free); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/test/notification.c b/test/notification.c index 893d279..87e6ef1 100644 --- a/test/notification.c +++ b/test/notification.c @@ -4,6 +4,8 @@ #include "src/option_parser.h" #include "src/settings.h" +extern const char *base; + TEST test_notification_is_duplicate_field(char **field, struct notification *a, struct notification *b) @@ -118,7 +120,8 @@ TEST test_notification_referencing(void) SUITE(suite_notification) { cmdline_load(0, NULL); - load_settings("data/dunstrc.default"); + char *config_path = g_strconcat(base, "/data/dunstrc.default", NULL); + load_settings(config_path); struct notification *a = notification_create(); a->appname = g_strdup("Test"); @@ -145,6 +148,7 @@ SUITE(suite_notification) RUN_TEST(test_notification_referencing); g_clear_pointer(&settings.icon_path, g_free); + g_free(config_path); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/test/option_parser.c b/test/option_parser.c index bb44954..48c2a11 100644 --- a/test/option_parser.c +++ b/test/option_parser.c @@ -1,6 +1,8 @@ #include "src/option_parser.c" #include "greatest.h" +extern const char *base; + TEST test_next_section(void) { const char *section = NULL; @@ -276,7 +278,8 @@ TEST test_option_get_bool(void) SUITE(suite_option_parser) { - FILE *config_file = fopen("data/test-ini", "r"); + char *config_path = g_strconcat(base, "/data/test-ini", NULL); + FILE *config_file = fopen(config_path, "r"); if (!config_file) { fputs("\nTest config file 'data/test-ini' couldn't be opened, failing.\n", stderr); exit(1); @@ -310,6 +313,8 @@ SUITE(suite_option_parser) RUN_TEST(test_option_get_int); RUN_TEST(test_option_get_double); RUN_TEST(test_option_get_bool); + + g_free(config_path); free_ini(); g_strfreev(argv); fclose(config_file); diff --git a/test/test.c b/test/test.c index 57133a5..258af1d 100644 --- a/test/test.c +++ b/test/test.c @@ -1,9 +1,14 @@ #include "greatest.h" #include +#include +#include +#include #include "src/log.h" +const char *base; + SUITE_EXTERN(suite_utils); SUITE_EXTERN(suite_option_parser); SUITE_EXTERN(suite_notification); @@ -14,6 +19,13 @@ SUITE_EXTERN(suite_icon); GREATEST_MAIN_DEFS(); int main(int argc, char *argv[]) { + char *prog = realpath(argv[0], NULL); + if (!prog) { + fprintf(stderr, "Cannot determine actual path of test executable: %s\n", strerror(errno)); + exit(1); + } + base = dirname(prog); + // do not print out warning messages, when executing tests dunst_log_init(true); @@ -25,5 +37,8 @@ int main(int argc, char *argv[]) { RUN_SUITE(suite_misc); RUN_SUITE(suite_icon); GREATEST_MAIN_END(); + + base = NULL; + free(prog); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */