Skip to content

Commit c12c551

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

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
@@ -18,24 +18,34 @@ let get_mock_payloads () =
1818
if Caml.Sys.file_exists state_path then kind, payload_path, Some state_path else kind, payload_path, None
1919
)
2020

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

@@ -51,12 +61,10 @@ let () =
5161
log#error "%s" e;
5262
Lwt.return_unit
5363
| Ok config ->
54-
let ctx = { ctx with config = Some config } in
55-
( match Context.refresh_secrets ctx with
56-
| Ok ctx -> Lwt_list.iter_s (process ~ctx) payloads
57-
| Error e ->
58-
log#error "failed to read secrets:";
59-
log#error "%s" e;
60-
Lwt.return_unit
61-
)
64+
match Context.refresh_secrets ctx with
65+
| Ok ctx -> Lwt_list.iter_s (process ~secrets:(Option.value_exn ctx.secrets) ~config) payloads
66+
| Error e ->
67+
log#error "failed to read secrets:";
68+
log#error "%s" e;
69+
Lwt.return_unit
6270
)

0 commit comments

Comments
 (0)