aboutsummaryrefslogtreecommitdiffstats
path: root/tests/blogc-git-receiver/check_settings.c
blob: cb1203769aa5b910b7dced68c854929dbc069e30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2019 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/settings.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_settings_get_section(void **state)
{
    bc_error_t *err = NULL;

    setenv("HOME", "/home/blogc", 1);

    bc_config_t *config = bc_config_parse("", 0, NULL, &err);
    assert_null(err);
    assert_null(bgr_settings_get_section(config, "/home/blogc/repos/foo.git"));
    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, &err);
    assert_null(err);
    char *s = bgr_settings_get_section(config, "/home/blogc/repos/bar.git");
    assert_string_equal(s, "repo:bar.git");
    free(s);
    bc_config_free(config);

    setenv("BLOGC_GIT_RECEIVER_BASE_DIR", "/home/bola", 1);
    will_return(__wrap_realpath, NULL);
    will_return(__wrap_realpath, "/home/bola/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, &err);
    assert_null(err);
    s = bgr_settings_get_section(config, "/home/bola/repos/asd/bar.git");
    assert_string_equal(s, "repo:asd/bar.git");
    free(s);
    bc_config_free(config);
}


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