From 4970112199d967185c9e19fcd019924b72fdf845 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sat, 25 Feb 2017 23:14:59 +0100 Subject: make: renamed [environment] to [global] --- src/blogc-make/settings.c | 33 ++++++--- tests/blogc-make/check_blogc_make.sh.in | 4 +- tests/blogc-make/check_settings.c | 119 +++++++++++++++++++++++++++++++- 3 files changed, 145 insertions(+), 11 deletions(-) diff --git a/src/blogc-make/settings.c b/src/blogc-make/settings.c index 8ee4c64..0e80807 100644 --- a/src/blogc-make/settings.c +++ b/src/blogc-make/settings.c @@ -58,7 +58,7 @@ static const struct default_settings_map { }; -static const char* required_environment[] = { +static const char* required_global[] = { "AUTHOR_NAME", "AUTHOR_EMAIL", "SITE_TITLE", @@ -100,13 +100,30 @@ bm_settings_parse(const char *content, size_t content_len, bc_error_t **err) rv->copy = NULL; rv->tags = NULL; - char **env = bc_config_list_keys(config, "environment"); + // this is some code for compatibility with the [environment] section, + // even if I never released a version with it, but some people is using + // it already. + const char *section = NULL; + char **env = bc_config_list_keys(config, "global"); + if (env != NULL) { + section = "global"; + } + else { + env = bc_config_list_keys(config, "environment"); + if (env != NULL) { + section = "environment"; + } + else { + section = "global"; + } + } + if (env != NULL) { for (size_t i = 0; env[i] != NULL; i++) { for (size_t j = 0; env[i][j] != '\0'; j++) { if (!((env[i][j] >= 'A' && env[i][j] <= 'Z') || env[i][j] == '_')) { *err = bc_error_new_printf(BLOGC_MAKE_ERROR_SETTINGS, - "Invalid [environment] key: %s", env[i]); + "Invalid [%s] key: %s", section, env[i]); bc_strv_free(env); bm_settings_free(rv); rv = NULL; @@ -114,17 +131,17 @@ bm_settings_parse(const char *content, size_t content_len, bc_error_t **err) } } bc_trie_insert(rv->env, env[i], - bc_strdup(bc_config_get(config, "environment", env[i]))); + bc_strdup(bc_config_get(config, section, env[i]))); } } bc_strv_free(env); - for (size_t i = 0; required_environment[i] != NULL; i++) { - const char *value = bc_trie_lookup(rv->env, required_environment[i]); + for (size_t i = 0; required_global[i] != NULL; i++) { + const char *value = bc_trie_lookup(rv->env, required_global[i]); if (value == NULL || value[0] == '\0') { *err = bc_error_new_printf(BLOGC_MAKE_ERROR_SETTINGS, - "[environment] key required but not found or empty: %s", - required_environment[i]); + "[%s] key required but not found or empty: %s", section, + required_global[i]); bm_settings_free(rv); rv = NULL; goto cleanup; diff --git a/tests/blogc-make/check_blogc_make.sh.in b/tests/blogc-make/check_blogc_make.sh.in index 1352bbc..2dccc6c 100755 --- a/tests/blogc-make/check_blogc_make.sh.in +++ b/tests/blogc-make/check_blogc_make.sh.in @@ -22,7 +22,7 @@ mkdir -p "${TEMP}"/proj{,/templates,/content/post} ### minimal settings, will produce no file cat > "${TEMP}/proj/blogcfile" <type, BLOGC_MAKE_ERROR_SETTINGS); assert_string_equal(err->msg, - "[environment] key required but not found or empty: AUTHOR_NAME"); + "[global] key required but not found or empty: AUTHOR_NAME"); bc_error_free(err); } static void test_settings(void **state) +{ + const char *a = + "[settings]\n" + "output_dir = bola\n" + "content_dir = guda\n" + "main_template = foo.tmpl\n" + "\n" + "[global]\n" + "BOLA = asd\n" + "GUDA = qwe\n"; + bc_error_t *err = NULL; + bm_settings_t *s = bm_settings_parse(a, strlen(a), &err); + assert_non_null(err); + assert_null(s); + assert_int_equal(err->type, BLOGC_MAKE_ERROR_SETTINGS); + assert_string_equal(err->msg, + "[global] key required but not found or empty: AUTHOR_NAME"); + bc_error_free(err); +} + + +static void +test_settings_env(void **state) { const char *a = "[settings]\n" @@ -59,6 +82,98 @@ test_settings(void **state) static void test_settings2(void **state) +{ + const char *a = + "[settings]\n" + "output_dir = bola\n" + "content_dir = guda\n" + "main_template = foo.tmpl\n" + "\n" + "[global]\n" + "BOLA = asd\n" + "GUDA = qwe\n" + "AUTHOR_NAME = chunda\n" + "AUTHOR_EMAIL = chunda@example.com\n" + "SITE_TITLE = Fuuuuuuuuu\n" + "SITE_TAGLINE = My cool tagline\n" + "BASE_DOMAIN = http://example.com\n" + "\n" + "[posts]\n" + "\n" + "aaaa\n" + "bbbb\n" + "cccc\n" + "[pages]\n" + " dddd\n" + "eeee\n" + "ffff\n" + "[tags]\n" + "gggg\n" + "\n" + " hhhh\n" + "iiii\n" + "[copy]\n" + "jjjj\n" + "kkkk\n" + "llll\n"; + bc_error_t *err = NULL; + bm_settings_t *s = bm_settings_parse(a, strlen(a), &err); + assert_null(err); + assert_non_null(s); + assert_null(s->root_dir); + assert_int_equal(bc_trie_size(s->env), 7); + assert_string_equal(bc_trie_lookup(s->env, "BOLA"), "asd"); + assert_string_equal(bc_trie_lookup(s->env, "GUDA"), "qwe"); + assert_string_equal(bc_trie_lookup(s->env, "AUTHOR_NAME"), "chunda"); + assert_string_equal(bc_trie_lookup(s->env, "AUTHOR_EMAIL"), "chunda@example.com"); + assert_string_equal(bc_trie_lookup(s->env, "SITE_TITLE"), "Fuuuuuuuuu"); + assert_string_equal(bc_trie_lookup(s->env, "SITE_TAGLINE"), "My cool tagline"); + assert_string_equal(bc_trie_lookup(s->env, "BASE_DOMAIN"), "http://example.com"); + assert_int_equal(bc_trie_size(s->settings), 17); + 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, "output_dir"), "bola"); + assert_string_equal(bc_trie_lookup(s->settings, "content_dir"), "guda"); + assert_string_equal(bc_trie_lookup(s->settings, "template_dir"), "templates"); + assert_string_equal(bc_trie_lookup(s->settings, "main_template"), "foo.tmpl"); + assert_string_equal(bc_trie_lookup(s->settings, "date_format"), + "%b %d, %Y, %I:%M %p GMT"); + assert_string_equal(bc_trie_lookup(s->settings, "posts_per_page"), "10"); + assert_string_equal(bc_trie_lookup(s->settings, "atom_prefix"), "atom"); + assert_string_equal(bc_trie_lookup(s->settings, "atom_ext"), ".xml"); + assert_string_equal(bc_trie_lookup(s->settings, "atom_posts_per_page"), "10"); + assert_string_equal(bc_trie_lookup(s->settings, "pagination_prefix"), "page"); + assert_string_equal(bc_trie_lookup(s->settings, "post_prefix"), "post"); + assert_string_equal(bc_trie_lookup(s->settings, "tag_prefix"), "tag"); + assert_string_equal(bc_trie_lookup(s->settings, "runserver_host"), "127.0.0.1"); + assert_string_equal(bc_trie_lookup(s->settings, "runserver_port"), "8080"); + assert_string_equal(bc_trie_lookup(s->settings, "runserver_threads"), "20"); + assert_non_null(s->posts); + assert_string_equal(s->posts[0], "aaaa"); + assert_string_equal(s->posts[1], "bbbb"); + assert_string_equal(s->posts[2], "cccc"); + assert_null(s->posts[3]); + assert_non_null(s->pages); + assert_string_equal(s->pages[0], "dddd"); + assert_string_equal(s->pages[1], "eeee"); + assert_string_equal(s->pages[2], "ffff"); + assert_null(s->pages[3]); + assert_non_null(s->copy); + assert_string_equal(s->copy[0], "jjjj"); + assert_string_equal(s->copy[1], "kkkk"); + assert_string_equal(s->copy[2], "llll"); + assert_null(s->copy[3]); + assert_non_null(s->tags); + assert_string_equal(s->tags[0], "gggg"); + assert_string_equal(s->tags[1], "hhhh"); + assert_string_equal(s->tags[2], "iiii"); + assert_null(s->tags[3]); + bm_settings_free(s); +} + + +static void +test_settings_env2(void **state) { const char *a = "[settings]\n" @@ -155,7 +270,9 @@ main(void) const UnitTest tests[] = { unit_test(test_settings_empty), unit_test(test_settings), + unit_test(test_settings_env), unit_test(test_settings2), + unit_test(test_settings_env2), }; return run_tests(tests); } -- cgit v1.2.3-18-g5258