This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
/
uTestsFromLibGit2.pas
440 lines (363 loc) · 12.6 KB
/
uTestsFromLibGit2.pas
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
unit uTestsFromLibGit2;
interface
uses
TestFramework, Windows, SysUtils,
uGitForDelphi;
type
TTestFromLibGit2 = class (TTestCase)
strict private
function GitReturnValue(aResult: Integer): String;
public
class function NamedSuite(const aName: String): ITestSuite;
procedure must_pass(aResult: Integer);
procedure must_fail(aResult: Integer; aExpectedResult: Integer = 0);
procedure must_be_true(b: Boolean; const msg: String = '');
function remove_loose_object(const aRepository_folder: PAnsiChar; object_: Pgit_object): Integer;
function gitfo_exists(const path: String): Integer;
function gitfo_not_exists(const path: String): Integer;
function copydir_recurs(const fromDir, toDir: AnsiString): Boolean;
function rmdir_recurs(const dir: AnsiString): Boolean;
function open_temp_repo(var repo: Pgit_repository; const path: PAnsiChar): Integer;
procedure close_temp_repo(repo: Pgit_repository);
function git__suffixcmp(str, suffix: PAnsiChar): Integer;
function ensure_repository_init(
working_directory: PAnsiChar;
repository_kind: Integer;
expected_path_index,
expected_path_repository,
expected_working_directory: PAnsiChar): Integer;
function remove_placeholders(directory_path, filename: PAnsiChar): Integer;
end;
TTestSuiteForLibGit2 = class(TTestSuite, ITestSuite, ITest)
public
constructor Create(TestClass: TTestCaseClass; const aName: String);
end;
const
REPOSITORY_FOLDER_ = 'resources/testrepo.git/';
TEST_INDEX_PATH = 'resources/testrepo.git/index';
TEST_INDEX2_PATH = 'resources/gitgit.index';
TEST_INDEXBIG_PATH = 'resources/big.index';
TEST_INDEX_ENTRY_COUNT = 109;
TEST_INDEX2_ENTRY_COUNT = 1437;
TEMP_REPO_FOLDER: PAnsiChar = '/testrepo.git/';
TEMP_REPO_FOLDER_REL: PAnsiChar = './testrepo.git/';
TEMP_REPO_FOLDER_NS: PAnsiChar = '/testrepo.git';
TEMP_REPO_FOLDER_NS_REL: PAnsiChar = './testrepo.git';
tag1_id = 'b25fa35b38051e4ae45d4222e795f9df2e43f1d1';
tag2_id = '7b4384978d2493e851f9cca7858815fac9b10980';
tagged_commit = 'e90810b8df3e80c413d903f631643c716887138d';
tree_oid = '1810dff58d8a660512d4832e740f692884338ccd';
type
test_entry = record
index: Integer;
path: array [0..127] of AnsiChar;
file_size: size_t;
mtime: time_t;
end;
const
TEST_ENTRIES: array [0..4] of test_entry =
(
(index: 4; path: 'Makefile'; file_size: 5064; mtime: $4C3F7F33),
(index: 62; path: 'tests/Makefile'; file_size: 2631; mtime: $4C3F7F33),
(index: 36; path: 'src/index.c'; file_size: 10014; mtime: $4C43368D),
(index: 6; path: 'git.git-authors'; file_size: 2709; mtime: $4C3F7F33),
(index: 48; path: 'src/revobject.h'; file_size: 1448; mtime: $4C3F7FE2)
);
function REPOSITORY_FOLDER: PAnsiChar;
function OctalToInt(const Value: string): Longint;
function cmp_files(const File1, File2: TFileName): Integer;
implementation
uses
Classes, ShellAPI;
function REPOSITORY_FOLDER: PAnsiChar;
begin
Result := PAnsiChar(AnsiString(StringReplace(ExtractFilePath(ParamStr(0)), PathDelim, '/', [rfReplaceAll]) + REPOSITORY_FOLDER_));
end;
function OctalToInt(const Value: string): Longint;
var
i: Integer;
begin
Result := 0;
for i := 1 to Length(Value) do
Result := Result * 8 + StrToInt(Value[i]);
end;
{ TTestFromLibGit2 }
procedure TTestFromLibGit2.must_fail(aResult: Integer; aExpectedResult: Integer = 0);
begin
if aExpectedResult <> 0 then
begin
if aExpectedResult <> aResult then
CheckEquals(GitReturnValue(aExpectedResult), GitReturnValue(aResult));
end
else
begin
if aResult = GIT_SUCCESS then
CheckEquals('not GIT_SUCCESS', GitReturnValue(aResult));
end;
end;
procedure TTestFromLibGit2.must_pass(aResult: Integer);
begin
CheckTrue(aResult = GIT_SUCCESS, GitReturnValue(aResult));
end;
function TTestFromLibGit2.gitfo_exists(const path: String): Integer;
begin
if DirectoryExists(path) or FileExists(path) then
Result := GIT_SUCCESS
else
Result := GIT_ERROR;
end;
function TTestFromLibGit2.gitfo_not_exists(const path: String): Integer;
begin
if DirectoryExists(path) or FileExists(path) then
Result := GIT_ERROR
else
Result := GIT_SUCCESS;
end;
function TTestFromLibGit2.GitReturnValue(aResult: Integer): String;
var
errorName, errorMessage: AnsiString;
begin
errorName := git_strerror(aResult);
errorMessage := git_lasterror;
if errorMessage <> '' then
Result := Format('%d %s: %s', [aResult, errorName, errorMessage])
else
Result := Format('%d %s', [aResult, errorName]);
end;
procedure TTestFromLibGit2.must_be_true(b: Boolean; const msg: String = '');
begin
CheckTrue(b, msg);
end;
function TTestFromLibGit2.remove_loose_object(const aRepository_folder: PAnsiChar; object_: Pgit_object): Integer;
const
objects_folder = 'objects/';
var
ptr, full_path, top_folder: PAnsiChar;
path_length, objects_length: Integer;
dwAttrs: Cardinal;
begin
CheckTrue(aRepository_folder <> nil);
CheckTrue(object_ <> nil);
objects_length := strlen(objects_folder);
path_length := strlen(aRepository_folder);
GetMem(full_path, path_length + objects_length + GIT_OID_HEXSZ + 3);
ptr := full_path;
StrCopy(ptr, aRepository_folder);
StrCopy(ptr + path_length, objects_folder);
top_folder := ptr + path_length + objects_length;
ptr := top_folder;
ptr^ := '/';
Inc(ptr);
git_oid_pathfmt(ptr, git_object_id(object_));
Inc(ptr, GIT_OID_HEXSZ + 1);
ptr^ := #0;
dwAttrs := GetFileAttributesA(full_path);
if SetFileAttributesA(full_path, dwAttrs and (not FILE_ATTRIBUTE_READONLY)) and (not DeleteFileA(full_path)) then
begin
raise Exception.CreateFmt('can''t delete object file "%s"', [full_path]);
Result := -1;
Exit;
end;
top_folder^ := #0;
if (not RemoveDirectoryA(full_path)) and (GetLastError <> ERROR_DIR_NOT_EMPTY) then
begin
raise Exception.CreateFmt('can''t remove object directory "%s"', [full_path]);
Result := -1;
Exit;
end;
FreeMem(full_path, path_length + objects_length + GIT_OID_HEXSZ + 3);
Result := GIT_SUCCESS;
end;
function TTestFromLibGit2.remove_placeholders(directory_path, filename: PAnsiChar): Integer;
var
path, fname: String;
tsDirs: TStringList;
i, r: Integer;
SR: TSearchRec;
begin
/// I think this is what remove_placeholders does:
/// Ensures that `directory_path` exists,
/// then deletes `filename` if it exists in `directory_path` or any subdirectory
fname := String(AnsiString(filename));
path := StringReplace(String(AnsiString(directory_path)), '/', PathDelim, [rfReplaceAll]);
if (path <> '') and (path[1] = '\') then
Insert('.', path, 1);
Result := GIT_EINVALIDPATH;
if DirectoryExists(path) then
begin
tsDirs := TStringList.Create;
try
tsDirs.Add(path);
i := 0;
while i < tsDirs.Count do
begin
path := tsDirs[i];
r := FindFirst(path + '*.*', faDirectory, SR);
while r = 0 do
begin
if (SR.Attr and faDirectory = faDirectory) and (SR.Name <> '.') and (SR.Name <> '..') then
tsDirs.Add(path + SR.Name + PathDelim);
r := FindNext(SR);
end;
FindClose(SR);
Inc(i);
end;
for i := 0 to tsDirs.Count - 1 do
SysUtils.DeleteFile(tsDirs[i] + fname);
Result := GIT_SUCCESS;
finally
tsDirs.Free;
end;
end;
end;
function cmp_files(const File1, File2: TFileName): Integer;
var
ms1, ms2: TMemoryStream;
begin
Result := GIT_ERROR;
ms2 := nil;
ms1 := TMemoryStream.Create;
try
ms2 := TMemoryStream.Create;
ms1.LoadFromFile(File1);
ms2.LoadFromFile(File2);
if ms1.Size = ms2.Size then
begin
if CompareMem(ms1.Memory, ms2.Memory, ms1.Size) then
Result := GIT_SUCCESS;
end;
finally
ms1.Free;
ms2.Free;
end
end;
function TTestFromLibGit2.copydir_recurs(const fromDir, toDir: AnsiString): Boolean;
function _copydir_recurs(const _fromDir, _toDir: string): Boolean;
var
fos: ShellAPI.TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
fos.wFunc := FO_COPY;
fos.fFlags := FOF_FILESONLY or FOF_NOCONFIRMMKDIR;
fos.pFrom := PChar(_fromDir + #0#0);
fos.pTo := PChar(_toDir + #0#0);
Result := (0 = ShellAPI.ShFileOperation(fos));
end;
var
from_path, to_path: String;
begin
from_path := StringReplace(String(AnsiString(fromDir)), '/', PathDelim, [rfReplaceAll]);
to_path := StringReplace(String(AnsiString(ToDir)), '/', PathDelim, [rfReplaceAll]);
if (from_path <> '') and (from_path[1] = '\') then
Insert('.', from_path, 1);
if (to_path <> '') and (to_path[1] = '\') then
Insert('.', to_path, 1);
Result := _copydir_recurs(from_path, to_path);
end;
function TTestFromLibGit2.git__suffixcmp(str, suffix: PAnsiChar): Integer;
var
a, b: size_t;
begin
a := strlen(str);
b := strlen(suffix);
if (a < b) then
Result := -1
else
Result := StrComp(str + (a - b), suffix);
end;
function TTestFromLibGit2.ensure_repository_init(working_directory: PAnsiChar; repository_kind: Integer; expected_path_index, expected_path_repository,
expected_working_directory: PAnsiChar): Integer;
var
win_path: String;
path_odb: AnsiString; //[GIT_PATH_MAX];
repo: Pgit_repository;
label
cleanup;
begin
win_path := StringReplace(String(AnsiString(working_directory)), '/', PathDelim, [rfReplaceAll]);
if (win_path <> '') and (win_path[1] = '\') then
Insert('.', win_path, 1);
if DirectoryExists(win_path) then
begin
Result := GIT_ERROR;
Exit;
end;
path_odb := AnsiString(expected_path_repository) + AnsiString(GIT_OBJECTS_DIR);
if (git_repository_init(repo, working_directory, repository_kind) < GIT_SUCCESS) then
begin
Result := GIT_ERROR;
Exit;
end;
if ((git_repository_path(repo, GIT_REPO_PATH_WORKDIR) <> nil) or (expected_working_directory <> nil)) then
begin
if (git__suffixcmp(git_repository_path(repo, GIT_REPO_PATH_WORKDIR), expected_working_directory) <> 0) then
goto cleanup;
end;
if (git__suffixcmp(git_repository_path(repo, GIT_REPO_PATH_ODB), PAnsiChar(path_odb)) <> 0) then
goto cleanup;
if (git__suffixcmp(git_repository_path(repo, GIT_REPO_PATH), expected_path_repository) <> 0) then
goto cleanup;
if ((git_repository_path(repo, GIT_REPO_PATH_INDEX) <> nil) or (expected_path_index <> nil)) then
begin
if (git__suffixcmp(git_repository_path(repo, GIT_REPO_PATH_INDEX), expected_path_index) <> 0) then
goto cleanup;
if (git_repository_is_bare(repo) = 1) then
goto cleanup;
end
else if (git_repository_is_bare(repo) = 0) then
goto cleanup;
if (git_repository_is_empty(repo) = 0) then
goto cleanup;
git_repository_free(repo);
rmdir_recurs(working_directory);
Result := GIT_SUCCESS;
Exit;
cleanup:
git_repository_free(repo);
rmdir_recurs(working_directory);
Result := GIT_ERROR;
end;
function TTestFromLibGit2.rmdir_recurs(const dir: AnsiString): Boolean;
function _rmdir_recurs(const _dir: string): Boolean;
var
fos: ShellAPI.TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
fos.wFunc := FO_DELETE;
fos.fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
fos.pFrom := PChar(_dir + #0);
Result := (0 = ShellAPI.ShFileOperation(fos));
end;
var
path: String;
begin
path := StringReplace(String(AnsiString(dir)), '/', PathDelim, [rfReplaceAll]);
// if (path <> '') and (path[1] = '\') then
// Insert('.', path, 1);
Result := _rmdir_recurs(path);
end;
function TTestFromLibGit2.open_temp_repo(var repo: Pgit_repository; const path: PAnsiChar): Integer;
begin
if not copydir_recurs(path, TEMP_REPO_FOLDER) then
Result := GIT_ERROR
else
Result := git_repository_open(repo, TEMP_REPO_FOLDER_REL);
end;
procedure TTestFromLibGit2.close_temp_repo(repo: Pgit_repository);
begin
git_repository_free(repo);
rmdir_recurs(TEMP_REPO_FOLDER_REL);
end;
{ TTestSuiteForLibGit2 }
class function TTestFromLibGit2.NamedSuite(const aName: String): ITestSuite;
begin
Result := TTestSuiteForLibGit2.Create(Self, aName);
end;
constructor TTestSuiteForLibGit2.Create(TestClass: TTestCaseClass; const aName: String);
begin
inherited Create(aName);
AddTests(testClass);
end;
initialization
InitLibgit2;
end.