Skip to content

Commit

Permalink
make: added a bunch of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelmartins committed Jan 1, 2017
1 parent 463f6bd commit b832feb
Show file tree
Hide file tree
Showing 7 changed files with 1,139 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -67,6 +67,9 @@ blogc*.html
/tests/blogc-git-receiver/check_post_receive.sh
/tests/blogc-git-receiver/check_shell_command_parser
/tests/blogc-git-receiver/check_shell.sh
/tests/blogc-make/check_atom
/tests/blogc-make/check_blogc_make.sh
/tests/blogc-make/check_exec
/tests/blogc-make/check_settings
/tests/blogc-runserver/check_httpd_utils
/tests/blogc-runserver/check_mime
Expand Down
44 changes: 44 additions & 0 deletions Makefile.am
Expand Up @@ -406,6 +406,12 @@ check_SCRIPTS += \
endif
endif

if BUILD_MAKE
check_SCRIPTS += \
tests/blogc-make/check_blogc_make.sh \
$(NULL)
endif

check_SCRIPTS += \
tests/blogc/check_blogc.sh \
$(NULL)
Expand Down Expand Up @@ -731,9 +737,47 @@ endif

if BUILD_MAKE_LIB
check_PROGRAMS += \
tests/blogc-make/check_atom \
tests/blogc-make/check_exec \
tests/blogc-make/check_settings \
$(NULL)

tests_blogc_make_check_atom_SOURCES = \
tests/blogc-make/check_atom.c \
$(NULL)

tests_blogc_make_check_atom_CFLAGS = \
$(CMOCKA_CFLAGS) \
$(NULL)

tests_blogc_make_check_atom_LDFLAGS = \
-no-install \
$(NULL)

tests_blogc_make_check_atom_LDADD = \
$(CMOCKA_LIBS) \
libblogc_make.la \
libblogc_common.la \
$(NULL)

tests_blogc_make_check_exec_SOURCES = \
tests/blogc-make/check_exec.c \
$(NULL)

tests_blogc_make_check_exec_CFLAGS = \
$(CMOCKA_CFLAGS) \
$(NULL)

tests_blogc_make_check_exec_LDFLAGS = \
-no-install \
$(NULL)

tests_blogc_make_check_exec_LDADD = \
$(CMOCKA_LIBS) \
libblogc_make.la \
libblogc_common.la \
$(NULL)

tests_blogc_make_check_settings_SOURCES = \
tests/blogc-make/check_settings.c \
$(NULL)
Expand Down
3 changes: 2 additions & 1 deletion configure.ac
Expand Up @@ -227,7 +227,8 @@ AC_CONFIG_FILES([tests/blogc-git-receiver/check_post_receive.sh],
[chmod +x tests/blogc-git-receiver/check_post_receive.sh])
AC_CONFIG_FILES([tests/blogc-git-receiver/check_shell.sh],
[chmod +x tests/blogc-git-receiver/check_shell.sh])

AC_CONFIG_FILES([tests/blogc-make/check_blogc_make.sh],
[chmod +x tests/blogc-make/check_blogc_make.sh])
AC_OUTPUT

