From c16c86bcc01dff2a984aeffd58e2402a876cc12d Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Tue, 24 Jul 2018 02:27:48 +0200 Subject: make: handle empty prefixes properly still needs to add integration tests --- src/blogc-make/atom.c | 15 ++++++---- src/blogc-make/ctx.c | 5 ++-- src/blogc-make/rules.c | 22 ++++++++------ src/blogc-make/settings.c | 2 +- tests/blogc-make/check_atom.c | 62 +++++++++++++++++++++++++++++++++++++++ tests/blogc-make/check_settings.c | 9 ++++-- 6 files changed, 94 insertions(+), 21 deletions(-) diff --git a/src/blogc-make/atom.c b/src/blogc-make/atom.c index d5f5387..0e4e631 100644 --- a/src/blogc-make/atom.c +++ b/src/blogc-make/atom.c @@ -20,11 +20,11 @@ static const char atom_template[] = "\n" " {{ SITE_TITLE }}{%% ifdef FILTER_TAG %%} - " "{{ FILTER_TAG }}{%% endif %%}\n" - " {{ BASE_URL }}/%s{%% ifdef FILTER_TAG %%}/{{ FILTER_TAG }}" + " {{ BASE_URL }}%s%s{%% ifdef FILTER_TAG %%}/{{ FILTER_TAG }}" "{%% endif %%}%s\n" " {{ DATE_FIRST_FORMATTED }}\n" " \n" - " \n" " \n" " {{ AUTHOR_NAME }}\n" @@ -34,10 +34,10 @@ static const char atom_template[] = " {%% block listing %%}\n" " \n" " {{ TITLE }}\n" - " {{ BASE_URL }}/%s/{{ FILENAME }}/\n" + " {{ BASE_URL }}%s%s/{{ FILENAME }}/\n" " {{ DATE_FORMATTED }}\n" " {{ DATE_FORMATTED }}\n" - " \n" + " \n" " \n" " {{ AUTHOR_NAME }}\n" " {{ AUTHOR_EMAIL }}\n" @@ -66,9 +66,12 @@ bm_atom_deploy(bm_settings_t *settings, bc_error_t **err) const char *atom_prefix = bc_trie_lookup(settings->settings, "atom_prefix"); const char *atom_ext = bc_trie_lookup(settings->settings, "atom_ext"); const char *post_prefix = bc_trie_lookup(settings->settings, "post_prefix"); + const char *atom_slash = atom_prefix[0] == '\0' ? "" : "/"; + const char *post_slash = post_prefix[0] == '\0' ? "" : "/"; - char *content = bc_strdup_printf(atom_template, atom_prefix, atom_ext, - atom_prefix, atom_ext, post_prefix, post_prefix); + char *content = bc_strdup_printf(atom_template, atom_slash, atom_prefix, + atom_ext, atom_slash, atom_prefix, atom_ext, post_slash, post_prefix, + post_slash, post_prefix); if (-1 == write(fd, content, strlen(content))) { *err = bc_error_new_printf(BLOGC_MAKE_ERROR_ATOM, diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c index 0a3f9a0..0c47c12 100644 --- a/src/blogc-make/ctx.c +++ b/src/blogc-make/ctx.c @@ -234,12 +234,13 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0, const char *content_dir = bc_trie_lookup(settings->settings, "content_dir"); const char *post_prefix = bc_trie_lookup(settings->settings, "post_prefix"); const char *source_ext = bc_trie_lookup(settings->settings, "source_ext"); + const char *slash = post_prefix[0] == '\0' ? "" : "/"; rv->posts_fctx = NULL; if (settings->posts != NULL) { for (size_t i = 0; settings->posts[i] != NULL; i++) { - char *f = bc_strdup_printf("%s/%s/%s%s", content_dir, post_prefix, - settings->posts[i], source_ext); + char *f = bc_strdup_printf("%s%s%s/%s%s", content_dir, slash, + post_prefix, settings->posts[i], source_ext); rv->posts_fctx = bc_slist_append(rv->posts_fctx, bm_filectx_new(rv, f, settings->posts[i], NULL)); free(f); diff --git a/src/blogc-make/rules.c b/src/blogc-make/rules.c index 8851313..2f4b119 100644 --- a/src/blogc-make/rules.c +++ b/src/blogc-make/rules.c @@ -82,10 +82,9 @@ index_outputlist(bm_ctx_t *ctx) "html_ext"); const char *index_prefix = bc_trie_lookup(ctx->settings->settings, "index_prefix"); - bool is_index = (index_prefix == NULL) && (html_ext[0] == '/'); - char *f = bc_strdup_printf("%s%s%s%s", ctx->short_output_dir, - is_index ? "" : "/", is_index ? "" : index_prefix, - html_ext); + const char *slash = index_prefix[0] == '\0' && html_ext[0] == '/' ? "" : "/"; + char *f = bc_strdup_printf("%s%s%s%s", ctx->short_output_dir, slash, + index_prefix, html_ext); rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL)); free(f); return rv; @@ -141,8 +140,9 @@ atom_outputlist(bm_ctx_t *ctx) bc_slist_t *rv = NULL; const char *atom_prefix = bc_trie_lookup(ctx->settings->settings, "atom_prefix"); + const char *slash = atom_prefix[0] == '\0' ? "" : "/"; const char *atom_ext = bc_trie_lookup(ctx->settings->settings, "atom_ext"); - char *f = bc_strdup_printf("%s/%s%s", ctx->short_output_dir, + char *f = bc_strdup_printf("%s%s%s%s", ctx->short_output_dir, slash, atom_prefix, atom_ext); rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL)); free(f); @@ -199,8 +199,9 @@ atom_tags_outputlist(bm_ctx_t *ctx) const char *atom_prefix = bc_trie_lookup(ctx->settings->settings, "atom_prefix"); const char *atom_ext = bc_trie_lookup(ctx->settings->settings, "atom_ext"); + const char *slash = atom_prefix[0] == '\0' ? "" : "/"; for (size_t i = 0; ctx->settings->tags[i] != NULL; i++) { - char *f = bc_strdup_printf("%s/%s/%s%s", ctx->short_output_dir, + char *f = bc_strdup_printf("%s%s%s/%s%s", ctx->short_output_dir, slash, atom_prefix, ctx->settings->tags[i], atom_ext); rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL)); free(f); @@ -271,10 +272,11 @@ pagination_outputlist(bm_ctx_t *ctx) "pagination_prefix"); const char *html_ext = bc_trie_lookup(ctx->settings->settings, "html_ext"); + const char *slash = pagination_prefix[0] == '\0' ? "" : "/"; bc_slist_t *rv = NULL; for (size_t i = 0; i < pages; i++) { - char *f = bc_strdup_printf("%s/%s/%d%s", ctx->short_output_dir, + char *f = bc_strdup_printf("%s%s%s/%d%s", ctx->short_output_dir, slash, pagination_prefix, i + 1, html_ext); rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL)); free(f); @@ -335,10 +337,11 @@ posts_outputlist(bm_ctx_t *ctx) "post_prefix"); const char *html_ext = bc_trie_lookup(ctx->settings->settings, "html_ext"); + const char *slash = post_prefix[0] == '\0' ? "" : "/"; bc_slist_t *rv = NULL; for (size_t i = 0; ctx->settings->posts[i] != NULL; i++) { - char *f = bc_strdup_printf("%s/%s/%s%s", ctx->short_output_dir, + char *f = bc_strdup_printf("%s%s%s/%s%s", ctx->short_output_dir, slash, post_prefix, ctx->settings->posts[i], html_ext); rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL)); free(f); @@ -405,8 +408,9 @@ tags_outputlist(bm_ctx_t *ctx) const char *tag_prefix = bc_trie_lookup(ctx->settings->settings, "tag_prefix"); const char *html_ext = bc_trie_lookup(ctx->settings->settings, "html_ext"); + const char *slash = tag_prefix[0] == '\0' ? "" : "/"; for (size_t i = 0; ctx->settings->tags[i] != NULL; i++) { - char *f = bc_strdup_printf("%s/%s/%s%s", ctx->short_output_dir, + char *f = bc_strdup_printf("%s%s%s/%s%s", ctx->short_output_dir, slash, tag_prefix, ctx->settings->tags[i], html_ext); rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL)); free(f); diff --git a/src/blogc-make/settings.c b/src/blogc-make/settings.c index 9e24d41..2029199 100644 --- a/src/blogc-make/settings.c +++ b/src/blogc-make/settings.c @@ -34,7 +34,7 @@ static const struct default_settings_map { // html {"html_ext", "/index.html"}, - {"index_prefix", NULL}, + {"index_prefix", ""}, {"post_prefix", "post"}, {"tag_prefix", "tag"}, {"html_order", "DESC"}, diff --git a/tests/blogc-make/check_atom.c b/tests/blogc-make/check_atom.c index 6faa53e..43ed93d 100644 --- a/tests/blogc-make/check_atom.c +++ b/tests/blogc-make/check_atom.c @@ -21,6 +21,67 @@ #include "../../src/common/utils.h" +static void +test_atom_empty(void **state) +{ + bm_settings_t *settings = bc_malloc(sizeof(bm_settings_t)); + settings->settings = bc_trie_new(free); + bc_trie_insert(settings->settings, "atom_prefix", bc_strdup("")); + bc_trie_insert(settings->settings, "atom_ext", bc_strdup(".xml")); + bc_trie_insert(settings->settings, "post_prefix", bc_strdup("")); + + bc_error_t *err = NULL; + char *rv = bm_atom_deploy(settings, &err); + + assert_non_null(rv); + assert_null(err); + + size_t cmp_len; + char *cmp = bc_file_get_contents(rv, true, &cmp_len, &err); + + assert_non_null(cmp); + assert_null(err); + + assert_string_equal(cmp, + "\n" + "\n" + " {{ SITE_TITLE }}{% ifdef FILTER_TAG %} - " + "{{ FILTER_TAG }}{% endif %}\n" + " {{ BASE_URL }}{% ifdef FILTER_TAG %}/{{ FILTER_TAG }}" + "{% endif %}.xml\n" + " {{ DATE_FIRST_FORMATTED }}\n" + " \n" + " \n" + " \n" + " {{ AUTHOR_NAME }}\n" + " {{ AUTHOR_EMAIL }}\n" + " \n" + " {{ SITE_TAGLINE }}\n" + " {% block listing %}\n" + " \n" + " {{ TITLE }}\n" + " {{ BASE_URL }}/{{ FILENAME }}/\n" + " {{ DATE_FORMATTED }}\n" + " {{ DATE_FORMATTED }}\n" + " \n" + " \n" + " {{ AUTHOR_NAME }}\n" + " {{ AUTHOR_EMAIL }}\n" + " \n" + " \n" + " \n" + " {% endblock %}\n" + "\n"); + + free(cmp); + bm_atom_destroy(rv); + free(rv); + bc_trie_free(settings->settings); + free(settings); +} + + static void test_atom_file(void **state) { @@ -147,6 +208,7 @@ int main(void) { const UnitTest tests[] = { + unit_test(test_atom_empty), unit_test(test_atom_file), unit_test(test_atom_dir), }; diff --git a/tests/blogc-make/check_settings.c b/tests/blogc-make/check_settings.c index 6eaaaec..8acf7e6 100644 --- a/tests/blogc-make/check_settings.c +++ b/tests/blogc-make/check_settings.c @@ -126,7 +126,7 @@ test_settings2(void **state) assert_string_equal(bc_trie_lookup(s->global, "SITE_TITLE"), "Fuuuuuuuuu"); assert_string_equal(bc_trie_lookup(s->global, "SITE_TAGLINE"), "My cool tagline"); assert_string_equal(bc_trie_lookup(s->global, "BASE_DOMAIN"), "http://example.com"); - assert_int_equal(bc_trie_size(s->settings), 15); + assert_int_equal(bc_trie_size(s->settings), 16); assert_string_equal(bc_trie_lookup(s->settings, "source_ext"), ".txt"); assert_string_equal(bc_trie_lookup(s->settings, "html_ext"), "/index.html"); assert_string_equal(bc_trie_lookup(s->settings, "content_dir"), "guda"); @@ -143,6 +143,7 @@ test_settings2(void **state) assert_string_equal(bc_trie_lookup(s->settings, "tag_prefix"), "tag"); assert_string_equal(bc_trie_lookup(s->settings, "html_order"), "DESC"); assert_string_equal(bc_trie_lookup(s->settings, "atom_order"), "DESC"); + assert_string_equal(bc_trie_lookup(s->settings, "index_prefix"), ""); assert_non_null(s->posts); assert_string_equal(s->posts[0], "aaaa"); assert_string_equal(s->posts[1], "bbbb"); @@ -215,7 +216,7 @@ test_settings_env2(void **state) assert_string_equal(bc_trie_lookup(s->global, "SITE_TITLE"), "Fuuuuuuuuu"); assert_string_equal(bc_trie_lookup(s->global, "SITE_TAGLINE"), "My cool tagline"); assert_string_equal(bc_trie_lookup(s->global, "BASE_DOMAIN"), "http://example.com"); - assert_int_equal(bc_trie_size(s->settings), 15); + assert_int_equal(bc_trie_size(s->settings), 16); assert_string_equal(bc_trie_lookup(s->settings, "source_ext"), ".txt"); assert_string_equal(bc_trie_lookup(s->settings, "html_ext"), "/index.html"); assert_string_equal(bc_trie_lookup(s->settings, "content_dir"), "guda"); @@ -232,6 +233,7 @@ test_settings_env2(void **state) assert_string_equal(bc_trie_lookup(s->settings, "tag_prefix"), "tag"); assert_string_equal(bc_trie_lookup(s->settings, "html_order"), "DESC"); assert_string_equal(bc_trie_lookup(s->settings, "atom_order"), "DESC"); + assert_string_equal(bc_trie_lookup(s->settings, "index_prefix"), ""); assert_non_null(s->posts); assert_string_equal(s->posts[0], "aaaa"); assert_string_equal(s->posts[1], "bbbb"); @@ -304,7 +306,7 @@ test_settings_copy_files(void **state) assert_string_equal(bc_trie_lookup(s->global, "SITE_TITLE"), "Fuuuuuuuuu"); assert_string_equal(bc_trie_lookup(s->global, "SITE_TAGLINE"), "My cool tagline"); assert_string_equal(bc_trie_lookup(s->global, "BASE_DOMAIN"), "http://example.com"); - assert_int_equal(bc_trie_size(s->settings), 15); + assert_int_equal(bc_trie_size(s->settings), 16); assert_string_equal(bc_trie_lookup(s->settings, "source_ext"), ".txt"); assert_string_equal(bc_trie_lookup(s->settings, "html_ext"), "/index.html"); assert_string_equal(bc_trie_lookup(s->settings, "content_dir"), "guda"); @@ -321,6 +323,7 @@ test_settings_copy_files(void **state) assert_string_equal(bc_trie_lookup(s->settings, "tag_prefix"), "tag"); assert_string_equal(bc_trie_lookup(s->settings, "html_order"), "DESC"); assert_string_equal(bc_trie_lookup(s->settings, "atom_order"), "DESC"); + assert_string_equal(bc_trie_lookup(s->settings, "index_prefix"), ""); assert_non_null(s->posts); assert_string_equal(s->posts[0], "aaaa"); assert_string_equal(s->posts[1], "bbbb"); -- cgit v1.2.3-18-g5258