diff -r df914f383f5c -r 9e2943af74fe librazor/test-lua.c --- a/librazor/test-lua.c Thu Oct 09 17:27:41 2014 +0100 +++ b/librazor/test-lua.c Mon Jul 11 16:50:21 2016 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 J. Ali Harlow + * Copyright (C) 2009, 2016 J. Ali Harlow * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,8 +22,6 @@ #include #include #include -#include -#include #include #include #include @@ -63,18 +61,19 @@ int r; void *script; size_t len; - char *s, *test_file, *srcdir; + char *s, *test_uri, *srcdir, *suffix, *cwd; FILE *fp; struct razor_error *error = NULL; + struct razor_uri base_ru, test_ru, ru; if (argc > 2) { - fprintf(stderr, "usage: %s [TESTS-FILE]\n", argv[0]); + fprintf(stderr, "usage: %s [TESTS-URI]\n", argv[0]); exit(1); } if (argc == 2) - test_file = argv[1]; + test_uri = argv[1]; else - test_file = "test.lua"; + test_uri = "test.lua"; if (!mkdtemp(root)) { perror(root); @@ -95,31 +94,68 @@ chmod(s, S_IRUSR | S_IWUSR | S_IXUSR); free(s); - script = razor_file_get_contents(test_file, &len, 0, &error); + for (len = 32;; len *= 2) { + cwd = malloc(len); + if (getcwd(cwd, len)) + break; + free(cwd); + } + cwd = realloc(cwd, strlen(cwd) + 1); + + s = razor_concat("file:", cwd, NULL); + if (razor_uri_parse(&test_ru, test_uri, &error) || + razor_uri_parse(&base_ru, s, &error)) { + fprintf(stderr, "%s\n", razor_error_get_msg(error)); + razor_error_free(error); + exit(1); + } + free(s); + + razor_uri_resolve(&ru, &base_ru, &test_ru); + razor_uri_destroy(&base_ru); + s = razor_uri_recompose(&ru); + razor_uri_destroy(&ru); + script = razor_uri_get_contents(s, &len, 0, &error); + free(s); if (!script) { srcdir = getenv("srcdir"); - if (srcdir && errno == ENOENT && *test_file != '/') { + if (srcdir && errno == ENOENT) { razor_error_free(error); - s = malloc(strlen(srcdir) + strlen(test_file) + 2); - strcpy(s, srcdir); - strcat(s, "/"); - strcat(s, test_file); - script = razor_file_get_contents(s, &len, 0, &error); - if (!script) { + error = NULL; + suffix = srcdir[strlen(srcdir) - 1] == '/' ? NULL : "/"; + if (*srcdir == '/') + s = razor_concat("file:", srcdir, suffix, NULL); + else + s = razor_concat("file:", cwd, "/", srcdir, + suffix, NULL); + razor_uri_parse(&base_ru, s, &error); + free(s); + if (!error) { + razor_uri_resolve(&ru, &base_ru, &test_ru); + razor_uri_destroy(&base_ru); + s = razor_uri_recompose(&ru); + razor_uri_destroy(&ru); + script = razor_uri_get_contents(s, &len, 0, + &error); + free(s); + } + if (error) { fprintf(stderr, "%s\n", razor_error_get_msg(error)); razor_error_free(error); exit(1); } - free(s); } else { fprintf(stderr, "%s\n", razor_error_get_msg(error)); razor_error_free(error); exit(1); } } - r = run_lua_script(root, test_file, script, len, -1); - razor_file_free_contents(script, len); + razor_uri_destroy(&test_ru); + free(cwd); + + r = run_lua_script(root, test_uri, script, len, -1); + razor_uri_free_contents(script, len); recursive_remove(root); exit(r ? 1 : 0);