diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2017-03-03 01:10:35 +0100 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2017-03-03 01:10:35 +0100 |
commit | 0c290f72a9101c196031c3bcf5ee1340b4328b80 (patch) | |
tree | 4ac5e096ffc79afbd830b6c478646f355e1156b8 | |
parent | be39aad967f2243dd512ae9ab9510179d13abf16 (diff) | |
download | blogc-0c290f72a9101c196031c3bcf5ee1340b4328b80.tar.gz blogc-0c290f72a9101c196031c3bcf5ee1340b4328b80.tar.bz2 blogc-0c290f72a9101c196031c3bcf5ee1340b4328b80.zip |
make: reworked binary lookups, added them to ctx
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | src/blogc-make/ctx.c | 11 | ||||
-rw-r--r-- | src/blogc-make/ctx.h | 5 | ||||
-rw-r--r-- | src/blogc-make/exec.c | 20 | ||||
-rw-r--r-- | src/blogc-make/exec.h | 7 | ||||
-rw-r--r-- | src/blogc-make/main.c | 12 | ||||
-rw-r--r-- | tests/blogc-make/check_atom.c | 2 | ||||
-rw-r--r-- | tests/blogc-make/check_exec.c | 110 | ||||
-rw-r--r-- | tests/blogc-make/check_rules.c | 2 | ||||
-rw-r--r-- | tests/blogc-make/check_settings.c | 2 |
10 files changed, 83 insertions, 89 deletions
diff --git a/Makefile.am b/Makefile.am index 70b7b92..8191dd9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -790,6 +790,7 @@ tests_blogc_make_check_exec_CFLAGS = \ tests_blogc_make_check_exec_LDFLAGS = \ -no-install \ + -Wl,--wrap=access \ $(NULL) tests_blogc_make_check_exec_LDADD = \ diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c index 7d3489b..62b40b8 100644 --- a/src/blogc-make/ctx.c +++ b/src/blogc-make/ctx.c @@ -16,6 +16,7 @@ #include "../common/utils.h" #include "atom.h" #include "settings.h" +#include "exec.h" #include "ctx.h" @@ -105,7 +106,8 @@ bm_filectx_free(bm_filectx_t *fctx) bm_ctx_t* -bm_ctx_new(bm_ctx_t *base, const char *settings_file, bc_error_t **err) +bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0, + bc_error_t **err) { if (settings_file == NULL || err == NULL || *err != NULL) return NULL; @@ -138,6 +140,9 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, bc_error_t **err) bm_ctx_t *rv = NULL; if (base == NULL) { rv = bc_malloc(sizeof(bm_ctx_t)); + rv->blogc = bm_exec_find_binary(argv0, "blogc", "BLOGC"); + rv->blogc_runserver = bm_exec_find_binary(argv0, "blogc-runserver", + "BLOGC_RUNSERVER"); rv->verbose = false; } else { @@ -224,7 +229,7 @@ bm_ctx_reload(bm_ctx_t *ctx) // needs to dup path, because it may be freed when reloading. char *tmp = bc_strdup(ctx->settings_fctx->path); bc_error_t *err = NULL; - ctx = bm_ctx_new(ctx, tmp, &err); + ctx = bm_ctx_new(ctx, tmp, NULL, &err); free(tmp); if (err != NULL) { // failed to reload, keep old ctx bc_error_print(err, "blogc-make"); @@ -283,5 +288,7 @@ void bm_ctx_free(bm_ctx_t *ctx) { bm_ctx_free_internal(ctx); + free(ctx->blogc); + free(ctx->blogc_runserver); free(ctx); } diff --git a/src/blogc-make/ctx.h b/src/blogc-make/ctx.h index eb25145..77e8088 100644 --- a/src/blogc-make/ctx.h +++ b/src/blogc-make/ctx.h @@ -29,6 +29,8 @@ typedef struct { } bm_filectx_t; typedef struct { + char *blogc; + char *blogc_runserver; bool verbose; bm_settings_t *settings; @@ -49,7 +51,8 @@ bm_filectx_t* bm_filectx_new(bm_ctx_t *ctx, const char *filename); bool bm_filectx_changed(bm_filectx_t *ctx, struct timespec *ts); void bm_filectx_reload(bm_filectx_t *ctx); void bm_filectx_free(bm_filectx_t *fctx); -bm_ctx_t* bm_ctx_new(bm_ctx_t *base, const char *settings_file, bc_error_t **err); +bm_ctx_t* bm_ctx_new(bm_ctx_t *base, const char *settings_file, + const char *argv0, bc_error_t **err); void bm_ctx_reload(bm_ctx_t *ctx); void bm_ctx_free_internal(bm_ctx_t *ctx); void bm_ctx_free(bm_ctx_t *ctx); diff --git a/src/blogc-make/exec.c b/src/blogc-make/exec.c index 157254b..de5dd5c 100644 --- a/src/blogc-make/exec.c +++ b/src/blogc-make/exec.c @@ -21,11 +21,9 @@ #include "exec.h" #include "settings.h" -extern const char *argv0; - char* -bm_exec_find_binary(const char *bin, const char *env) +bm_exec_find_binary(const char *argv0, const char *bin, const char *env) { // first try: env var const char *env_bin = getenv(env); @@ -205,8 +203,9 @@ list_variables(const char *key, const char *value, bc_string_t *str) char* -bm_exec_build_blogc_cmd(bm_settings_t *settings, bc_trie_t *variables, - bool listing, const char *template, const char *output, bool sources_stdin) +bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, + bc_trie_t *variables, bool listing, const char *template, + const char *output, bool sources_stdin) { bc_string_t *rv = bc_string_new(); @@ -220,9 +219,7 @@ bm_exec_build_blogc_cmd(bm_settings_t *settings, bc_trie_t *variables, free(tmp); } - char *blogc_bin = bm_exec_find_binary("blogc", "BLOGC"); bc_string_append(rv, blogc_bin); - free(blogc_bin); if (settings != NULL) { bc_trie_foreach(settings->env, @@ -270,8 +267,8 @@ bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *variables, bool listing, break; } - char *cmd = bm_exec_build_blogc_cmd(ctx->settings, variables, listing, - template->path, output->path, input->len > 0); + char *cmd = bm_exec_build_blogc_cmd(ctx->blogc, ctx->settings, variables, + listing, template->path, output->path, input->len > 0); if (ctx->verbose) printf("%s\n", cmd); @@ -348,10 +345,7 @@ bm_exec_blogc_runserver(bm_ctx_t *ctx, const char *host, const char *port, bc_string_t *cmd = bc_string_new(); - char *blogc_runserver = bm_exec_find_binary("blogc-runserver", - "BLOGC_RUNSERVER"); - bc_string_append(cmd, blogc_runserver); - free(blogc_runserver); + bc_string_append(cmd, ctx->blogc_runserver); if (host != NULL) { char *tmp = bc_shell_quote(host); diff --git a/src/blogc-make/exec.h b/src/blogc-make/exec.h index ec52eec..08cc608 100644 --- a/src/blogc-make/exec.h +++ b/src/blogc-make/exec.h @@ -15,11 +15,12 @@ #include "ctx.h" #include "settings.h" -char* bm_exec_find_binary(const char *bin, const char *env); +char* bm_exec_find_binary(const char *argv0, const char *bin, const char *env); int bm_exec_command(const char *cmd, const char *input, char **output, char **error, bc_error_t **err); -char* bm_exec_build_blogc_cmd(bm_settings_t *settings, bc_trie_t *variables, - bool listing, const char *template, const char *output, bool sources_stdin); +char* bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, + bc_trie_t *variables, bool listing, const char *template, + const char *output, bool sources_stdin); int bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *variables, bool listing, bm_filectx_t *template, bm_filectx_t *output, bc_slist_t *sources, bool only_first_source); diff --git a/src/blogc-make/main.c b/src/blogc-make/main.c index c27c302..96f55b9 100644 --- a/src/blogc-make/main.c +++ b/src/blogc-make/main.c @@ -19,10 +19,6 @@ #include "ctx.h" #include "rules.h" -// is this beautiful? no. but there's no point in passing something that is -// essentially global to every function in exec.c -const char *argv0 = NULL; - static void print_help(void) @@ -61,11 +57,6 @@ main(int argc, char **argv) { setlocale(LC_ALL, ""); - // i really hope that no operating system omits argv[0], but ... - if (argc > 0) { - argv0 = argv[0]; - } - int rv = 0; bc_error_t *err = NULL; @@ -114,7 +105,8 @@ main(int argc, char **argv) rules = bc_slist_append(rules, bc_strdup("all")); } - ctx = bm_ctx_new(NULL, blogcfile ? blogcfile : "blogcfile", &err); + ctx = bm_ctx_new(NULL, blogcfile ? blogcfile : "blogcfile", + argc > 0 ? argv[0] : NULL, &err); if (err != NULL) { bc_error_print(err, "blogc-make"); rv = 3; diff --git a/tests/blogc-make/check_atom.c b/tests/blogc-make/check_atom.c index e487d30..6faa53e 100644 --- a/tests/blogc-make/check_atom.c +++ b/tests/blogc-make/check_atom.c @@ -20,8 +20,6 @@ #include "../../src/common/error.h" #include "../../src/common/utils.h" -const char *argv0 = NULL; - static void test_atom_file(void **state) diff --git a/tests/blogc-make/check_exec.c b/tests/blogc-make/check_exec.c index 43ef161..febcfae 100644 --- a/tests/blogc-make/check_exec.c +++ b/tests/blogc-make/check_exec.c @@ -13,19 +13,60 @@ #include <stdlib.h> #include <string.h> +#include <unistd.h> #include "../../src/blogc-make/exec.h" #include "../../src/blogc-make/settings.h" #include "../../src/common/utils.h" -const char *argv0 = NULL; + +int +__wrap_access(const char *pathname, int mode) +{ + assert_int_equal(mode, X_OK); + assert_string_equal(pathname, mock_type(const char*)); + return mock_type(int); +} static void -test_build_blogc_cmd_with_settings(void **state) +test_find_binary(void **state) { unsetenv("BLOGC"); + char *bin = bm_exec_find_binary(NULL, "blogc", "BLOGC"); + assert_string_equal(bin, "blogc"); + free(bin); + + will_return(__wrap_access, "../blogc"); + will_return(__wrap_access, 0); + bin = bm_exec_find_binary("../blogc", "blogc", "BLOGC"); + assert_string_equal(bin, "'../blogc'"); + free(bin); + + will_return(__wrap_access, "/usr/bin/blogc"); + will_return(__wrap_access, 0); + bin = bm_exec_find_binary("/usr/bin/blogc", "blogc", "BLOGC"); + assert_string_equal(bin, "'/usr/bin/blogc'"); + free(bin); + + will_return(__wrap_access, "../blogc"); + will_return(__wrap_access, 1); + bin = bm_exec_find_binary("../blogc", "blogc", "BLOGC"); + assert_string_equal(bin, "blogc"); + free(bin); + + setenv("BLOGC", "/path/to/blogc", 1); + bin = bm_exec_find_binary(NULL, "blogc", "BLOGC"); + assert_string_equal(bin, "'/path/to/blogc'"); + free(bin); + unsetenv("BLOGC"); +} + + +static void +test_build_blogc_cmd_with_settings(void **state) +{ bm_settings_t *settings = bc_malloc(sizeof(bm_settings_t)); settings->settings = bc_trie_new(free); bc_trie_insert(settings->settings, "locale", bc_strdup("en_US.utf8")); @@ -35,45 +76,25 @@ test_build_blogc_cmd_with_settings(void **state) bc_trie_t *variables = bc_trie_new(free); bc_trie_insert(variables, "LOL", bc_strdup("HEHE")); - char *rv = bm_exec_build_blogc_cmd(settings, variables, true, "main.tmpl", - "foo.html", true); + char *rv = bm_exec_build_blogc_cmd("blogc", settings, variables, true, + "main.tmpl", "foo.html", true); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE' -l " "-t 'main.tmpl' -o 'foo.html' -i"); free(rv); - rv = bm_exec_build_blogc_cmd(settings, variables, false, NULL, NULL, false); + rv = bm_exec_build_blogc_cmd("blogc", settings, variables, false, NULL, NULL, + false); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ' -D LOL='HEHE'"); free(rv); - rv = bm_exec_build_blogc_cmd(settings, NULL, false, NULL, NULL, false); + rv = bm_exec_build_blogc_cmd("blogc", settings, NULL, false, NULL, NULL, + false); assert_string_equal(rv, "LC_ALL='en_US.utf8' blogc -D FOO='BAR' -D BAR='BAZ'"); free(rv); - setenv("BLOGC", "/path/to/blogc", 1); - - rv = bm_exec_build_blogc_cmd(settings, variables, true, "main.tmpl", - "foo.html", true); - assert_string_equal(rv, - "LC_ALL='en_US.utf8' '/path/to/blogc' -D FOO='BAR' -D BAR='BAZ' " - "-D LOL='HEHE' -l -t 'main.tmpl' -o 'foo.html' -i"); - free(rv); - - rv = bm_exec_build_blogc_cmd(settings, variables, false, NULL, NULL, false); - assert_string_equal(rv, - "LC_ALL='en_US.utf8' '/path/to/blogc' -D FOO='BAR' -D BAR='BAZ' " - "-D LOL='HEHE'"); - free(rv); - - rv = bm_exec_build_blogc_cmd(settings, NULL, false, NULL, NULL, false); - assert_string_equal(rv, - "LC_ALL='en_US.utf8' '/path/to/blogc' -D FOO='BAR' -D BAR='BAZ'"); - free(rv); - - unsetenv("BLOGC"); - bc_trie_free(variables); bc_trie_free(settings->settings); bc_trie_free(settings->env); @@ -81,50 +102,30 @@ test_build_blogc_cmd_with_settings(void **state) } + static void test_build_blogc_cmd_without_settings(void **state) { - unsetenv("BLOGC"); - bc_trie_t *variables = bc_trie_new(free); bc_trie_insert(variables, "LOL", bc_strdup("HEHE")); - char *rv = bm_exec_build_blogc_cmd(NULL, variables, true, "main.tmpl", - "foo.html", true); + char *rv = bm_exec_build_blogc_cmd("blogc", NULL, variables, true, + "main.tmpl", "foo.html", true); assert_string_equal(rv, "blogc -D LOL='HEHE' -l -t 'main.tmpl' -o 'foo.html' -i"); free(rv); - rv = bm_exec_build_blogc_cmd(NULL, variables, false, NULL, NULL, false); + rv = bm_exec_build_blogc_cmd("blogc", NULL, variables, false, NULL, NULL, + false); assert_string_equal(rv, "blogc -D LOL='HEHE'"); free(rv); - rv = bm_exec_build_blogc_cmd(NULL, NULL, false, NULL, NULL, false); + rv = bm_exec_build_blogc_cmd("blogc", NULL, NULL, false, NULL, NULL, false); assert_string_equal(rv, "blogc"); free(rv); - setenv("BLOGC", "/path/to/blogc", 1); - - rv = bm_exec_build_blogc_cmd(NULL, variables, true, "main.tmpl", "foo.html", - true); - assert_string_equal(rv, - "'/path/to/blogc' -D LOL='HEHE' -l -t 'main.tmpl' -o 'foo.html' -i"); - free(rv); - - rv = bm_exec_build_blogc_cmd(NULL, variables, false, NULL, NULL, false); - assert_string_equal(rv, - "'/path/to/blogc' -D LOL='HEHE'"); - free(rv); - - rv = bm_exec_build_blogc_cmd(NULL, NULL, false, NULL, NULL, false); - assert_string_equal(rv, - "'/path/to/blogc'"); - free(rv); - - unsetenv("BLOGC"); - bc_trie_free(variables); } @@ -133,6 +134,7 @@ int main(void) { const UnitTest tests[] = { + unit_test(test_find_binary), unit_test(test_build_blogc_cmd_with_settings), unit_test(test_build_blogc_cmd_without_settings), }; diff --git a/tests/blogc-make/check_rules.c b/tests/blogc-make/check_rules.c index 773ff06..c8bd9a8 100644 --- a/tests/blogc-make/check_rules.c +++ b/tests/blogc-make/check_rules.c @@ -17,8 +17,6 @@ #include "../../src/blogc-make/rules.h" #include "../../src/common/utils.h" -const char *argv0 = NULL; - static void test_rule_parse_args(void **state) diff --git a/tests/blogc-make/check_settings.c b/tests/blogc-make/check_settings.c index 3bdc9c5..93aed59 100644 --- a/tests/blogc-make/check_settings.c +++ b/tests/blogc-make/check_settings.c @@ -18,8 +18,6 @@ #include "../../src/common/error.h" #include "../../src/common/utils.h" -const char *argv0 = NULL; - static void test_settings_empty(void **state) |