diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2018-08-19 14:22:45 +0200 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2019-08-09 20:14:10 +0200 |
commit | 82952bb16eeb8d27b4357fa16b76a8ee8819964b (patch) | |
tree | 4e823d9f5b22fa348216734409cf3ca4f79eff24 | |
parent | c12bdb94ecdc44f200a8030dfde4a5ec46053ea6 (diff) | |
download | blogc-feature/make-generic-rule.tar.gz blogc-feature/make-generic-rule.tar.bz2 blogc-feature/make-generic-rule.zip |
common: config-parser: mark section as list by prefixfeature/make-generic-rule
-rw-r--r-- | src/blogc-git-receiver/settings.c | 2 | ||||
-rw-r--r-- | src/blogc-make/settings.c | 2 | ||||
-rw-r--r-- | src/common/config-parser.c | 10 | ||||
-rw-r--r-- | src/common/config-parser.h | 3 | ||||
-rw-r--r-- | tests/blogc-git-receiver/check_settings.c | 6 | ||||
-rw-r--r-- | tests/common/check_config_parser.c | 213 |
6 files changed, 191 insertions, 45 deletions
diff --git a/src/blogc-git-receiver/settings.c b/src/blogc-git-receiver/settings.c index db29b18..e437a67 100644 --- a/src/blogc-git-receiver/settings.c +++ b/src/blogc-git-receiver/settings.c @@ -95,7 +95,7 @@ bgr_settings_parse(void) return NULL; } - bc_config_t *config = bc_config_parse(config_content, len, NULL, &err); + bc_config_t *config = bc_config_parse(config_content, len, NULL, NULL, &err); free(config_content); if (err != NULL) { fprintf(stderr, "warning: failed to parse configuration file (%s): %s\n", diff --git a/src/blogc-make/settings.c b/src/blogc-make/settings.c index be976e3..c8bbc63 100644 --- a/src/blogc-make/settings.c +++ b/src/blogc-make/settings.c @@ -86,7 +86,7 @@ bm_settings_parse(const char *content, size_t content_len, bc_error_t **err) return NULL; bc_config_t *config = bc_config_parse(content, content_len, list_sections, - err); + NULL, err); if (config == NULL || (err != NULL && *err != NULL)) return NULL; diff --git a/src/common/config-parser.c b/src/common/config-parser.c index bc4831b..f72b7fb 100644 --- a/src/common/config-parser.c +++ b/src/common/config-parser.c @@ -60,7 +60,7 @@ free_section(bc_configparser_section_t *section) bc_config_t* bc_config_parse(const char *src, size_t src_len, const char *list_sections[], - bc_error_t **err) + const char *list_sections_prefix[], bc_error_t **err) { if (err == NULL || *err != NULL) return NULL; @@ -150,6 +150,14 @@ bc_config_parse(const char *src, size_t src_len, const char *list_sections[], } } } + if (list_sections_prefix != NULL) { + for (size_t i = 0; list_sections_prefix[i] != NULL; i++) { + if (bc_str_starts_with(section_name, list_sections_prefix[i])) { + section->type = CONFIG_SECTION_TYPE_LIST; + break; + } + } + } switch (section->type) { case CONFIG_SECTION_TYPE_MAP: section->data = bc_trie_new(free); diff --git a/src/common/config-parser.h b/src/common/config-parser.h index 0b75ff8..fa29437 100644 --- a/src/common/config-parser.h +++ b/src/common/config-parser.h @@ -18,7 +18,8 @@ typedef struct { } bc_config_t; bc_config_t* bc_config_parse(const char *src, size_t src_len, - const char *list_sections[], bc_error_t **err); + const char *list_sections[], const char *list_sections_prefix[], + bc_error_t **err); char** bc_config_list_sections(bc_config_t *config); char** bc_config_list_keys(bc_config_t *config, const char *section); const char* bc_config_get(bc_config_t *config, const char *section, diff --git a/tests/blogc-git-receiver/check_settings.c b/tests/blogc-git-receiver/check_settings.c index cb12037..f6f6ee9 100644 --- a/tests/blogc-git-receiver/check_settings.c +++ b/tests/blogc-git-receiver/check_settings.c @@ -35,7 +35,7 @@ test_settings_get_section(void **state) setenv("HOME", "/home/blogc", 1); - bc_config_t *config = bc_config_parse("", 0, NULL, &err); + bc_config_t *config = bc_config_parse("", 0, NULL, NULL, &err); assert_null(err); assert_null(bgr_settings_get_section(config, "/home/blogc/repos/foo.git")); bc_config_free(config); @@ -52,7 +52,7 @@ test_settings_get_section(void **state) "[repo:baz.git]\n" "mirror = baz\n" "\n"; - config = bc_config_parse(conf, strlen(conf), NULL, &err); + config = bc_config_parse(conf, strlen(conf), NULL, NULL, &err); assert_null(err); char *s = bgr_settings_get_section(config, "/home/blogc/repos/bar.git"); assert_string_equal(s, "repo:bar.git"); @@ -72,7 +72,7 @@ test_settings_get_section(void **state) "[repo:asd/baz.git]\n" "mirror = baz\n" "\n"; - config = bc_config_parse(conf, strlen(conf), NULL, &err); + config = bc_config_parse(conf, strlen(conf), NULL, NULL, &err); assert_null(err); s = bgr_settings_get_section(config, "/home/bola/repos/asd/bar.git"); assert_string_equal(s, "repo:asd/bar.git"); diff --git a/tests/common/check_config_parser.c b/tests/common/check_config_parser.c index 49e2a5b..1986d5a 100644 --- a/tests/common/check_config_parser.c +++ b/tests/common/check_config_parser.c @@ -24,7 +24,7 @@ test_config_empty(void **state) { const char *a = ""; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -39,7 +39,7 @@ test_config_section_empty(void **state) { const char *a = "[foo]"; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -66,7 +66,7 @@ test_config_section(void **state) "[foo]\n" "asd = zxc"; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -90,7 +90,7 @@ test_config_section(void **state) "[foo]\n" "asd = zxc\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -114,7 +114,7 @@ test_config_section(void **state) "[foo]\r\n" "asd = zxc\r\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -145,7 +145,7 @@ test_config_section_multiple_keys(void **state) "qwe = rty \n" "zxc = vbn"; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -175,7 +175,7 @@ test_config_section_multiple_keys(void **state) "qwe = rty \n" "zxc = vbn\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -205,7 +205,7 @@ test_config_section_multiple_keys(void **state) "qwe = rty \r\n" "zxc = vbn\r\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -243,7 +243,7 @@ test_config_section_multiple_sections(void **state) "[bar]\n" "lol = hehe"; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -284,7 +284,7 @@ test_config_section_multiple_sections(void **state) "[bar]\n" "lol = hehe\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -325,7 +325,7 @@ test_config_section_multiple_sections(void **state) "[bar]\r\n" "lol = hehe\r\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -373,7 +373,7 @@ test_config_section_list(void **state) " asdasdadssad "; bc_error_t *err = NULL; const char *sections[] = {"bar", NULL}; - bc_config_t *c = bc_config_parse(a, strlen(a), sections, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), sections, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -416,7 +416,7 @@ test_config_section_list(void **state) "lol = hehe\n" "asdasdadssad\n"; err = NULL; - c = bc_config_parse(a, strlen(a), sections, &err); + c = bc_config_parse(a, strlen(a), sections, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -459,7 +459,7 @@ test_config_section_list(void **state) "lol = hehe\r\n" "asdasdadssad\r\n"; err = NULL; - c = bc_config_parse(a, strlen(a), sections, &err); + c = bc_config_parse(a, strlen(a), sections, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -495,6 +495,141 @@ test_config_section_list(void **state) static void +test_config_section_list_prefix(void **state) +{ + const char *a = + "[foo]\n" + "asd = zxc\n" + "qwe = rty\n" + "zxc = vbn\n" + "\n" + "[bara]\n" + "lol = hehe\n" + " asdasdadssad "; + bc_error_t *err = NULL; + const char *sections[] = {"bar", NULL}; + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, sections, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 2); + char **s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 2); + assert_string_equal(s[0], "foo"); + assert_string_equal(s[1], "bara"); + assert_null(s[2]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), "zxc"); + assert_string_equal(bc_config_get(c, "foo", "qwe"), "rty"); + assert_string_equal(bc_config_get(c, "foo", "zxc"), "vbn"); + char **bar = bc_config_get_list(c, "bara"); + assert_non_null(bar); + assert_string_equal(bar[0], "lol = hehe"); + assert_string_equal(bar[1], "asdasdadssad"); + assert_null(bar[2]); + bc_strv_free(bar); + char **k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 3); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_string_equal(k[2], "zxc"); + assert_null(k[3]); + bc_strv_free(k); + k = bc_config_list_keys(c, "bara"); + assert_null(k); + bc_config_free(c); + + a = + "[foo]\n" + "asd = zxc\n" + "qwe = rty\n" + "zxc = vbn\n" + "\n" + "[baras]\n" + "lol = hehe\n" + "asdasdadssad\n"; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, sections, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 2); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 2); + assert_string_equal(s[0], "foo"); + assert_string_equal(s[1], "baras"); + assert_null(s[2]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), "zxc"); + assert_string_equal(bc_config_get(c, "foo", "qwe"), "rty"); + assert_string_equal(bc_config_get(c, "foo", "zxc"), "vbn"); + bar = bc_config_get_list(c, "baras"); + assert_non_null(bar); + assert_string_equal(bar[0], "lol = hehe"); + assert_string_equal(bar[1], "asdasdadssad"); + assert_null(bar[2]); + bc_strv_free(bar); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 3); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_string_equal(k[2], "zxc"); + assert_null(k[3]); + bc_strv_free(k); + k = bc_config_list_keys(c, "baras"); + assert_null(k); + bc_config_free(c); + + a = + "[foo]\r\n" + "asd = zxc\r\n" + "qwe = rty\r\n" + "zxc = vbn\r\n" + "\r\n" + "[barasd]\r\n" + "lol = hehe\r\n" + "asdasdadssad\r\n"; + err = NULL; + c = bc_config_parse(a, strlen(a), NULL, sections, &err); + assert_null(err); + assert_non_null(c); + assert_non_null(c->root); + assert_int_equal(bc_trie_size(c->root), 2); + s = bc_config_list_sections(c); + assert_non_null(s); + assert_int_equal(bc_strv_length(s), 2); + assert_string_equal(s[0], "foo"); + assert_string_equal(s[1], "barasd"); + assert_null(s[2]); + bc_strv_free(s); + assert_string_equal(bc_config_get(c, "foo", "asd"), "zxc"); + assert_string_equal(bc_config_get(c, "foo", "qwe"), "rty"); + assert_string_equal(bc_config_get(c, "foo", "zxc"), "vbn"); + bar = bc_config_get_list(c, "barasd"); + assert_non_null(bar); + assert_string_equal(bar[0], "lol = hehe"); + assert_string_equal(bar[1], "asdasdadssad"); + assert_null(bar[2]); + bc_strv_free(bar); + k = bc_config_list_keys(c, "foo"); + assert_non_null(k); + assert_int_equal(bc_strv_length(k), 3); + assert_string_equal(k[0], "asd"); + assert_string_equal(k[1], "qwe"); + assert_string_equal(k[2], "zxc"); + assert_null(k[3]); + bc_strv_free(k); + k = bc_config_list_keys(c, "barasd"); + assert_null(k); + bc_config_free(c); +} + + +static void test_config_quoted_values(void **state) { const char *a = @@ -509,7 +644,7 @@ test_config_quoted_values(void **state) "h = \"\\\\asd\"\n" "i = '\\\\asd'\n"; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -557,12 +692,13 @@ test_config_quoted_values(void **state) "\"\\\\asd\"\n" "'\\\\asd'\n" "\n" - "[bar]\n" + "[bar:1]\n" "'lol = hehe'\n" "\" asdasdadssad \""; err = NULL; - const char *sections[] = {"foo", "bar", NULL}; - c = bc_config_parse(a, strlen(a), sections, &err); + const char *sections[] = {"foo", NULL}; + const char *sections_prefix[] = {"bar:", NULL}; + c = bc_config_parse(a, strlen(a), sections, sections_prefix, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -571,7 +707,7 @@ test_config_quoted_values(void **state) assert_non_null(s); assert_int_equal(bc_strv_length(s), 2); assert_string_equal(s[0], "foo"); - assert_string_equal(s[1], "bar"); + assert_string_equal(s[1], "bar:1"); assert_null(s[2]); bc_strv_free(s); char **bar = bc_config_get_list(c, "foo"); @@ -586,7 +722,7 @@ test_config_quoted_values(void **state) assert_string_equal(bar[8], "'\\asd'"); assert_null(bar[9]); bc_strv_free(bar); - bar = bc_config_get_list(c, "bar"); + bar = bc_config_get_list(c, "bar:1"); assert_non_null(bar); assert_string_equal(bar[0], "'lol = hehe'"); assert_string_equal(bar[1], " asdasdadssad "); @@ -594,7 +730,7 @@ test_config_quoted_values(void **state) bc_strv_free(bar); k = bc_config_list_keys(c, "foo"); assert_null(k); - k = bc_config_list_keys(c, "bar"); + k = bc_config_list_keys(c, "bar:1"); assert_null(k); bc_config_free(c); } @@ -607,7 +743,7 @@ test_config_empty_values(void **state) "[foo]\n" "asd ="; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -631,7 +767,7 @@ test_config_empty_values(void **state) "[foo]\n" "asd = \n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -656,7 +792,7 @@ test_config_empty_values(void **state) "asd = foo\n" "qwe ="; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -683,7 +819,7 @@ test_config_empty_values(void **state) "asd = foo\n" "qwe = \n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -710,7 +846,7 @@ test_config_empty_values(void **state) "asd =\n" "qwe = foo"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -737,7 +873,7 @@ test_config_empty_values(void **state) "asd = \n" "qwe = foo\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -763,7 +899,7 @@ test_config_empty_values(void **state) "[foo]\r\n" "asd ="; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -787,7 +923,7 @@ test_config_empty_values(void **state) "[foo]\r\n" "asd = \r\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -812,7 +948,7 @@ test_config_empty_values(void **state) "asd = foo\r\n" "qwe ="; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -839,7 +975,7 @@ test_config_empty_values(void **state) "asd = foo\r\n" "qwe = \r\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -866,7 +1002,7 @@ test_config_empty_values(void **state) "asd =\r\n" "qwe = foo"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -893,7 +1029,7 @@ test_config_empty_values(void **state) "asd = \r\n" "qwe = foo\r\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -925,7 +1061,7 @@ test_config_key_prefix(void **state) "LAST_FLIGHT = lol\n" "LAST_FLIGHT_SLUG = hehe\n"; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_null(err); assert_non_null(c); assert_non_null(c->root); @@ -956,7 +1092,7 @@ test_config_error_start(void **state) "asd\n" "[foo]"; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_non_null(err); assert_null(c); assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER); @@ -973,7 +1109,7 @@ test_config_error_section_with_newline(void **state) const char *a = "[foo\nbar]"; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_non_null(err); assert_null(c); assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER); @@ -992,7 +1128,7 @@ test_config_error_key_without_value(void **state) "asd = 12\n" "foo"; bc_error_t *err = NULL; - bc_config_t *c = bc_config_parse(a, strlen(a), NULL, &err); + bc_config_t *c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_non_null(err); assert_null(c); assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER); @@ -1005,7 +1141,7 @@ test_config_error_key_without_value(void **state) "asd = 12\n" "foo\n"; err = NULL; - c = bc_config_parse(a, strlen(a), NULL, &err); + c = bc_config_parse(a, strlen(a), NULL, NULL, &err); assert_non_null(err); assert_null(c); assert_int_equal(err->type, BC_ERROR_CONFIG_PARSER); @@ -1026,6 +1162,7 @@ main(void) unit_test(test_config_section_multiple_keys), unit_test(test_config_section_multiple_sections), unit_test(test_config_section_list), + unit_test(test_config_section_list_prefix), unit_test(test_config_quoted_values), unit_test(test_config_empty_values), unit_test(test_config_key_prefix), |