Blame


1 7b19e0f1 2017-11-05 stsp /*
2 7b19e0f1 2017-11-05 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 4027f31a 2017-11-04 stsp #include <stdio.h>
18 4027f31a 2017-11-04 stsp #include <stdlib.h>
19 4027f31a 2017-11-04 stsp #include <sha1.h>
20 4027f31a 2017-11-04 stsp
21 4027f31a 2017-11-04 stsp #include "got_error.h"
22 4027f31a 2017-11-04 stsp #include "got_refs.h"
23 4027f31a 2017-11-04 stsp #include "got_repository.h"
24 4027f31a 2017-11-04 stsp
25 4027f31a 2017-11-04 stsp #define RUN_TEST(expr, name) \
26 4027f31a 2017-11-04 stsp if (!(expr)) { printf("test %s failed", (name)); failure = 1; }
27 4027f31a 2017-11-04 stsp
28 4027f31a 2017-11-04 stsp #define GOT_REPO_PATH "../../../"
29 4027f31a 2017-11-04 stsp
30 4027f31a 2017-11-04 stsp static int
31 4027f31a 2017-11-04 stsp repo_open_test(const char *repo_path)
32 4027f31a 2017-11-04 stsp {
33 4027f31a 2017-11-04 stsp const struct got_error *err;
34 4027f31a 2017-11-04 stsp struct got_repository *repo;
35 4027f31a 2017-11-04 stsp const char *abspath;
36 4027f31a 2017-11-04 stsp int ret;
37 4027f31a 2017-11-04 stsp
38 92af5469 2017-11-05 stsp err = got_repo_open(&repo, repo_path);
39 4027f31a 2017-11-04 stsp ret = (err == NULL && repo != NULL);
40 4027f31a 2017-11-04 stsp got_repo_close(repo);
41 4027f31a 2017-11-04 stsp return ret;
42 4027f31a 2017-11-04 stsp }
43 4027f31a 2017-11-04 stsp
44 4027f31a 2017-11-04 stsp static int
45 4027f31a 2017-11-04 stsp repo_get_head_ref(const char *repo_path)
46 4027f31a 2017-11-04 stsp {
47 4027f31a 2017-11-04 stsp const struct got_error *err;
48 4027f31a 2017-11-04 stsp struct got_repository *repo;
49 4027f31a 2017-11-04 stsp struct got_reference *head_ref;
50 4027f31a 2017-11-04 stsp int ret;
51 4027f31a 2017-11-04 stsp
52 92af5469 2017-11-05 stsp err = got_repo_open(&repo, repo_path);
53 4027f31a 2017-11-04 stsp if (err != NULL || repo == NULL)
54 4027f31a 2017-11-04 stsp return 0;
55 4027f31a 2017-11-04 stsp err = got_repo_get_reference(&head_ref, repo, GOT_REF_HEAD);
56 4027f31a 2017-11-04 stsp if (err != NULL || head_ref == NULL)
57 4027f31a 2017-11-04 stsp return 0;
58 4027f31a 2017-11-04 stsp got_ref_close(head_ref);
59 4027f31a 2017-11-04 stsp got_repo_close(repo);
60 4027f31a 2017-11-04 stsp return 1;
61 4027f31a 2017-11-04 stsp }
62 4027f31a 2017-11-04 stsp
63 4027f31a 2017-11-04 stsp int
64 4027f31a 2017-11-04 stsp main(int argc, const char *argv[])
65 4027f31a 2017-11-04 stsp {
66 4027f31a 2017-11-04 stsp int failure = 0;
67 4027f31a 2017-11-04 stsp const char *repo_path;
68 4027f31a 2017-11-04 stsp
69 4027f31a 2017-11-04 stsp if (argc == 1)
70 4027f31a 2017-11-04 stsp repo_path = GOT_REPO_PATH;
71 4027f31a 2017-11-04 stsp else if (argc == 2)
72 4027f31a 2017-11-04 stsp repo_path = argv[1];
73 4027f31a 2017-11-04 stsp else {
74 4027f31a 2017-11-04 stsp fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
75 4027f31a 2017-11-04 stsp return 1;
76 4027f31a 2017-11-04 stsp }
77 4027f31a 2017-11-04 stsp
78 4027f31a 2017-11-04 stsp RUN_TEST(repo_open_test(repo_path), "repo_open");
79 4027f31a 2017-11-04 stsp RUN_TEST(repo_get_head_ref(repo_path), "get_head_ref");
80 4027f31a 2017-11-04 stsp
81 4027f31a 2017-11-04 stsp return failure ? 1 : 0;
82 4027f31a 2017-11-04 stsp }