فهرست منبع

Add EXE unit test

bel2125 5 سال پیش
والد
کامیت
c86b71addb
4فایلهای تغییر یافته به همراه27 افزوده شده و 17 حذف شده
  1. 14 15
      src/main.c
  2. 1 1
      unittest/CMakeLists.txt
  3. 5 0
      unittest/main.c
  4. 7 1
      unittest/private_exe.c

+ 14 - 15
src/main.c

@@ -323,20 +323,19 @@ show_usage_and_exit(const char *exeName)
 }
 
 
-#if defined(_WIN32) || defined(USE_COCOA)
+#if defined(_WIN32) || defined(USE_COCOA) || defined(MAIN_C_UNIT_TEST)
 static const char *config_file_top_comment =
     "# CivetWeb web server configuration file.\n"
     "# For detailed description of every option, visit\n"
     "# https://github.com/civetweb/civetweb/blob/master/docs/UserManual.md\n"
     "# Lines starting with '#' and empty lines are ignored.\n"
-    "# To make a change, remove leading '#', modify option's value,\n"
-    "# save this file and then restart Civetweb.\n\n";
+    "# To make changes, remove leading '#', modify option values,\n"
+    "# save this file and then restart CivetWeb.\n\n";
 
 static const char *
-get_url_to_first_open_port(const struct mg_context *ctx)
+get_url_to_first_open_port(const char *open_ports)
 {
 	static char url[100];
-	const char *open_ports = mg_get_option(ctx, "listening_ports");
 	int a, b, c, d, port, n;
 
 	if (sscanf(open_ports, "%d.%d.%d.%d:%d%n", &a, &b, &c, &d, &port, &n)
@@ -364,7 +363,7 @@ get_url_to_first_open_port(const struct mg_context *ctx)
 }
 
 
-#if defined(ENABLE_CREATE_CONFIG_FILE)
+#if defined(ENABLE_CREATE_CONFIG_FILE) || defined(MAIN_C_UNIT_TEST)
 static void
 create_config_file(const struct mg_context *ctx, const char *path)
 {
@@ -2808,15 +2807,15 @@ WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 		case ID_REMOVE_SERVICE:
 			manage_service(LOWORD(wParam));
 			break;
-		case ID_CONNECT:
-			fprintf(stdout, "[%s]\n", get_url_to_first_open_port(g_ctx));
-			ShellExecute(NULL,
-			             "open",
-			             get_url_to_first_open_port(g_ctx),
-			             NULL,
-			             NULL,
-			             SW_SHOW);
-			break;
+		case ID_CONNECT: {
+			/* Get port from server configuration (listening_ports) and build
+			 * URL from port. */
+			const char *port_opts = mg_get_option(g_ctx, "listening_ports");
+			const char *url = get_url_to_first_open_port(port_opts);
+
+			/* Open URL with Windows default browser */
+			ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOW);
+		} break;
 		case ID_WEBSITE:
 			fprintf(stdout, "[%s]\n", g_website);
 			ShellExecute(NULL, "open", g_website, NULL, NULL, SW_SHOW);

+ 1 - 1
unittest/CMakeLists.txt

@@ -205,7 +205,7 @@ civetweb_add_test(Timer "Timer Periodic")
 civetweb_add_test(Timer "Timer Mixed")
 
 # Tests with main.c
-#civetweb_add_test(EXE "Helper funcs")
+civetweb_add_test(EXE "Helper funcs")
 
 
 # Add the coverage command(s)

+ 5 - 0
unittest/main.c

@@ -71,6 +71,7 @@ main(const int argc, char *argv[])
 	const char *test_log_prefix = NULL;
 	char test_log_name[FILENAME_LEN];
 	char test_xml_name[FILENAME_LEN];
+	const char *test_dir = NULL;
 
 	int i;
 
@@ -115,6 +116,10 @@ main(const int argc, char *argv[])
 	srunner_add_suite(srunner, make_private_exe_suite());
 	srunner_add_suite(srunner, make_timertest_suite());
 
+	/* Print test directory */
+	test_dir = get_test_directory();
+	printf("Test directory: %s\n", test_dir);
+
 	/* Write test logs to a file */
 	if (test_log_prefix == NULL) {
 		/* Find the next free log name */

+ 7 - 1
unittest/private_exe.c

@@ -1,4 +1,4 @@
-/* Copyright (c) 2015-2018 the Civetweb developers
+/* Copyright (c) 2015-2020 the Civetweb developers
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -41,6 +41,12 @@ extern char *realpath(const char *path, char *resolved_path);
  * so this define will rename main in main.c */
 #define main exe_main
 
+/* Set this define, so all functions are included, that might be unused
+ * and excluded with the current compile options otherwise. */
+#define MAIN_C_UNIT_TEST
+
+/* Include the C file, so all static functions in main.c are available for
+ * unit testing */
 #include "../src/main.c"
 
 #include <stdlib.h>