AS_ECHO("
Expand Down
2 changes: 2 additions & 0 deletions src/blogc-make/exec.h
Expand Up @@ -9,8 +9,10 @@
#ifndef _MAKE_EXEC_H
#define _MAKE_EXEC_H

#include <stdbool.h>
#include "../common/error.h"
#include "../common/utils.h"
#include "ctx.h"
#include "settings.h"

int bm_exec_command(const char *cmd, const char *input, char **output,
Expand Down
154 changes: 154 additions & 0 deletions tests/blogc-make/check_atom.c
@@ -0,0 +1,154 @@
/*
* blogc: A blog compiler.
* Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br>
*
* This program can be distributed under the terms of the BSD License.
* See the file LICENSE.
*/

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include <stdlib.h>
#include <string.h>

#include "../../src/blogc-make/atom.h"
#include "../../src/blogc-make/settings.h"
#include "../../src/common/file.h"
#include "../../src/common/error.h"
#include "../../src/common/utils.h"


static void
test_atom_file(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("atom"));
bc_trie_insert(settings->settings, "atom_ext", bc_strdup(".xml"));
bc_trie_insert(settings->settings, "post_prefix", bc_strdup("post"));

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,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<feed xmlns=\"http://www.w3.org/2005/Atom\">\n"
" <title type=\"text\">{{ SITE_TITLE }}{% ifdef FILTER_TAG %} - "
"{{ FILTER_TAG }}{% endif %}</title>\n"
" <id>{{ BASE_URL }}/atom{% ifdef FILTER_TAG %}/{{ FILTER_TAG }}"
"{% endif %}.xml</id>\n"
" <updated>{{ DATE_FIRST_FORMATTED }}</updated>\n"
" <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/\" />\n"
" <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/atom{% ifdef FILTER_TAG %}"
"/{{ FILTER_TAG }}{% endif %}.xml\" rel=\"self\" />\n"
" <author>\n"
" <name>{{ AUTHOR_NAME }}</name>\n"
" <email>{{ AUTHOR_EMAIL }}</email>\n"
" </author>\n"
" <subtitle type=\"text\">{{ SITE_TAGLINE }}</subtitle>\n"
" {% block listing %}\n"
" <entry>\n"
" <title type=\"text\">{{ TITLE }}</title>\n"
" <id>{{ BASE_URL }}/post/{{ FILENAME }}/</id>\n"
" <updated>{{ DATE_FORMATTED }}</updated>\n"
" <published>{{ DATE_FORMATTED }}</published>\n"
" <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/post/{{ FILENAME }}/\" />\n"
" <author>\n"
" <name>{{ AUTHOR_NAME }}</name>\n"
" <email>{{ AUTHOR_EMAIL }}</email>\n"
" </author>\n"
" <content type=\"html\"><![CDATA[{{ CONTENT }}]]></content>\n"
" </entry>\n"
" {% endblock %}\n"
"</feed>\n");

free(cmp);
bm_atom_destroy(rv);
free(rv);
bc_trie_free(settings->settings);
free(settings);
}


static void
test_atom_dir(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("atom"));
bc_trie_insert(settings->settings, "atom_ext", bc_strdup("/index.xml"));
bc_trie_insert(settings->settings, "post_prefix", bc_strdup("post"));

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,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<feed xmlns=\"http://www.w3.org/2005/Atom\">\n"
" <title type=\"text\">{{ SITE_TITLE }}{% ifdef FILTER_TAG %} - "
"{{ FILTER_TAG }}{% endif %}</title>\n"
" <id>{{ BASE_URL }}/atom{% ifdef FILTER_TAG %}/{{ FILTER_TAG }}"
"{% endif %}/index.xml</id>\n"
" <updated>{{ DATE_FIRST_FORMATTED }}</updated>\n"
" <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/\" />\n"
" <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/atom{% ifdef FILTER_TAG %}"
"/{{ FILTER_TAG }}{% endif %}/index.xml\" rel=\"self\" />\n"
" <author>\n"
" <name>{{ AUTHOR_NAME }}</name>\n"
" <email>{{ AUTHOR_EMAIL }}</email>\n"
" </author>\n"
" <subtitle type=\"text\">{{ SITE_TAGLINE }}</subtitle>\n"
" {% block listing %}\n"
" <entry>\n"
" <title type=\"text\">{{ TITLE }}</title>\n"
" <id>{{ BASE_URL }}/post/{{ FILENAME }}/</id>\n"
" <updated>{{ DATE_FORMATTED }}</updated>\n"
" <published>{{ DATE_FORMATTED }}</published>\n"
" <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/post/{{ FILENAME }}/\" />\n"
" <author>\n"
" <name>{{ AUTHOR_NAME }}</name>\n"
" <email>{{ AUTHOR_EMAIL }}</email>\n"
" </author>\n"
" <content type=\"html\"><![CDATA[{{ CONTENT }}]]></content>\n"
" </entry>\n"
" {% endblock %}\n"
"</feed>\n");

free(cmp);
bm_atom_destroy(rv);
free(rv);
bc_trie_free(settings->settings);
free(settings);
}


int
main(void)
{
const UnitTest tests[] = {
unit_test(test_atom_file),
unit_test(test_atom_dir),
};
return run_tests(tests);
}

0 comments on commit b832feb

Please sign in to comment.