flake_env/tests/flake_env_test_util.re
2024-08-17 09:36:21 -04:00

161 lines
3.9 KiB
ReasonML

open Lib.Util;
open Unix;
let _pp_process_status = (pp_fmt, proc_stat) =>
switch (proc_stat) {
| WEXITED(i) => Fmt.pf(pp_fmt, "Exited normally with status %d", i)
| WSIGNALED(i) => Fmt.pf(pp_fmt, "Killed by signal %d", i)
| WSTOPPED(i) => Fmt.pf(pp_fmt, "Stopped by signal %d", i)
};
let _process_status_eq = (a, b) => {
switch (a, b) {
| (WEXITED(a_i), WEXITED(b_i)) when a_i == b_i => true
| (WSIGNALED(a_i), WSIGNALED(b_i)) when a_i == b_i => true
| (WSTOPPED(a_i), WSTOPPED(b_i)) when a_i == b_i => true
| _ => false
};
};
let testable_process_status =
Alcotest.testable(_pp_process_status, _process_status_eq);
let _syst_to_bool =
fun
| `Yes => true
| _ => false;
let check_process_status =
Alcotest.(check(Alcotest.pair(testable_process_status, string)));
let testable_result_string =
Alcotest.testable(
(pp_fmt, elem) => {
switch (elem) {
| Ok(s) => Fmt.pf(pp_fmt, "Ok(%s)", s)
| Error(s) => Fmt.pf(pp_fmt, "Error(%s)", s)
}
},
(a, b) =>
switch (a, b) {
| (Ok(a), Ok(b)) => String.compare(a, b) == 0
| (Error(a), Error(b)) => String.compare(a, b) == 0
| _ => false
},
);
let check_result_string = Alcotest.(check(testable_result_string));
let check_get_args =
Alcotest.(
check(
Alcotest.result(Alcotest.triple(string, string, list(string)), unit),
)
);
let test_run_process_success = () =>
check_process_status(
"Returns expected",
(WEXITED(0), ""),
run_process("true", []),
);
let test_run_process_failure = () =>
check_process_status(
"Returns expected",
(WEXITED(1), ""),
run_process("false", []),
);
let test_run_process_stdout = () =>
check_process_status(
"Returns expected",
(WEXITED(0), "echoed\n"),
run_process("echo", ["echoed"]),
);
let test_hash_one = () => {
check_result_string(
"Hash matches",
Ok("6ead949bf4bcae230b9ed9cd11e578e34ce9f9ea"),
hash_files(["spit_version.sh"]),
);
};
let test_hash_multiple = () => {
check_result_string(
"Hash matches",
Ok("f109b7892a541ed1e3cf39314cd25d21042b984f"),
hash_files(["spit_version.sh", "spit_version.sh"]),
);
};
let test_hash_filters_nonexistent = () => {
check_result_string(
"Hash matches",
Ok("6ead949bf4bcae230b9ed9cd11e578e34ce9f9ea"),
hash_files(["spit_version.sh", "FOOBARBAZ"]),
);
};
let test_get_args_simple = () => {
check_get_args(
"Parses successfully",
Ok(("foo", "bar", ["oof", "rab", "zab"])),
get_args([|"000", "foo", "bar", "oof", "rab", "zab"|]),
);
};
let test_get_args_just_enough = () => {
check_get_args(
"Parses just enough args",
Ok(("foo", "bar", [])),
get_args([|"000", "foo", "bar"|]),
);
};
let test_get_args_error = () => {
check_get_args(
"Errors on too few args",
Error(),
get_args([|"000", "111"|]),
);
};
let () =
Alcotest.(
run(
"Watches",
[
(
"run_process",
[
test_case("Capture's Stdout", `Quick, test_run_process_stdout),
test_case("Success", `Quick, test_run_process_success),
test_case("Failure", `Quick, test_run_process_failure),
],
),
(
"hash_files",
[
test_case("Hashes one file", `Quick, test_hash_one),
test_case("Hashes multiple files", `Quick, test_hash_multiple),
test_case(
"Filters non-existent",
`Quick,
test_hash_filters_nonexistent,
),
],
),
(
"get_args",
[
test_case("Parses Args", `Quick, test_get_args_simple),
test_case(
"Parses just enough args",
`Quick,
test_get_args_just_enough,
),
test_case("Handles too few args", `Quick, test_get_args_error),
],
),
],
)
);