Skip to content

Commit d08251b

Browse files
committed
test: recreate context per test case
For each test case, initializes a repo state from file if one exists.
1 parent 680766e commit d08251b

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

test/test.ml

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,34 @@ let get_mock_payloads () =
2020
if Caml.Sys.file_exists state_path then kind, payload_path, Some state_path else kind, payload_path, None
2121
)
2222

23-
let process ~(ctx : Context.t) (kind, path, state_path) =
24-
let%lwt ctx =
23+
let process ~(secrets : Config_t.secrets) ~config (kind, path, state_path) =
24+
let headers = [ "x-github-event", kind ] in
25+
let make_test_context event =
26+
let repo = Github.repo_of_notification @@ Github.parse_exn headers event in
27+
let ctx = Context.make () in
28+
ctx.secrets <- Some secrets;
29+
ignore (State.find_or_add_repo ctx.state repo.url);
2530
match state_path with
26-
| None -> Lwt.return ctx
31+
| None ->
32+
Context.set_repo_config ctx repo.url config;
33+
Lwt.return ctx
2734
| Some state_path ->
2835
match Common.get_local_file state_path with
2936
| Error e ->
3037
log#error "failed to read %s: %s" state_path e;
3138
Lwt.return ctx
3239
| Ok file ->
33-
let state = State_j.state_of_string file in
34-
Lwt.return { ctx with state }
40+
let repo_state = State_j.repo_state_of_string file in
41+
Common.Stringtbl.set ctx.state.repos ~key:repo.url ~data:repo_state;
42+
Context.set_repo_config ctx repo.url config;
43+
Lwt.return ctx
3544
in
3645
Stdio.printf "===== file %s =====\n" path;
3746
let headers = [ "x-github-event", kind ] in
3847
match Common.get_local_file path with
3948
| Error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
4049
| Ok event ->
50+
let%lwt ctx = make_test_context event in
4151
let%lwt _ctx = Action_local.process_github_notification ctx headers event in
4252
Lwt.return_unit
4353

@@ -53,12 +63,10 @@ let () =
5363
log#error "%s" e;
5464
Lwt.return_unit
5565
| Ok config ->
56-
let ctx = { ctx with config = Some config } in
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-
)
66+
match Context.refresh_secrets ctx with
67+
| Ok ctx -> Lwt_list.iter_s (process ~secrets:(Option.value_exn ctx.secrets) ~config) payloads
68+
| Error e ->
69+
log#error "failed to read secrets:";
70+
log#error "%s" e;
71+
Lwt.return_unit
6472
)

0 commit comments

Comments
 (0)