Make tests runnable from everywhere
This commit is contained in:
		
							parent
							
								
									2e9d8300de
								
							
						
					
					
						commit
						8c192f3c28
					
				
							
								
								
									
										19
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								Makefile
									
									
									
									
									
								
							| @ -71,18 +71,17 @@ dunstify: dunstify.o | |||||||
| 
 | 
 | ||||||
| .PHONY: test test-valgrind test-coverage | .PHONY: test test-valgrind test-coverage | ||||||
| test: test/test clean-coverage-run | test: test/test clean-coverage-run | ||||||
| 	cd test && ./test | 	./test/test | ||||||
| 
 | 
 | ||||||
| test-valgrind: test/test | test-valgrind: test/test | ||||||
| 	cd ./test \
 | 	valgrind \
 | ||||||
| 		&& valgrind \
 | 		--suppressions=.valgrind.suppressions \
 | ||||||
| 			--suppressions=../.valgrind.suppressions \
 | 		--leak-check=full \
 | ||||||
| 			--leak-check=full \
 | 		--show-leak-kinds=definite \
 | ||||||
| 			--show-leak-kinds=definite \
 | 		--errors-for-leak-kinds=definite \
 | ||||||
| 			--errors-for-leak-kinds=definite \
 | 		--num-callers=40 \
 | ||||||
| 			--num-callers=40 \
 | 		--error-exitcode=123 \
 | ||||||
| 			--error-exitcode=123 \
 | 		./test/test | ||||||
| 			./test |  | ||||||
| 
 | 
 | ||||||
| test-coverage: CFLAGS += -fprofile-arcs -ftest-coverage -O0 | test-coverage: CFLAGS += -fprofile-arcs -ftest-coverage -O0 | ||||||
| test-coverage: test | test-coverage: test | ||||||
|  | |||||||
							
								
								
									
										30
									
								
								test/icon.c
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								test/icon.c
									
									
									
									
									
								
							| @ -10,24 +10,23 @@ | |||||||
