aboutsummaryrefslogtreecommitdiff
path: root/tests/test_recipe.c
blob: fc7cc785f1b06d2e8a9897cfffefbe1d4d1e02fe (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
#include "testing.h"
#include "relocation.h"
#include "recipe.h"

static void make_local_recipe(const char *localdir) {
    char path[PATH_MAX] = {0};
    sprintf(path, "./%s", localdir);
    mkdir(path, 0755);
    if (!pushd(path)) {
        touch("meta.yaml");
        touch("build.sh");
        system("git init .");
        system("git config --local user.name test");
        system("git config --local user.email test@test.tld");
        system("git config --local commit.gpgsign false");
        system("git add meta.yaml build.sh");
        system("git commit -m \"initial commit\"");
        popd();
    } else {
        SYSERROR("failed to enter directory: %s (%s)", localdir, strerror(errno));
    }
}

void test_recipe_clone() {
    struct testcase {
        char *recipe_dir;
        char *url;
        char *gitref;
        int expect_type;
        int expect_return;
    };
    struct testcase tc[] = {
        {.recipe_dir = "recipe_condaforge",
         .url = "https://github.com/conda-forge/fitsverify-feedstock",
         .gitref = "HEAD",
         .expect_type = RECIPE_TYPE_CONDA_FORGE,
         .expect_return = 0},
        {.recipe_dir = "recipe_astroconda",
         .url = "https://github.com/astroconda/astroconda-contrib",
         .gitref = "HEAD",
         .expect_type = RECIPE_TYPE_ASTROCONDA,
         .expect_return = 0},
        {.recipe_dir = "recipe_generic",
         .url = "local_repo",
         .gitref = "HEAD",
         .expect_type = RECIPE_TYPE_GENERIC,
         .expect_return = 0},
        {.recipe_dir = "recipe_unknown",
         .url = "https://github.com/astroconda/firewatch",
         .gitref = "HEAD",
         .expect_type = RECIPE_TYPE_UNKNOWN,
         .expect_return = 0},
        {.recipe_dir = "recipe_broken",
         .url = "123_BAD_BAD_BAD_456",
         .gitref = "HEAD",
         .expect_type = RECIPE_TYPE_UNKNOWN,
         .expect_return = 128},
    };
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        struct testcase *test = &tc[i];
        if (test->recipe_dir && strcmp(test->recipe_dir, "/") != 0 && access(test->recipe_dir, F_OK) == 0) {
            rmtree(test->recipe_dir);
        }
        if (strstr(test->recipe_dir, "_generic")) {
            rmtree("local_repo");
            make_local_recipe("local_repo");
        }

        char *result_path = NULL;
        int result = 0;

        // Clone the repository
        result = recipe_clone(test->recipe_dir, test->url, test->gitref, &result_path);
        // Ensure git didn't return an error
        STASIS_ASSERT(result == test->expect_return, "failed while cloning recipe");
        // Ensure a path to the repository was returned in the result argument
        STASIS_ASSERT(result_path != NULL, "result path should not be NULL");
        // Verify the repository was detected as the correct recipe type
        STASIS_ASSERT(recipe_get_type(result_path) == test->expect_type, "repository detected as the wrong type");

        if (test->expect_return == 0) {
            // Verify the result path exists
            STASIS_ASSERT(result_path && access(result_path, F_OK) == 0, "result path should be a valid directory");
        } else {
            // Verify the result path does not exist
            STASIS_ASSERT(result_path && access(result_path, F_OK) != 0, "result path should not exist");
        }
        guard_free(result_path);
    }

}

int main(int argc, char *argv[]) {
    STASIS_TEST_BEGIN_MAIN();
    STASIS_TEST_FUNC *tests[] = {
        test_recipe_clone,
    };
    STASIS_TEST_RUN(tests);
    popd();
    STASIS_TEST_END_MAIN();
}