]> git.proxmox.com Git - libgit2.git/blob - tests/checkout/icase.c
checkout tests: cleanup realpath impl on Win32
[libgit2.git] / tests / checkout / icase.c
1 #include "clar_libgit2.h"
2
3 #include "git2/checkout.h"
4 #include "path.h"
5
6 #ifdef GIT_WIN32
7 # include <Windows.h>
8 #endif
9
10 static git_repository *repo;
11 static git_object *obj;
12 static git_checkout_options checkout_opts;
13
14 void test_checkout_icase__initialize(void)
15 {
16 git_oid id;
17
18 repo = cl_git_sandbox_init("testrepo");
19
20 cl_git_pass(git_reference_name_to_id(&id, repo, "refs/heads/dir"));
21 cl_git_pass(git_object_lookup(&obj, repo, &id, GIT_OBJ_ANY));
22
23 git_checkout_init_options(&checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION);
24 checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
25 }
26
27 void test_checkout_icase__cleanup(void)
28 {
29 git_object_free(obj);
30 cl_git_sandbox_cleanup();
31 }
32
33 static char *test_realpath(const char *in)
34 {
35 #ifdef GIT_WIN32
36 HANDLE fh;
37 HMODULE kerneldll;
38 char *filename;
39
40 typedef DWORD (__stdcall *getfinalpathname)(HANDLE, LPSTR, DWORD, DWORD);
41 getfinalpathname getfinalpathfn;
42
43 cl_assert(filename = malloc(MAX_PATH));
44 cl_assert(kerneldll = LoadLibrary("kernel32.dll"));
45 cl_assert(getfinalpathfn = (getfinalpathname)GetProcAddress(kerneldll, "GetFinalPathNameByHandleA"));
46
47 cl_assert(fh = CreateFileA(in, FILE_READ_ATTRIBUTES | STANDARD_RIGHTS_READ, FILE_SHARE_READ,
48 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL));
49
50 cl_win32_pass(getfinalpathfn(fh, filename, MAX_PATH, VOLUME_NAME_DOS));
51
52 CloseHandle(fh);
53
54 git_path_mkposix(filename);
55
56 return filename;
57 #else
58 return realpath(in, NULL);
59 #endif
60 }
61
62 static void assert_name_is(const char *expected)
63 {
64 char *actual;
65 size_t actual_len, expected_len, start;
66
67 cl_assert(actual = test_realpath(expected));
68
69 expected_len = strlen(expected);
70 actual_len = strlen(actual);
71 cl_assert(actual_len >= expected_len);
72
73 start = actual_len - expected_len;
74 cl_assert_equal_s(expected, actual + start);
75
76 if (start)
77 cl_assert_equal_strn("/", actual + (start - 1), 1);
78
79 free(actual);
80 }
81
82 void test_checkout_icase__overwrites_files_for_files(void)
83 {
84 cl_git_write2file("testrepo/NEW.txt", "neue file\n", 10, \
85 O_WRONLY | O_CREAT | O_TRUNC, 0644);
86
87 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
88 assert_name_is("testrepo/new.txt");
89 }
90
91 void test_checkout_icase__overwrites_links_for_files(void)
92 {
93 cl_must_pass(p_symlink("../tmp", "testrepo/NEW.txt"));
94
95 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
96
97 cl_assert(!git_path_exists("tmp"));
98 assert_name_is("testrepo/new.txt");
99 }
100
101 void test_checkout_icase__overwites_folders_for_files(void)
102 {
103 cl_must_pass(p_mkdir("testrepo/NEW.txt", 0777));
104
105 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
106
107 assert_name_is("testrepo/new.txt");
108 cl_assert(!git_path_isdir("testrepo/new.txt"));
109 }
110
111 void test_checkout_icase__overwrites_files_for_folders(void)
112 {
113 cl_git_write2file("testrepo/A", "neue file\n", 10, \
114 O_WRONLY | O_CREAT | O_TRUNC, 0644);
115
116 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
117 assert_name_is("testrepo/a");
118 cl_assert(git_path_isdir("testrepo/a"));
119 }
120
121 void test_checkout_icase__overwrites_links_for_folders(void)
122 {
123 cl_must_pass(p_symlink("..", "testrepo/A"));
124
125 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
126
127 cl_assert(!git_path_exists("b.txt"));
128 assert_name_is("testrepo/a");
129 }
130