Skip to content

Commit c39d7a5

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 419e6e1 commit c39d7a5

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
@@ -19,24 +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 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+
Context.set_repo_config ctx repo.url config;
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-
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+
Context.set_repo_config ctx repo.url config;
42+
Lwt.return ctx
3443
in
3544
Stdio.printf "===== file %s =====\n" path;
3645
let headers = [ "x-github-event", kind ] in
3746
match Common.get_local_file path with
3847
| Error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
3948
| Ok event ->
49+
let%lwt ctx = make_test_context event in
4050
let%lwt _ctx = Action_local.process_github_notification ctx headers event in
4151
Lwt.return_unit
4252

@@ -52,12 +62,10 @@ let () =
5262
log#error "%s" e;
5363
Lwt.return_unit
5464
| Ok config ->
55-
let ctx = { ctx with config = Some config } in
56-
( match Context.refresh_secrets ctx with
57-
| Ok ctx -> Lwt_list.iter_s (process ~ctx) payloads
58-
| Error e ->
59-
log#error "failed to read secrets:";
60-
log#error "%s" e;
61-
Lwt.return_unit
62-
)
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
6371
)

0 commit comments

Comments
 (0)