Blob


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