aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/blogc-git-receiver/check_post_receive.c86
-rwxr-xr-xtests/blogc-git-receiver/check_post_receive.sh87
2 files changed, 173 insertions, 0 deletions
diff --git a/tests/blogc-git-receiver/check_post_receive.c b/tests/blogc-git-receiver/check_post_receive.c
new file mode 100644
index 0000000..74a9a29
--- /dev/null
+++ b/tests/blogc-git-receiver/check_post_receive.c
@@ -0,0 +1,86 @@
+/*
+ * 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 <string.h>
+#include <stdlib.h>
+#include "../../src/common/config-parser.h"
+#include "../../src/common/utils.h"
+#include "../../src/blogc-git-receiver/post-receive.h"
+
+
+char*
+__wrap_realpath(const char *path, char *resolved_path)
+{
+ const char *real_path = mock_type(const char*);
+ if (real_path == NULL)
+ return NULL;
+ assert_string_equal(path, real_path);
+ return bc_strdup(real_path);
+}
+
+
+static void
+test_post_receive_get_config_section(void **state)
+{
+ bc_config_t *config = bc_config_parse("", 0, NULL);
+ assert_null(bgr_post_receive_get_config_section(config,
+ "/home/blogc/repos/foo.git", "/home/blogc"));
+ bc_config_free(config);
+
+ will_return(__wrap_realpath, NULL);
+ will_return(__wrap_realpath, "/home/blogc/repos/bar.git");
+ const char *conf =
+ "[repo:foo.git]\n"
+ "mirror = foo\n"
+ "\n"
+ "[repo:bar.git]\n"
+ "mirror = bar\n"
+ "\n"
+ "[repo:baz.git]\n"
+ "mirror = baz\n"
+ "\n";
+ config = bc_config_parse(conf, strlen(conf), NULL);
+ char *s = bgr_post_receive_get_config_section(config,
+ "/home/blogc/repos/bar.git", "/home/blogc");
+ assert_string_equal(s, "repo:bar.git");
+ free(s);
+ bc_config_free(config);
+
+ will_return(__wrap_realpath, NULL);
+ will_return(__wrap_realpath, "/home/blogc/repos/asd/bar.git");
+ conf =
+ "[repo:asd/foo.git]\n"
+ "mirror = foo\n"
+ "\n"
+ "[repo:asd/bar.git]\n"
+ "mirror = bar\n"
+ "\n"
+ "[repo:asd/baz.git]\n"
+ "mirror = baz\n"
+ "\n";
+ config = bc_config_parse(conf, strlen(conf), NULL);
+ s = bgr_post_receive_get_config_section(config,
+ "/home/blogc/repos/asd/bar.git", "/home/blogc");
+ assert_string_equal(s, "repo:asd/bar.git");
+ free(s);
+ bc_config_free(config);
+}
+
+
+int
+main(void)
+{
+ const UnitTest tests[] = {
+ unit_test(test_post_receive_get_config_section),
+ };
+ return run_tests(tests);
+}
diff --git a/tests/blogc-git-receiver/check_post_receive.sh b/tests/blogc-git-receiver/check_post_receive.sh
new file mode 100755
index 0000000..78b566e
--- /dev/null
+++ b/tests/blogc-git-receiver/check_post_receive.sh
@@ -0,0 +1,87 @@
+#!/bin/bash
+
+set -xe -o pipefail
+
+TEMP="$(mktemp -d)"
+[[ -n "${TEMP}" ]]
+
+trap_func() {
+ [[ -e "${TEMP}/output.txt" ]] && cat "${TEMP}/output.txt"
+ rm -rf "${TEMP}"
+}
+
+trap trap_func EXIT
+
+mkdir -p "${TEMP}/repos"
+git init --bare "${TEMP}/repos/foo.git" &> /dev/null
+
+ln -s "${PWD}/blogc-git-receiver" "${TEMP}/repos/foo.git/hooks/post-receive"
+
+cat > "${TEMP}/tmp.txt" <<EOF
+blob
+mark :1
+data 4
+bar
+
+reset refs/heads/master
+commit refs/heads/master
+mark :2
+author Rafael G. Martins <rafael@rafaelmartins.eng.br> 1476033730 +0200
+committer Rafael G. Martins <rafael@rafaelmartins.eng.br> 1476033888 +0200
+data 11
+testing...
+M 100644 :1 foo
+
+EOF
+
+cd "${TEMP}/repos/foo.git"
+git fast-import < "${TEMP}/tmp.txt" &> /dev/null
+
+git init --bare "${TEMP}/repos/bar.git" &> /dev/null
+
+HOME="${TEMP}" ${TESTS_ENVIRONMENT} ./hooks/post-receive 2>&1 | tee "${TEMP}/output.txt"
+grep "warning: repository mirroring disabled" "${TEMP}/output.txt" &> /dev/null
+
+git config --local remote.mirror.pushurl "${TEMP}/repos/bar.git"
+HOME="${TEMP}" ${TESTS_ENVIRONMENT} ./hooks/post-receive 2>&1 | tee "${TEMP}/output.txt"
+grep "[new branch] *master" "${TEMP}/output.txt" &> /dev/null
+
+git config --local --unset remote.mirror.pushurl
+rm -rf "${TEMP}/repos/bar.git"
+git init --bare "${TEMP}/repos/bar.git" &> /dev/null
+git config --local remote.mirror.url "${TEMP}/repos/bar.git"
+HOME="${TEMP}" ${TESTS_ENVIRONMENT} ./hooks/post-receive 2>&1 | tee "${TEMP}/output.txt"
+grep "[new branch] *master" "${TEMP}/output.txt" &> /dev/null
+
+git config --local --unset remote.mirror.url
+rm -rf "${TEMP}/repos/bar.git"
+cat > "${TEMP}/blogc-git-receiver.ini" <<EOF
+[repo:boo.git]
+mirror = 123
+
+[repo:foo.git]
+mirror = ${TEMP}/repos/bar.git
+
+[repo:bar.git]
+mirror = lol
+EOF
+git init --bare "${TEMP}/repos/bar.git" &> /dev/null
+HOME="${TEMP}" ${TESTS_ENVIRONMENT} ./hooks/post-receive 2>&1 | tee "${TEMP}/output.txt"
+grep "[new branch] *master" "${TEMP}/output.txt" &> /dev/null
+
+rm -rf "${TEMP}/repos/bar.git"
+cat > "${TEMP}/blogc-git-receiver.ini" <<EOF
+asd[repo:boo.git]
+mirror = 123
+
+[repo:foo.git]
+mirror = ${TEMP}/repos/bar.git
+
+[repo:bar.git]
+mirror = lol
+EOF
+git init --bare "${TEMP}/repos/bar.git" &> /dev/null
+HOME="${TEMP}" ${TESTS_ENVIRONMENT} ./hooks/post-receive 2>&1 | tee "${TEMP}/output.txt"
+grep "warning: failed to parse configuration file " "${TEMP}/output.txt" &> /dev/null
+
+rm "${TEMP}/output.txt"