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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
type content =
| File of string (* file *)
| Dir of (string * string) list (* directory with list filename * content *)
| NamedDir of string * (string * string) list (* directory with list filename * content *)
| Symlink (* Soft Link *)
| Hardlink (* Hard link *)
| V (* void *)
type arborescence = {
name: string;
first : content;
second : content;
}
(** Contents *)
let foo = "foo\n"
let bar = "bar\n"
let foobar = "foo\nbar\n"
let same_file = {
name = "same-file";
first = File foo;
second = File foo;
}
let diff_file = {
name = "diff-file";
first = File foo;
second = File bar;
}
let diff_file_plus_fst = {
name = "diff-file-plus-fst";
first = File foobar;
second = File foo;
}
let diff_file_plus_snd = {
name = "diff-file-plus-snd";
first = File foo;
second = File foobar;
}
let content_working_diff = [
same_file;
diff_file;
diff_file_plus_fst;
diff_file_plus_snd;
{ name = "diff-file";
first = File foo;
second = File bar;
};
{ name = "diff-file-plus-fst";
first = File foobar;
second = File foo;
};
{ name = "diff-file-plus-snd";
first = File foo;
second = File foobar;
};
{ name = "file-only-fst";
first = File foo;
second = V;
};
{ name = "file-only-snd";
first = V;
second = File foo;
};
{ name = "same-dir";
first = Dir [];
second = Dir [];
};
{ name = "diff-dir-plus-fst";
first = Dir [ "fst", foo ];
second = Dir [ "fst", foobar ] ;
};
{ name = "diff-dir-plus-snd";
first = Dir [ "fst", foobar ];
second = Dir [ "fst", foo ];
};
{ name = "dir-only-fst";
first = Dir [ "fst", foo ];
second = V;
};
]
let content_dir_file = [
same_file;
{ name = "file-fst-dir-snd";
first = File foo;
second = Dir [ "fst", foo];
};
]
let content_file_dir = [
same_file;
{ name = "dir-fst-file-snd";
first = Dir [ "fst", foo ];
second = File foo;
};
]
let content_symlink_fst = [
same_file;
{ name = "linked-file-fst";
first = Symlink;
second = File foo;
};
]
let content_symlink_snd = [
same_file;
{ name = "linked-file-snd";
first = File foo;
second = Symlink;
};
]
let content_hardlink_fst = [
same_file;
{ name = "hardlinked-file-fst";
first = Hardlink;
second = File foo;
};
]
let content_hardlink_snd = [
same_file;
{ name = "hardlinked-file-snd";
first = File foo;
second = Hardlink;
};
]
let content_patch_failure_garbage = [
same_file;
diff_file;
]
let diff_patch_failure_garbage =
"something in\n" ^
"the file\n" ^
"that is not\n" ^
"patch format\n"
let content_patch_failure_truncated = [
same_file;
diff_file;
diff_file_plus_fst;
]
let diff_patch_failure_truncated =
"--- first/diff-file\n" ^
"+++ second/diff-file\n" ^
"@@ -1,1 +1,1 @@\n" ^
"-foo\n" ^
"+bar\n" ^
"--- first/diff-fi\n"
let _good_diff =
"\n" ^
"--- first/diff-file\n" ^
"+++ second/diff-file\n" ^
"@@ -1,1 +1,1 @@\n" ^
"-foo\n" ^
"+bar\n" ^
"--- first/diff-file-plus-fst\n" ^
"+++ second/diff-file-plus-fst\n" ^
"@@ -2,1 +2,0 @@\n" ^
"-bar\n"
let content_empty_file_snd = [
same_file;
{ name = "im-empty";
first = V;
second = File "";
};
]
let content_empty_file_fst = [
same_file;
{ name = "im-empty";
first = File "";
second = V;
};
]
let content_file_fst_to_file_in_dir_snd = [
same_file;
let name = "move-me" in
{ name;
first = File bar;
second = NamedDir ("inner", [name, bar]);
};
]
let content_single_file_in_dir_snd = [
same_file;
{ name = "im-here";
first = Dir [ "delete-me", bar];
second = Dir [];
};
{ name = "im-not-here";
first = Dir [ "delete-me", "baz"];
second = V;
};
]
(** Utils *)
let print = Printf.printf
let rm_hex =
let re =
Str.regexp {|[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]?|}
in
let by = "c0ffee" in
fun s -> Str.global_replace re by s
open OpamFilename.Op
let read_dir root names =
let lst =
List.map (fun name ->
let dir = root / name in
(name^"/", []) ::
List.map (fun f ->
OpamFilename.remove_prefix root f,
OpamStd.String.split (OpamFilename.read f) '\n')
(OpamFilename.rec_files dir)
@
List.map (fun d -> OpamFilename.remove_prefix_dir root d, [])
(OpamFilename.rec_dirs dir))
names
|> List.flatten
|> List.filter (fun (name, _) ->
not (OpamStd.String.contains ~sub:".git" name))
|> List.map (fun (file, content) ->
(OpamSystem.back_to_forward file, content))
in
let lst = List.sort (fun (f,_) (f',_) -> String.compare f f') lst in
OpamStd.Format.itemize ~bullet:"+ "
(function
| d, [] -> d
| d, c ->
Printf.sprintf "%s\n%s"
d
(OpamStd.List.concat_map ~left:"" ~right:"" "\n"
(Printf.sprintf "> %s") c))
lst
let first = "first"
let second = "second"
let write_setup ?(only_fst=false) dir content =
let first_root = dir / first in
let second_root = dir / second in
List.iter (fun d ->
OpamFilename.cleandir d;
OpamFilename.mkdir d)
(if only_fst then [first_root] else [ first_root; second_root; ]);
let link_f =
let link = lazy (
let f = dir // "linked_file" in
if not (OpamFilename.exists f) then
OpamFilename.write f bar;
f
) in
fun () -> Lazy.force link
in
let create inner_dir name = function
| File content ->
OpamFilename.write (inner_dir // name) content
| (Dir _ | NamedDir _) as cdir ->
let name, lst =
match cdir with
| Dir lst -> name, lst
| NamedDir (name, lst) -> name, lst
| _ -> assert false
in
let inner_dir = inner_dir / name in
OpamFilename.mkdir inner_dir;
List.iter (fun (n,c) -> OpamFilename.write (inner_dir // n) c) lst
| Symlink ->
OpamFilename.link ~relative:false ~target:(link_f ())
~link:(inner_dir // name)
| Hardlink ->
let target = OpamFilename.to_string (link_f ()) in
let link = OpamFilename.to_string (inner_dir // name) in
Unix.link target link
| V -> ()
in
List.iter (fun {name; first; second} ->
create first_root name first;
if not only_fst then create second_root name second)
content
(* --Git-- *)
let git_cmds repo_root commands error_msg =
let commands =
List.map (fun args ->
OpamSystem.make_command "git"
("-C"::(OpamFilename.Dir.to_string repo_root)::args))
commands
in
try
List.iter (fun command ->
match OpamProcess.run command with
| {OpamProcess.r_code = 0; _ } -> ()
| _ -> failwith (OpamProcess.string_of_command command))
commands
with Failure e ->
print "ERROR:%s: %s\n" error_msg (rm_hex e)
let make_git_repo dir =
let first_root = dir / first in
let commands = [
[ "init"];
[ "add"; "--all" ];
[ "commit"; "-qm"; "first" ];
] in
git_cmds first_root commands "Git init"
let generate_git_diff dir =
let first_root = dir / first in
let name = dir // "diff-git" in
OpamFilename.remove name;
OpamFilename.touch name;
let commands = [
[ "add"; "--all" ];
[ "commit"; "-qm"; "second" ];
[ "status" ];
[ "-c"; "diff.noprefix=false"; "diff"; "--text"; "--no-ext-diff"; "-R"; "-p";
"HEAD..HEAD^"; "--output="^(OpamFilename.to_string name) ]
] in
git_cmds first_root commands "Git generate diff";
print "*** GIT DIFF ***\n";
print "%s\n" (rm_hex @@ OpamFilename.read name);
name
(* --Git-- *)
type diff_patch =
| DiffPatch
| Patch of string
type setup = {
label: string; (* setup label *)
content: arborescence list; (* the content of directory, first and second one *)
kind: diff_patch; (* what test to run *)
git: bool; (* add a test where the first directory is a git directory or not *)
}
let print_dirs dir =
print "%s\n" (read_dir dir [ first; second ])
let diff_patch dir setup =
let { content; kind; git; _ } = setup in
write_setup dir content;
print "*** SETUP ***\n";
print_dirs dir;
let diff =
match kind with
| Patch patch ->
print "*** GIVEN DIFF ***\n";
let fpatch = dir // "patch" in
OpamFilename.write fpatch patch;
Some fpatch
| DiffPatch ->
print "*** DIFF ***\n";
match
OpamRepositoryBackend.get_diff dir
(OpamFilename.Base.of_string first)
(OpamFilename.Base.of_string second)
with
| exception Failure s -> print "ERROR: %s\n" (rm_hex s); None
| exception e ->
print "ERROR: %s\n" (rm_hex @@ Printexc.to_string e);
None
| None -> print "No diff\n"; None
| Some (f,_) -> Some f
in
match diff with
| None -> ()
| Some diff ->
if git then make_git_repo dir;
print "%s\n" (OpamFilename.read diff);
let apply ~git diff =
let git = if git then "GIT " else "" in
let result =
OpamFilename.patch ~allow_unclean:false (`Patch_file diff)
(dir / first)
in
match result with
| Ok _ ->
print "*** %sPATCHED ***\n" git;
print_dirs dir;
true
| Error exn ->
print "*** %sPATCH ERROR ***\n" git;
print "ERROR: %s\n" (rm_hex @@ Printexc.to_string exn);
false
in
let patched = apply ~git:false diff in
if patched && git then
(let diff = generate_git_diff dir in
write_setup ~only_fst:true dir content;
let _ : bool = apply ~git:true diff in ())
(** The tests *)
let tests = [
{ label = "normal";
content = content_working_diff;
kind = DiffPatch;
git = true;
};
{ label = "diff file/dir error";
content = content_dir_file;
kind = DiffPatch;
git = true;
};
{ label = "diff dir/file error";
content = content_file_dir;
kind = DiffPatch;
git = true;
};
{ label = "symlink fst";
content = content_symlink_fst;
kind = DiffPatch;
git = false;
};
{ label = "symlink snd";
content = content_symlink_snd;
kind = DiffPatch;
git = false;
};
{ label = "hardlink fst";
content = content_hardlink_fst;
kind = DiffPatch;
git = false;
};
{ label = "hardlink snd";
content = content_hardlink_snd;
kind = DiffPatch;
git = false;
};
{ label = "patch error garbage";
content = content_patch_failure_garbage;
kind = Patch diff_patch_failure_garbage;
git = false;
};
{ label = "patch truncated";
content = content_patch_failure_truncated;
kind = Patch diff_patch_failure_truncated;
git = false;
};
{ label = "add empty file";
content = content_empty_file_snd;
kind = DiffPatch;
git = true;
};
{ label = "remove empty file";
content = content_empty_file_fst;
kind = DiffPatch;
git = true;
};
{ label = "move file into a new directory";
content = content_file_fst_to_file_in_dir_snd;
kind = DiffPatch;
git = true;
};
{ label = "delete file that deletes the directory";
content = content_single_file_in_dir_snd;
kind = DiffPatch;
git = true;
};
]
let () =
(* This causes Windows to use LF endings instead of CRLF, which simplifies the comparison with the reference file *)
Unix.putenv "LC_ALL" "C";
set_binary_mode_out stdout true;
Unix.dup2 Unix.stdout Unix.stderr;
OpamFilename.with_tmp_dir @@ fun dir ->
List.iteri (fun i setup ->
print "\n----------------------\n";
print " Test %d: %s\n" (i+1) setup.label;
print "----------------------\n\n";
diff_patch dir setup)
tests
|