diff options
| author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-05-09 22:12:36 -0300 | 
|---|---|---|
| committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-05-09 22:12:36 -0300 | 
| commit | 0cb80d3050de4adb65b6c4acb4c9ac765b2ae448 (patch) | |
| tree | 3197ccb602b56ad9030f37bd959ee8c393921fe0 | |
| parent | 502ed6760d9d2d674d0dcc87a6cb223ff079745d (diff) | |
| download | blogc-0cb80d3050de4adb65b6c4acb4c9ac765b2ae448.tar.gz blogc-0cb80d3050de4adb65b6c4acb4c9ac765b2ae448.tar.bz2 blogc-0cb80d3050de4adb65b6c4acb4c9ac765b2ae448.zip  | |
raise warning if user provided DATE for one source file, but not for all source files
| -rw-r--r-- | Makefile.am | 1 | ||||
| -rw-r--r-- | src/loader.c | 10 | ||||
| -rw-r--r-- | tests/check_loader.c | 56 | 
3 files changed, 67 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index 0932a3b..3a29f17 100644 --- a/Makefile.am +++ b/Makefile.am @@ -123,6 +123,7 @@ tests_check_loader_CFLAGS = \  tests_check_loader_LDFLAGS = \  	-no-install \  	-Wl,--wrap=blogc_file_get_contents \ +	-Wl,--wrap=fprintf \  	$(NULL)  tests_check_loader_LDADD = \ diff --git a/src/loader.c b/src/loader.c index 786fc05..f868aeb 100644 --- a/src/loader.c +++ b/src/loader.c @@ -10,6 +10,7 @@  #include <config.h>  #endif /* HAVE_CONFIG_H */ +#include <stdio.h>  #include <string.h>  #include "utils/utils.h"  #include "file.h" @@ -99,6 +100,7 @@ blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err)      blogc_error_t *tmp_err = NULL;      b_slist_t *rv = NULL;      bool first = true; +    unsigned int with_date = 0;      for (b_slist_t *tmp = l; tmp != NULL; tmp = tmp->next) {          char *f = tmp->data; @@ -113,6 +115,8 @@ blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err)              rv = NULL;              break;          } +        if (b_trie_lookup(s, "DATE")) +            with_date++;          if (first) {              const char *val = b_trie_lookup(s, "DATE");              if (val != NULL) @@ -132,5 +136,11 @@ blogc_source_parse_from_files(b_trie_t *conf, b_slist_t *l, blogc_error_t **err)          }          rv = b_slist_append(rv, s);      } +    if (with_date > 0 && with_date < b_slist_length(l)) +        // fatal error, maybe? +        fprintf(stderr, +            "blogc: warning: 'DATE' variable provided for at least one source " +            "file, but not for all source files. This means that you may get " +            "wrong values for 'DATE_FIRST' and 'DATE_LAST' variables.\n");      return rv;  } diff --git a/tests/check_loader.c b/tests/check_loader.c index 158ce1d..d478871 100644 --- a/tests/check_loader.c +++ b/tests/check_loader.c @@ -15,6 +15,7 @@  #include <setjmp.h>  #include <cmocka.h>  #include <string.h> +#include <stdio.h>  #include "../src/template-parser.h"  #include "../src/loader.h"  #include "../src/utils/utils.h" @@ -65,6 +66,15 @@ __wrap_blogc_file_get_contents(const char *path, size_t *len, blogc_error_t **er  } +int +__wrap_fprintf(FILE *stream, const char *format, ...) +{ +    assert_true(stream == mock_type(FILE*)); +    assert_string_equal(format, mock_type(const char*)); +    return strlen(format); +} + +  static void  test_template_parse_from_file(void **state)  { @@ -167,6 +177,51 @@ test_source_parse_from_files(void **state)  static void +test_source_parse_from_files_without_all_dates(void **state) +{ +    will_return(__wrap_fprintf, stderr); +    will_return(__wrap_fprintf, +        "blogc: warning: 'DATE' variable provided for at least one source " +        "file, but not for all source files. This means that you may get wrong " +        "values for 'DATE_FIRST' and 'DATE_LAST' variables.\n"); +    will_return(__wrap_blogc_file_get_contents, "bola1.txt"); +    will_return(__wrap_blogc_file_get_contents, b_strdup( +        "ASD: 123\n" +        "--------\n" +        "bola")); +    will_return(__wrap_blogc_file_get_contents, "bola2.txt"); +    will_return(__wrap_blogc_file_get_contents, b_strdup( +        "ASD: 456\n" +        "DATE: 2002-02-03 04:05:06\n" +        "--------\n" +        "bola")); +    will_return(__wrap_blogc_file_get_contents, "bola3.txt"); +    will_return(__wrap_blogc_file_get_contents, b_strdup( +        "ASD: 789\n" +        "DATE: 2003-02-03 04:05:06\n" +        "--------\n" +        "bola")); +    blogc_error_t *err = NULL; +    b_slist_t *s = NULL; +    s = b_slist_append(s, b_strdup("bola1.txt")); +    s = b_slist_append(s, b_strdup("bola2.txt")); +    s = b_slist_append(s, b_strdup("bola3.txt")); +    b_trie_t *c = b_trie_new(free); +    b_slist_t *t = blogc_source_parse_from_files(c, s, &err); +    assert_null(err); +    assert_non_null(t); +    assert_int_equal(b_slist_length(t), 3);  // it is enough, no need to look at the items +    assert_int_equal(b_trie_size(c), 3); +    assert_string_equal(b_trie_lookup(c, "FILENAME_FIRST"), "bola1"); +    assert_string_equal(b_trie_lookup(c, "FILENAME_LAST"), "bola3"); +    assert_string_equal(b_trie_lookup(c, "DATE_LAST"), "2003-02-03 04:05:06"); +    b_trie_free(c); +    b_slist_free_full(s, free); +    b_slist_free_full(t, (b_free_func_t) b_trie_free); +} + + +static void  test_source_parse_from_files_null(void **state)  {      blogc_error_t *err = NULL; @@ -193,6 +248,7 @@ main(void)          unit_test(test_source_parse_from_file),          unit_test(test_source_parse_from_file_null),          unit_test(test_source_parse_from_files), +        unit_test(test_source_parse_from_files_without_all_dates),          unit_test(test_source_parse_from_files_null),      };      return run_tests(tests);  | 