| #define IS_ICON_PNG(pb)  4 == gdk_pixbuf_get_width(pb) | #define IS_ICON_PNG(pb)  4 == gdk_pixbuf_get_width(pb) | ||||||
| #define IS_ICON_SVG(pb) 16 == 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) | TEST test_get_pixbuf_from_file_tilde(void) | ||||||
| { | { | ||||||
|         char *cwd  = g_get_current_dir(); |  | ||||||
|         const char *home = g_get_home_dir(); |         const char *home = g_get_home_dir(); | ||||||
|         const char *iconpath = ICONPREFIX; |         const char *iconpath = ICONPREFIX; | ||||||
| 
 | 
 | ||||||
|         if (0 != strncmp(home, cwd, strlen(home))) { |         if (0 != strncmp(home, base, strlen(home))) { | ||||||
|                 g_free(cwd); |  | ||||||
|                 SKIPm("Current directory is not a subdirectory from user's home." |                 SKIPm("Current directory is not a subdirectory from user's home." | ||||||
|                       " Cannot test iconpath tilde expansion.\n"); |                       " 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), "~"); |         path = string_replace_at(path, 0, strlen(home), "~"); | ||||||
| 
 | 
 | ||||||
|         GdkPixbuf *pixbuf = get_pixbuf_from_file(path); |         GdkPixbuf *pixbuf = get_pixbuf_from_file(path); | ||||||
|         g_clear_pointer(&path, g_free); |         g_clear_pointer(&path, g_free); | ||||||
|         g_clear_pointer(&cwd, g_free); |  | ||||||
| 
 | 
 | ||||||
|         ASSERT(pixbuf); |         ASSERT(pixbuf); | ||||||
|         ASSERTm("The wrong pixbuf is loaded in the icon file.", IS_ICON_SVG(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) | TEST test_get_pixbuf_from_file_absolute(void) | ||||||
| { | { | ||||||
|         char *cwd  = g_get_current_dir(); |  | ||||||
|         const char *iconpath = ICONPREFIX; |         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); |         GdkPixbuf *pixbuf = get_pixbuf_from_file(path); | ||||||
|         g_clear_pointer(&path, g_free); |         g_clear_pointer(&path, g_free); | ||||||
|         g_clear_pointer(&cwd, g_free); |  | ||||||
| 
 | 
 | ||||||
|         ASSERT(pixbuf); |         ASSERT(pixbuf); | ||||||
|         ASSERTm("The wrong pixbuf is loaded in the icon file.", IS_ICON_SVG(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) | 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); |         GdkPixbuf *pixbuf = get_pixbuf_from_icon(icon); | ||||||
|         ASSERT(pixbuf); |         ASSERT(pixbuf); | ||||||
|         ASSERTm("PNG pixbuf isn't loaded", IS_ICON_PNG(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) | TEST test_get_pixbuf_from_icon_fileuri(void) | ||||||
| { | { | ||||||
|         char *curdir = g_get_current_dir(); |         char *icon = g_strconcat("file://", base, "/data/icons/valid.svg", NULL); | ||||||
|         char *icon = g_strconcat("file://", curdir,"/data/icons/valid.svg", NULL); |  | ||||||
|         GdkPixbuf *pixbuf = get_pixbuf_from_icon(icon); |         GdkPixbuf *pixbuf = get_pixbuf_from_icon(icon); | ||||||
|         ASSERT(pixbuf); |         ASSERT(pixbuf); | ||||||
|         ASSERTm("SVG pixbuf isn't loaded", IS_ICON_SVG(pixbuf)); |         ASSERTm("SVG pixbuf isn't loaded", IS_ICON_SVG(pixbuf)); | ||||||
|         g_clear_pointer(&pixbuf, g_object_unref); |         g_clear_pointer(&pixbuf, g_object_unref); | ||||||
| 
 | 
 | ||||||
|         g_free(icon); |         g_free(icon); | ||||||
|         g_free(curdir); |  | ||||||
|         PASS(); |         PASS(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| SUITE(suite_icon) | SUITE(suite_icon) | ||||||
| { | { | ||||||
|         settings.icon_path = |         settings.icon_path = g_strconcat( | ||||||
|                  "." ICONPREFIX "/invalid" |                      base, ICONPREFIX "/invalid" | ||||||
|                 ":." ICONPREFIX "/valid" |                 ":", base, ICONPREFIX "/valid" | ||||||
|                 ":." ICONPREFIX "/both"; |                 ":", base, ICONPREFIX "/both", | ||||||
|  |                 NULL); | ||||||
| 
 | 
 | ||||||
|         RUN_TEST(test_get_pixbuf_from_file_tilde); |         RUN_TEST(test_get_pixbuf_from_file_tilde); | ||||||
|         RUN_TEST(test_get_pixbuf_from_file_absolute); |         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_filename); | ||||||
|         RUN_TEST(test_get_pixbuf_from_icon_fileuri); |         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: */ | /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ | ||||||
|  | |||||||
| @ -4,6 +4,8 @@ | |||||||
| #include "src/option_parser.h" | #include "src/option_parser.h" | ||||||
| #include "src/settings.h" | #include "src/settings.h" | ||||||
| 
 | 
 | ||||||
|  | extern const char *base; | ||||||
|  | 
 | ||||||
| TEST test_notification_is_duplicate_field(char **field, | TEST test_notification_is_duplicate_field(char **field, | ||||||
|                                           struct notification *a, |                                           struct notification *a, | ||||||
|                                           struct notification *b) |                                           struct notification *b) | ||||||
| @ -118,7 +120,8 @@ TEST test_notification_referencing(void) | |||||||
| SUITE(suite_notification) | SUITE(suite_notification) | ||||||
| { | { | ||||||
|         cmdline_load(0, NULL); |         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(); |         struct notification *a = notification_create(); | ||||||
|         a->appname = g_strdup("Test"); |         a->appname = g_strdup("Test"); | ||||||
| @ -145,6 +148,7 @@ SUITE(suite_notification) | |||||||
|         RUN_TEST(test_notification_referencing); |         RUN_TEST(test_notification_referencing); | ||||||
| 
 | 
 | ||||||
|         g_clear_pointer(&settings.icon_path, g_free); |         g_clear_pointer(&settings.icon_path, g_free); | ||||||
|  |         g_free(config_path); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ | /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ | ||||||
|  | |||||||
| @ -1,6 +1,8 @@ | |||||||
| #include "src/option_parser.c" | #include "src/option_parser.c" | ||||||
| #include "greatest.h" | #include "greatest.h" | ||||||
| 
 | 
 | ||||||
|  | extern const char *base; | ||||||
|  | 
 | ||||||
| TEST test_next_section(void) | TEST test_next_section(void) | ||||||
| { | { | ||||||
|         const char *section = NULL; |         const char *section = NULL; | ||||||
| @ -276,7 +278,8 @@ TEST test_option_get_bool(void) | |||||||
| 
 | 
 | ||||||
| SUITE(suite_option_parser) | 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) { |         if (!config_file) { | ||||||
|                 fputs("\nTest config file 'data/test-ini' couldn't be opened, failing.\n", stderr); |                 fputs("\nTest config file 'data/test-ini' couldn't be opened, failing.\n", stderr); | ||||||
|                 exit(1); |                 exit(1); | ||||||
| @ -310,6 +313,8 @@ SUITE(suite_option_parser) | |||||||
|         RUN_TEST(test_option_get_int); |         RUN_TEST(test_option_get_int); | ||||||
|         RUN_TEST(test_option_get_double); |         RUN_TEST(test_option_get_double); | ||||||
|         RUN_TEST(test_option_get_bool); |         RUN_TEST(test_option_get_bool); | ||||||
|  | 
 | ||||||
|  |         g_free(config_path); | ||||||
|         free_ini(); |         free_ini(); | ||||||
|         g_strfreev(argv); |         g_strfreev(argv); | ||||||
|         fclose(config_file); |         fclose(config_file); | ||||||
|  | |||||||
							
								
								
									
										15
									
								
								test/test.c
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								test/test.c
									
									
									
									
									
								
							| @ -1,9 +1,14 @@ | |||||||
| #include "greatest.h" | #include "greatest.h" | ||||||
| 
 | 
 | ||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
|  | #include <stdlib.h> | ||||||
|  | #include <libgen.h> | ||||||
|  | #include <errno.h> | ||||||
| 
 | 
 | ||||||
| #include "src/log.h" | #include "src/log.h" | ||||||
| 
 | 
 | ||||||
|  | const char *base; | ||||||
|  | 
 | ||||||
| SUITE_EXTERN(suite_utils); | SUITE_EXTERN(suite_utils); | ||||||
| SUITE_EXTERN(suite_option_parser); | SUITE_EXTERN(suite_option_parser); | ||||||
| SUITE_EXTERN(suite_notification); | SUITE_EXTERN(suite_notification); | ||||||
| @ -14,6 +19,13 @@ SUITE_EXTERN(suite_icon); | |||||||
| GREATEST_MAIN_DEFS(); | GREATEST_MAIN_DEFS(); | ||||||
| 
 | 
 | ||||||
| int main(int argc, char *argv[]) { | 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
 |         // do not print out warning messages, when executing tests
 | ||||||
|         dunst_log_init(true); |         dunst_log_init(true); | ||||||
| 
 | 
 | ||||||
| @ -25,5 +37,8 @@ int main(int argc, char *argv[]) { | |||||||
|         RUN_SUITE(suite_misc); |         RUN_SUITE(suite_misc); | ||||||
|         RUN_SUITE(suite_icon); |         RUN_SUITE(suite_icon); | ||||||
|         GREATEST_MAIN_END(); |         GREATEST_MAIN_END(); | ||||||
|  | 
 | ||||||
|  |         base = NULL; | ||||||
|  |         free(prog); | ||||||
| } | } | ||||||
| /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ | /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Benedikt Heine
						Benedikt Heine