From 3f31448e0a87a32a6d7b1f9d58d05f6e949285d0 Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Mon, 21 Sep 2015 12:17:50 +0200 Subject: [PATCH] tester: automatically detect res/writable directories (bis) and add custom option otherwise --- mediastreamer2 | 2 +- tester/common/bc_tester_utils.c | 76 ++++++++++++++++++++++++++++----- tester/common/bc_tester_utils.h | 2 +- tester/liblinphone_tester.c | 14 +----- 4 files changed, 69 insertions(+), 25 deletions(-) diff --git a/mediastreamer2 b/mediastreamer2 index b1639f814..9851fe87f 160000 --- a/mediastreamer2 +++ b/mediastreamer2 @@ -1 +1 @@ -Subproject commit b1639f8140700ff7919f9bf62e5d0dac9fa271c5 +Subproject commit 9851fe87fea2d3afe713a33762516c1cb853922a diff --git a/tester/common/bc_tester_utils.c b/tester/common/bc_tester_utils.c index 4f9fe9b2d..2e00f3030 100644 --- a/tester/common/bc_tester_utils.c +++ b/tester/common/bc_tester_utils.c @@ -307,38 +307,86 @@ int bc_tester_run_tests(const char *suite_name, const char *test_name) { void bc_tester_helper(const char *name, const char* additionnal_helper) { bc_tester_printf(bc_printf_verbosity_info, "%s --help\n" +#ifdef HAVE_CU_CURSES + "\t\t\t--curses\n" +#endif "\t\t\t--list-suites\n" "\t\t\t--list-tests \n" "\t\t\t--suite \n" "\t\t\t--test \n" -#ifdef HAVE_CU_CURSES - "\t\t\t--curses\n" -#endif + "\t\t\t--resource-dir (directory where tester resource are located)\n" + "\t\t\t--writable-dir (directory where temporary files should be created)\n" "\t\t\t--xml\n" "\t\t\t--xml-file \n" "\t\t\t--max-alloc (maximum ammount of memory obtained via malloc allocator)\n" "And additionally:\n" "%s", - name, additionnal_helper); + name, + additionnal_helper); } -void bc_tester_init(void (*ftester_printf)(int level, const char *fmt, va_list args), int iverbosity_info, int iverbosity_error) { +static int file_exists(const char* root_path) { + FILE* file; + char * sounds_path = malloc(sizeof(char)*strlen(root_path)+strlen("sounds")); + sprintf(sounds_path, "%ssounds", root_path); + file = fopen(sounds_path, "r"); + int found = (file != NULL); + if (file) fclose(file); + return found; +} + +static void detect_res_prefix(const char* prog) { + char* progpath = NULL; + char* prefix = NULL; + #if defined(BC_TESTER_WINDOWS_PHONE) || defined(BC_TESTER_WINDOWS_UNIVERSAL) bc_tester_set_resource_dir_prefix("Assets"); #elif defined(__QNX__) bc_tester_set_resource_dir_prefix("./app/native/assets/"); -#else - bc_tester_set_resource_dir_prefix("."); #endif #ifdef ANDROID bc_tester_set_writable_dir_prefix("/data/data/org.linphone.tester/cache"); #elif defined(__QNX__) bc_tester_set_writable_dir_prefix("./tmp"); -#else - bc_tester_set_writable_dir_prefix("."); #endif + if (strchr(prog, '/') != NULL) { + progpath = strdup(prog); + progpath[strrchr(prog, '/') - prog + 1] = '\0'; + } else if (strchr(prog, '\\') != NULL) { + progpath = strdup(prog); + progpath[strrchr(prog, '\\') - prog + 1] = '\0'; + } + + if (file_exists(".")) { + prefix = strdup("."); + } else if (file_exists("..")) { + prefix = strdup(".."); + } else if (file_exists(progpath)) { + prefix = strdup(progpath); + } + + if (progpath != NULL) { + free(progpath); + } + if (prefix != NULL) { + if (bc_tester_resource_dir_prefix == NULL) { + printf("Resource directory set to %s\n", prefix); + bc_tester_set_resource_dir_prefix(prefix); + } + if (bc_tester_writable_dir_prefix == NULL) { + printf("Writable directory set to %s\n", prefix); + bc_tester_set_resource_dir_prefix(prefix); + } + free(prefix); + } else if (bc_tester_resource_dir_prefix == NULL || bc_tester_writable_dir_prefix == NULL) { + printf("Could not find resource directory in %s! Please try again using option --resource-dir and/or --writable-dir.\n", progpath); + abort(); + } +} + +void bc_tester_init(void (*ftester_printf)(int level, const char *fmt, va_list args), int iverbosity_info, int iverbosity_error) { tester_printf_va = ftester_printf; bc_printf_verbosity_error = iverbosity_error; bc_printf_verbosity_info = iverbosity_info; @@ -382,6 +430,12 @@ int bc_tester_parse_args(int argc, char **argv, int argid) } else if (strcmp(argv[i], "--max-alloc") == 0) { CHECK_ARG("--max-alloc", ++i, argc); max_vm_kb = atol(argv[i]); + } else if (strcmp(argv[i], "--resource-dir") == 0) { + CHECK_ARG("--resource-dir", ++i, argc); + bc_tester_resource_dir_prefix = strdup(argv[i]); + } else if (strcmp(argv[i], "--writable-dir") == 0) { + CHECK_ARG("--writable-dir", ++i, argc); + bc_tester_writable_dir_prefix = strdup(argv[i]); } else { bc_tester_printf(bc_printf_verbosity_error, "Unknown option \"%s\"\n", argv[i]); return -1; @@ -396,9 +450,11 @@ int bc_tester_parse_args(int argc, char **argv, int argid) return i - argid + 1; } -int bc_tester_start(void) { +int bc_tester_start(const char* prog_name) { int ret; + detect_res_prefix(prog_name); + if (max_vm_kb) bc_tester_set_max_vm(max_vm_kb); diff --git a/tester/common/bc_tester_utils.h b/tester/common/bc_tester_utils.h index ad786f32a..8eeaa1c44 100644 --- a/tester/common/bc_tester_utils.h +++ b/tester/common/bc_tester_utils.h @@ -72,7 +72,7 @@ void bc_tester_init(void (*ftester_printf)(int level, const char *fmt, va_list a , int verbosity_info, int verbosity_error); void bc_tester_helper(const char *name, const char* additionnal_helper); int bc_tester_parse_args(int argc, char** argv, int argid); -int bc_tester_start(void); +int bc_tester_start(const char* prog_name); void bc_tester_add_suite(test_suite_t *suite); void bc_tester_uninit(void); void bc_tester_printf(int level, const char *fmt, ...); diff --git a/tester/liblinphone_tester.c b/tester/liblinphone_tester.c index 728861462..3a0628711 100644 --- a/tester/liblinphone_tester.c +++ b/tester/liblinphone_tester.c @@ -196,18 +196,6 @@ int main (int argc, char *argv[]) liblinphone_tester_init(NULL); -#ifndef WIN32 /*this hack doesn't work for argv[0]="c:\blablab\"*/ - // this allows to launch tester from outside of tester directory - if (strstr(argv[0], ".libs")) { - int prefix_length = strstr(argv[0], ".libs") - argv[0] + 1; - char *prefix = ms_strdup_printf("%s%.*s", argv[0][0] == '/' ? "" : "./", prefix_length, argv[0]); - ms_warning("Resource prefix set to %s", prefix); - bc_tester_set_resource_dir_prefix(prefix); - bc_tester_set_writable_dir_prefix(prefix); - ms_free(prefix); - } -#endif - for(i = 1; i < argc; ++i) { if (strcmp(argv[i], "--verbose") == 0) { linphone_core_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL); @@ -249,7 +237,7 @@ int main (int argc, char *argv[]) } } - ret = bc_tester_start(); + ret = bc_tester_start(argv[0]); liblinphone_tester_uninit(); return ret; }