From 0dce9e20704f5df6691cca727ca29544a4aa5688 Mon Sep 17 00:00:00 2001 From: Yasunari Watanabe Date: Wed, 23 Dec 2020 19:41:37 +0800 Subject: [PATCH] modify tests to recreate context per test case For each test case, initializes a repo state from file if one exists. --- test/test.ml | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/test/test.ml b/test/test.ml index e32e6713..71d77f54 100644 --- a/test/test.ml +++ b/test/test.ml @@ -19,23 +19,31 @@ let get_mock_payloads () = let state_path = Caml.Filename.concat mock_state_dir fn in if Caml.Sys.file_exists state_path then kind, payload_path, Some state_path else kind, payload_path, None) -let process ~(ctx : Context.t) (kind, path, state_path) = - let%lwt ctx = +let process ~(secrets : Config_t.secrets) ~config (kind, path, state_path) = + let headers = [ "x-github-event", kind ] in + let make_test_context event = + let repo = Github.repo_of_notification @@ Github.parse_exn ~secret:secrets.gh_token headers event in + let ctx = Context.make () in + ctx.secrets <- Some secrets; + ignore (State.get_or_init_repo_state ctx.state repo.full_name); match state_path with - | None -> Lwt.return ctx + | None -> + State.set_repo_config ctx.state ~config ~repo_name:repo.full_name; + Lwt.return ctx | Some state_path -> try - let state = state_path |> Common.get_local_file |> State_j.state_of_string in - state.config <- ctx.state.config; - Lwt.return { ctx with state } + let repo_state = state_path |> Common.get_local_file |> State_j.repo_state_of_string in + Hashtbl.set ctx.state ~key:repo.full_name ~data:repo_state; + State.set_repo_config ctx.state ~repo_name:repo.full_name ~config; + Lwt.return ctx with Sys_error e -> log#error "failed to read %s: %s" state_path e; Lwt.return ctx in Stdio.printf "===== file %s =====\n" path; - let headers = [ "x-github-event", kind ] in try let event = Common.get_local_file path in + let%lwt ctx = make_test_context event in let%lwt _ctx = Action_local.process_github_notification ctx headers event in Lwt.return_unit with Sys_error e -> Lwt.return @@ log#error "failed to read %s: %s" path e @@ -50,12 +58,10 @@ let () = log#error "%s" e; Lwt.return_unit | Ok config -> - ctx.state.config <- Some config; - ( match Context.refresh_secrets ctx with - | Ok ctx -> Lwt_list.iter_s (process ~ctx) payloads - | Error e -> - log#error "failed to read secrets:"; - log#error "%s" e; - Lwt.return_unit - ) + match Context.refresh_secrets ctx with + | Ok ctx -> Lwt_list.iter_s (process ~secrets:(Option.value_exn ctx.secrets) ~config) payloads + | Error e -> + log#error "failed to read secrets:"; + log#error "%s" e; + Lwt.return_unit )