Skip to content

Commit

Permalink
Port tests to Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterDA committed Nov 2, 2022
1 parent 49bb4fd commit 73fa389
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ jobs:

- run: opam install . --deps-only --with-test

- run: opam exec -- dune build
- run: opam exec -- dune runtest
23 changes: 19 additions & 4 deletions test/mock_exec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ let ( / ) = Filename.concat

let strf = Printf.sprintf

let unix_path path =
if Sys.win32 then
Lwt_process.pread ("", [| "cygpath"; "-u"; path|]) >|= fun str -> String.trim str
else
Lwt.return path

let next_container_id = ref 0

let base_tar =
let mydir = Sys.getcwd () in
Lwt_main.run (Lwt_io.(with_file ~mode:input) (mydir / "base.tar") Lwt_io.read)
Lwt_main.run begin
let base_tar = mydir / "base.tar" in
(* unix_path (mydir / "base.tar") >>= fun base_tar -> *)
Lwt_io.(with_file ~mode:input) base_tar Lwt_io.read
end
|> Bytes.of_string

let with_fd x f =
Expand Down Expand Up @@ -80,9 +90,14 @@ let exec ?cwd ?stdin ?stdout ?stderr ~pp cmd =
Fmt.pr "exec: %a@." Fmt.(Dump.array string) argv;
begin match Array.to_list argv with
| "docker" :: args -> exec_docker ?stdout args
| "sudo" :: "--" :: ("tar" :: _ as tar) -> Os.default_exec ?cwd ?stdin ?stdout ~pp ("", Array.of_list tar)
| "sudo" :: "--" :: "mkdir" :: args
| "mkdir" :: args -> mkdir args
| "sudo" :: "--" :: ("tar" :: _ as tar) when not Os.running_as_root ->
Os.default_exec ?cwd ?stdin ?stdout ~pp ("", Array.of_list tar)
| "tar" :: "-C" :: path :: opts when Os.running_as_root ->
unix_path path >>= fun path ->
let tar = (if Sys.win32 then "C:\\cygwin64\\bin\\tar.exe" else "tar") :: "-C" :: path :: opts in
Os.default_exec ?cwd ?stdin ?stdout ~pp ("", Array.of_list tar)
| "mkdir" :: args when Os.running_as_root -> mkdir args
| "sudo" :: "--" :: "mkdir" :: args when not Os.running_as_root -> mkdir args
| x -> Fmt.failwith "Unknown mock command %a" Fmt.(Dump.list string) x
end
| (x, _) -> Fmt.failwith "Unexpected absolute path: %S" x
10 changes: 9 additions & 1 deletion test/mock_store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ type t = {
mutable builds : int;
}

let unix_path path =
if Sys.win32 then
Lwt_process.pread ("", [| "cygpath"; "-u"; path|]) >|= fun str -> String.trim str
else
Lwt.return path

let delay_store = ref Lwt.return_unit

let rec waitpid_non_intr pid =
Expand All @@ -34,7 +40,8 @@ let build t ?base ~id fn =
begin match base with
| None -> Os.ensure_dir tmp_dir; Lwt.return_unit
| Some base ->
Lwt_process.exec ("", [| "cp"; "-r"; t.dir / base; tmp_dir |]) >>= function
Lwt.both (unix_path (t.dir / base)) (unix_path tmp_dir) >>= fun (src, dst) ->
Lwt_process.exec ("", [| "cp"; "-r"; src; dst |]) >>= function
| Unix.WEXITED 0 -> Lwt.return_unit
| _ -> failwith "cp failed!"
end >>= fun () ->
Expand All @@ -45,6 +52,7 @@ let build t ?base ~id fn =
Unix.rename tmp_dir dir;
Lwt_result.return ()
| Error _ as e ->
unix_path tmp_dir >>= fun tmp_dir ->
rm_r tmp_dir;
Lwt.return e
)
Expand Down
Loading

0 comments on commit 73fa389

Please sign in to comment.