]>
Commit | Line | Data |
---|---|---|
764df57e BS |
1 | #include "clar_libgit2.h" |
2 | ||
3 | #include "git2/clone.h" | |
4 | #include "repository.h" | |
5 | ||
d024419f | 6 | #define DO_LOCAL_TEST 0 |
15445f9e | 7 | #define DO_LIVE_NETWORK_TESTS 0 |
5bb0dc93 | 8 | #define LIVE_REPO_URL "git://github.com/nulltoken/TestGitRepository" |
822d9dd5 BS |
9 | |
10 | ||
764df57e BS |
11 | static git_repository *g_repo; |
12 | ||
13 | void test_clone_clone__initialize(void) | |
14 | { | |
1c7eb971 | 15 | g_repo = NULL; |
764df57e BS |
16 | } |
17 | ||
18 | void test_clone_clone__cleanup(void) | |
19 | { | |
1c7eb971 BS |
20 | if (g_repo) { |
21 | git_repository_free(g_repo); | |
22 | g_repo = NULL; | |
23 | } | |
764df57e BS |
24 | } |
25 | ||
26 | // TODO: This is copy/pasted from network/remotelocal.c. | |
27 | static void build_local_file_url(git_buf *out, const char *fixture) | |
28 | { | |
1c7eb971 | 29 | const char *in_buf; |
764df57e | 30 | |
1c7eb971 | 31 | git_buf path_buf = GIT_BUF_INIT; |
764df57e | 32 | |
1c7eb971 BS |
33 | cl_git_pass(git_path_prettify_dir(&path_buf, fixture, NULL)); |
34 | cl_git_pass(git_buf_puts(out, "file://")); | |
764df57e | 35 | |
bb1f6087 | 36 | #ifdef GIT_WIN32 |
1c7eb971 BS |
37 | /* |
38 | * A FILE uri matches the following format: file://[host]/path | |
39 | * where "host" can be empty and "path" is an absolute path to the resource. | |
40 | * | |
41 | * In this test, no hostname is used, but we have to ensure the leading triple slashes: | |
42 | * | |
43 | * *nix: file:///usr/home/... | |
44 | * Windows: file:///C:/Users/... | |
45 | */ | |
46 | cl_git_pass(git_buf_putc(out, '/')); | |
764df57e BS |
47 | #endif |
48 | ||
1c7eb971 | 49 | in_buf = git_buf_cstr(&path_buf); |
764df57e | 50 | |
1c7eb971 BS |
51 | /* |
52 | * A very hacky Url encoding that only takes care of escaping the spaces | |
53 | */ | |
54 | while (*in_buf) { | |
55 | if (*in_buf == ' ') | |
56 | cl_git_pass(git_buf_puts(out, "%20")); | |
57 | else | |
58 | cl_git_pass(git_buf_putc(out, *in_buf)); | |
764df57e | 59 | |
1c7eb971 BS |
60 | in_buf++; |
61 | } | |
764df57e | 62 | |
1c7eb971 | 63 | git_buf_free(&path_buf); |
764df57e BS |
64 | } |
65 | ||
66 | ||
67 | void test_clone_clone__bad_url(void) | |
68 | { | |
1c7eb971 | 69 | /* Clone should clean up the mess if the URL isn't a git repository */ |
b401bace | 70 | cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", NULL, NULL, NULL)); |
1c7eb971 BS |
71 | cl_assert(!git_path_exists("./foo")); |
72 | cl_git_fail(git_clone_bare(&g_repo, "not_a_repo", "./foo.git", NULL)); | |
73 | cl_assert(!git_path_exists("./foo.git")); | |
764df57e BS |
74 | } |
75 | ||
76 | ||
77 | void test_clone_clone__local(void) | |
78 | { | |
1c7eb971 BS |
79 | git_buf src = GIT_BUF_INIT; |
80 | build_local_file_url(&src, cl_fixture("testrepo.git")); | |
764df57e | 81 | |
d024419f | 82 | #if DO_LOCAL_TEST |
b401bace | 83 | cl_git_pass(git_clone(&g_repo, git_buf_cstr(&src), "./local", NULL, NULL, NULL)); |
1c7eb971 BS |
84 | git_repository_free(g_repo); |
85 | git_futils_rmdir_r("./local", GIT_DIRREMOVAL_FILES_AND_DIRS); | |
86 | cl_git_pass(git_clone_bare(&g_repo, git_buf_cstr(&src), "./local.git", NULL)); | |
87 | git_futils_rmdir_r("./local.git", GIT_DIRREMOVAL_FILES_AND_DIRS); | |
3c4b008c | 88 | #endif |
bb1f6087 | 89 | |
1c7eb971 | 90 | git_buf_free(&src); |
764df57e BS |
91 | } |
92 | ||
93 | ||
8340dd5d | 94 | void test_clone_clone__network_full(void) |
764df57e | 95 | { |
822d9dd5 | 96 | #if DO_LIVE_NETWORK_TESTS |
1c7eb971 BS |
97 | git_remote *origin; |
98 | ||
b401bace | 99 | cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./test2", NULL, NULL, NULL)); |
1c7eb971 BS |
100 | cl_assert(!git_repository_is_bare(g_repo)); |
101 | cl_git_pass(git_remote_load(&origin, g_repo, "origin")); | |
81167385 | 102 | git_futils_rmdir_r("./test2", GIT_DIRREMOVAL_FILES_AND_DIRS); |
8340dd5d BS |
103 | #endif |
104 | } | |
105 | ||
106 | void test_clone_clone__network_bare(void) | |
107 | { | |
822d9dd5 | 108 | #if DO_LIVE_NETWORK_TESTS |
1c7eb971 BS |
109 | git_remote *origin; |
110 | ||
d024419f | 111 | cl_git_pass(git_clone_bare(&g_repo, LIVE_REPO_URL, "test", NULL)); |
1c7eb971 BS |
112 | cl_assert(git_repository_is_bare(g_repo)); |
113 | cl_git_pass(git_remote_load(&origin, g_repo, "origin")); | |
d024419f | 114 | git_futils_rmdir_r("./test", GIT_DIRREMOVAL_FILES_AND_DIRS); |
da73fb70 | 115 | #endif |
764df57e BS |
116 | } |
117 | ||
118 | ||
119 | void test_clone_clone__already_exists(void) | |
120 | { | |
822d9dd5 | 121 | #if DO_LIVE_NETWORK_TESTS |
1c7eb971 BS |
122 | /* Should pass with existing-but-empty dir */ |
123 | p_mkdir("./foo", GIT_DIR_MODE); | |
b401bace | 124 | cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL, NULL, NULL)); |
1c7eb971 BS |
125 | git_repository_free(g_repo); g_repo = NULL; |
126 | git_futils_rmdir_r("./foo", GIT_DIRREMOVAL_FILES_AND_DIRS); | |
acdd3d95 BS |
127 | #endif |
128 | ||
1c7eb971 BS |
129 | /* Should fail with a file */ |
130 | cl_git_mkfile("./foo", "Bar!"); | |
b401bace | 131 | cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL, NULL, NULL)); |
1c7eb971 BS |
132 | git_futils_rmdir_r("./foo", GIT_DIRREMOVAL_FILES_AND_DIRS); |
133 | ||
134 | /* Should fail with existing-and-nonempty dir */ | |
135 | p_mkdir("./foo", GIT_DIR_MODE); | |
136 | cl_git_mkfile("./foo/bar", "Baz!"); | |
b401bace | 137 | cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", NULL, NULL, NULL)); |
1c7eb971 | 138 | git_futils_rmdir_r("./foo", GIT_DIRREMOVAL_FILES_AND_DIRS); |
764df57e | 139 | } |