From b1e995532b810160f306cc08ad7d6365799c2b0b Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Mon, 28 Oct 2024 09:45:49 +0100 Subject: [PATCH] add alcotest --- git-kv.opam | 2 ++ test/dune | 5 +++++ test/tests.ml | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 test/tests.ml diff --git a/git-kv.opam b/git-kv.opam index eba381c..155ae63 100644 --- a/git-kv.opam +++ b/git-kv.opam @@ -21,6 +21,8 @@ depends: [ "conf-git" {with-test} "mirage-clock-unix" {with-test} "git-unix" {>= "3.10.0" & with-test} + "alcotest" {>= "1.8.0" & with-test} + "bos" {>= "0.2.1" & with-test} ] build: [ diff --git a/test/dune b/test/dune index 45a02f5..a7a3709 100644 --- a/test/dune +++ b/test/dune @@ -25,3 +25,8 @@ (with-stdout-to git-daemon (run ./git_daemon_exists.exe))) + +(test + (name tests) + (libraries git-kv alcotest bos) + (modules tests)) \ No newline at end of file diff --git a/test/tests.ml b/test/tests.ml new file mode 100644 index 0000000..e32ff76 --- /dev/null +++ b/test/tests.ml @@ -0,0 +1,45 @@ +let ( let* ) = Result.bind + +let run_it cmd = + let* status = Bos.OS.Cmd.run_status cmd in + if status = `Exited 0 then Ok () else + Error (`Msg ("status not 0, but " ^ Fmt.to_to_string Bos.OS.Cmd.pp_status status)) + +let empty_repo () = + let* cwd = Bos.OS.Dir.current () in + let* tmpdir = Bos.OS.Dir.tmp "git-kv-%s" in + let* () = Bos.OS.Dir.set_current tmpdir in + let cmd = Bos.Cmd.(v "git" % "init" % "-q") in + let* () = run_it cmd in + let* () = Bos.OS.Dir.set_current cwd in + let cmd = Bos.Cmd.(v "git" % "daemon" % "--base-path=." % "--export-all" % "--reuseaddr" % "--pid-file=pid" % "--detach") in + let* () = run_it cmd in + let* pid = Bos.OS.File.read (Fpath.v "pid") in + Ok (tmpdir, String.trim pid) + +let kill_git pid = + Unix.kill (int_of_string pid) Sys.sigterm + +let simple () = + match + let* (tmpdir, pid) = empty_repo () in + print_endline ("git started with " ^ Fpath.to_string tmpdir); + print_endline ("git pid " ^ pid); + Unix.sleep 2; + kill_git pid; + print_endline "git killed"; + Ok () + with + | Ok () -> Alcotest.(check bool __LOC__ true true) + | Error `Msg msg -> + print_endline ("got an error from bos: " ^ msg) + +let basic_tests = [ + "Simple", `Quick, simple ; +] + +let tests = [ + "Basic tests", basic_tests ; +] + +let () = Alcotest.run "Git-KV alcotest tests" tests