Skip to content

Commit 40b3c1d

Browse files
committed
modify tests to recreate context per test case
For each test case, initializes a repo state from file if one exists.
1 parent 9e17e7d commit 40b3c1d

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

test/test.ml

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,34 @@ let get_mock_payloads () =
1919
let state_path = Caml.Filename.concat mock_state_dir fn in
2020
if Caml.Sys.file_exists state_path then kind, payload_path, Some state_path else kind, payload_path, None)
2121

22-
let process ~(ctx : Context.t) (kind, path, state_path) =
23-
let%lwt ctx =
22+
let process ~(secrets : Config_t.secrets) ~config (kind, path, state_path) =
23+
let headers = [ "x-github-event", kind ] in
24+
let make_test_context event =
25+
let repo = Github.repo_of_notification @@ Github.parse_exn ~secret:secrets.gh_token headers event in
26+
let ctx = Context.make () in
27+
ctx.secrets <- Some secrets;
28+
ignore (State.find_or_add_repo ctx.state repo.url);
2429
match state_path with
25-
| None -> Lwt.return ctx
30+
| None ->
31+
State.set_repo_config ctx.state ~config ~repo_url:repo.url;
32+
Lwt.return ctx
2633
| Some state_path ->
2734
match Common.get_local_file state_path with
2835
| Error e ->
2936
log#error "failed to read %s: %s" state_path e;
3037
Lwt.return ctx
3138
| Ok file ->
32-
let state = State_j.state_of_string file in
33-
state.config <- ctx.state.config;
34-
Lwt.return { ctx with state }
39+
let repo_state = State_j.repo_state_of_string file in
40+
Hashtbl.set ctx.state.repos ~key:repo.url ~data:repo_state;
41+
State.set_repo_config ctx.state ~repo_url:repo.url ~config;
42+
Lwt.return ctx
3543
in
3644
Stdio.printf "===== file %s =====\n" path;
3745
let headers = [ "x-github-event", kind ] in
3846
match Common.get_local_file path with
3947
| Error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
4048
| Ok event ->
49+
let%lwt ctx = make_test_context event in
4150
let%lwt _ctx = Action_local.process_github_notification ctx headers event in
4251
Lwt.return_unit
4352

@@ -53,12 +62,10 @@ let () =
5362
log#error "%s" e;
5463
Lwt.return_unit
5564
| Ok config ->
56-
ctx.state.config <- Some config;
57-
( match Context.refresh_secrets ctx with
58-
| Ok ctx -> Lwt_list.iter_s (process ~ctx) payloads
59-
| Error e ->
60-
log#error "failed to read secrets:";
61-
log#error "%s" e;
62-
Lwt.return_unit
63-
)
65+
match Context.refresh_secrets ctx with
66+
| Ok ctx -> Lwt_list.iter_s (process ~secrets:(Option.value_exn ctx.secrets) ~config) payloads
67+
| Error e ->
68+
log#error "failed to read secrets:";
69+
log#error "%s" e;
70+
Lwt.return_unit
6471
)

0 commit comments

Comments
 (0)