Skip to content

Commit

Permalink
modify tests to recreate context per test case
Browse files Browse the repository at this point in the history
For each test case, initializes a repo state from file if one exists.
  • Loading branch information
yasunariw committed Dec 23, 2020
1 parent 5860f80 commit 0dce9e2
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)

0 comments on commit 0dce9e2

Please sign in to comment.