Skip to content

Commit

Permalink
Add tests for OpamSystem.translate_patch
Browse files Browse the repository at this point in the history
  • Loading branch information
dra27 committed Jul 11, 2018
1 parent 0637f70 commit 6246c99
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core/opamSystem.mli
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ val get_lock_fd: lock -> Unix.file_descr
didn't apply. *)
val patch: dir:string -> string -> exn option OpamProcess.job

val translate_patch: dir:string -> string -> string -> unit
type _ read_return = Lines : string list read_return
| CrLf : bool option read_return
val read_lines : 'a read_return -> string -> 'a

(** Create a tempory file in {i ~/.opam/logs/<name>XXX}. ?auto_clean controls
whether the file is automatically deleted when opam terminates
(default: [true]). *)
Expand Down
12 changes: 12 additions & 0 deletions tests/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@
((name runtest)
(deps ((files_recursively_in .) ../src/client/opamMain.exe ../src/tools/opam_check.exe))
(action (run make all))))

(executable
((name patcher)
(modules (patcher))
(libraries (opam-core))))

(rule
(with-stdout-to patcher-test.result (run ${exe:patcher.exe})))

(alias
((name runtest)
(action (system "diff ${path:patcher-test.result} ${path:patcher-test.reference}"))))
43 changes: 43 additions & 0 deletions tests/patcher-test.reference
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
73 changes: 73 additions & 0 deletions tests/patcher.ml
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 ()

0 comments on commit 6246c99

Please sign in to comment.