-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for OpamSystem.translate_patch
- Loading branch information
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
PATCH No CRLF adaptation necessary for b/always-crlf | ||
PATCH No CRLF adaptation necessary for b/always-lf | ||
PATCH No CRLF adaptation necessary for b/test1 | ||
PATCH No CRLF adaptation necessary for b/test2 | ||
PATCH No CRLF adaptation necessary for b/test3 | ||
Before patch state of c: | ||
./always-crlf: CRLF | ||
./always-lf: LF | ||
./test1: LF | ||
./test2: LF | ||
./test3: LF | ||
patching file always-crlf | ||
patching file always-lf | ||
patching file test1 | ||
patching file test2 | ||
patching file test3 | ||
After patch state of c: | ||
./always-crlf: CRLF | ||
./always-lf: LF | ||
./test1: LF | ||
./test2: LF | ||
PATCH No CRLF adaptation necessary for b/always-crlf | ||
PATCH No CRLF adaptation necessary for b/always-lf | ||
PATCH No CRLF adaptation necessary for b/test1 | ||
PATCH Adding \r to patch chunks for b/test2 | ||
PATCH No CRLF adaptation necessary for b/test3 | ||
PATCH Transform 30-34 +\r | ||
Before patch state of c: | ||
./always-crlf: CRLF | ||
./always-lf: LF | ||
./test1: LF | ||
./test2: CRLF | ||
./test3: LF | ||
patching file always-crlf | ||
patching file always-lf | ||
patching file test1 | ||
patching file test2 | ||
patching file test3 | ||
After patch state of c: | ||
./always-crlf: CRLF | ||
./always-lf: LF | ||
./test1: LF | ||
./test2: CRLF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
let set_debug_level n sections = | ||
let debug_sections = | ||
List.fold_left (fun set elt -> OpamCoreConfig.StringSet.add elt set) OpamCoreConfig.StringSet.empty sections | ||
in | ||
OpamCoreConfig.(update ~noop:()) ~debug_level:n ~debug_sections () | ||
|
||
let test_dir = "patcher-test" | ||
|
||
let write_file ~dir ~name use_crlf pattern = | ||
let ch = open_out_bin (Filename.concat dir name) in | ||
let eol = if use_crlf then "\r\n" else "\n" in | ||
List.fold_left (fun a (n, s) -> for i = a to a + n - 1 do Printf.fprintf ch "Line %d%s" i eol done; a + n + s) 1 pattern |> ignore; | ||
close_out ch | ||
|
||
let setup_directory ~dir = | ||
OpamSystem.remove dir; | ||
OpamSystem.mkdir dir; | ||
OpamSystem.chdir dir; | ||
List.iter OpamSystem.mkdir ["a"; "b"; "c"] | ||
|
||
let pattern1 ?(test1 = false) ?(test2 = false) ?(test3 = false) dir = | ||
write_file ~dir ~name:"always-lf" false [(5, 0)]; | ||
write_file ~dir ~name:"always-crlf" true [(5, 0)]; | ||
write_file ~dir ~name:"test1" test1 [(5, 0)]; | ||
write_file ~dir ~name:"test2" test2 [(5, 0)]; | ||
write_file ~dir ~name:"test3" test3 [(5, 0)] | ||
|
||
let pattern2 dir = | ||
write_file ~dir ~name:"always-lf" false [(3, 1); (1, 0);]; | ||
write_file ~dir ~name:"always-crlf" true [(6, 0)]; | ||
write_file ~dir ~name:"test1" false [(6, 0)]; | ||
write_file ~dir ~name:"test2" false [(1, 1); (1, 1); (1, 0)] | ||
|
||
let eol_style ch f = | ||
let s = | ||
match OpamSystem.read_lines CrLf f with | ||
| None -> "mixed" | ||
| Some true -> "CRLF" | ||
| Some false -> "LF" | ||
in | ||
output_string ch s | ||
|
||
let generate_patch () = | ||
flush stderr; flush stdout; | ||
if Sys.command "diff -Naur a b > input.patch" <> 1 then exit 2; | ||
set_debug_level (-3) ["PATCH"]; | ||
OpamSystem.translate_patch ~dir:"c" "input.patch" "output.patch"; | ||
set_debug_level 0 []; | ||
OpamSystem.chdir "c"; | ||
Printf.eprintf "Before patch state of c:\n"; | ||
List.iter (fun f -> Printf.eprintf " %s: %a\n%!" f eol_style f) (OpamSystem.ls "."); | ||
flush stdout; | ||
if Sys.command "patch -p1 -i ../output.patch" <> 0 then exit 2; | ||
Printf.eprintf "After patch state of c:\n"; | ||
List.iter (fun f -> Printf.eprintf " %s: %a\n%!" f eol_style f) (OpamSystem.ls "."); | ||
OpamSystem.chdir Filename.parent_dir_name | ||
|
||
let tests () = | ||
set_debug_level 0 []; | ||
let cwd = Sys.getcwd () in | ||
setup_directory test_dir; | ||
pattern1 "a"; | ||
pattern2 "b"; | ||
pattern1 "c"; | ||
generate_patch (); | ||
pattern1 ~test2:true "c"; | ||
generate_patch (); | ||
OpamSystem.chdir cwd; | ||
OpamSystem.remove test_dir | ||
|
||
let () = | ||
Unix.dup2 Unix.stdout Unix.stderr; | ||
tests () |