Skip to content

Commit 5f7c02c

Browse files
committed
tests: load secrets and config files per test case
Make secrets, config, state, and incoming payload configurable individually, so that it's easier to test multi-repo behavior.
1 parent 0fdd590 commit 5f7c02c

File tree

5 files changed

+43
-46
lines changed

5 files changed

+43
-46
lines changed

lib/api_local.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ let cwd = Caml.Sys.getcwd ()
77
let cache_dir = Caml.Filename.concat cwd "github-api-cache"
88

99
module Github : Api.Github = struct
10+
let mock_config_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_config"
11+
1012
let get_config ~(ctx : Context.t) ~repo:_ =
11-
let url = Caml.Filename.concat cwd ctx.config_filename in
13+
let url = Caml.Filename.concat mock_config_dir ctx.config_filename in
1214
match get_local_file url with
1315
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get config %s" e url
1416
| Ok file -> Lwt.return @@ Ok (Config_j.config_of_string file)
File renamed without changes.
File renamed without changes.

test/dune

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
(deps
1010
(source_tree ../mock_states)
1111
(source_tree ../mock_payloads)
12-
(source_tree github-api-cache)
13-
monorobot.json
14-
secrets.json)
12+
(source_tree ../mock_config)
13+
(source_tree ../mock_secrets)
14+
(source_tree github-api-cache))
1515
(action
1616
(with-stdout-to
1717
slack_payloads.out

test/test.ml

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
open Base
22
open Lib
3+
open Common
34

45
let log = Devkit.Log.from "test"
56

7+
let () = Devkit.Log.set_loglevels "error"
8+
69
let mock_payload_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_payloads"
710

811
let mock_state_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_states"
912

13+
let mock_secrets_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_secrets"
14+
15+
let mock_config_dir = Caml.Filename.concat Caml.Filename.parent_dir_name "mock_config"
16+
1017
module Action_local = Action.Action (Api_local.Github) (Api_local.Slack)
1118

1219
let get_mock_payloads () =
@@ -16,56 +23,44 @@ let get_mock_payloads () =
1623
|> List.filter_map ~f:(fun fn -> Github.event_of_filename fn |> Option.map ~f:(fun kind -> kind, fn))
1724
|> List.map ~f:(fun (kind, fn) ->
1825
let payload_path = Caml.Filename.concat mock_payload_dir fn in
19-
let state_path = Caml.Filename.concat mock_state_dir fn in
20-
if Caml.Sys.file_exists state_path then kind, payload_path, Some state_path else kind, payload_path, None)
26+
let state_filepath =
27+
let path = Caml.Filename.concat mock_state_dir fn in
28+
if Caml.Sys.file_exists path then Some path else None
29+
in
30+
let secrets_filepath =
31+
let path = Caml.Filename.concat mock_secrets_dir fn in
32+
if Caml.Sys.file_exists path then Some path else None
33+
in
34+
let config_filename =
35+
let path = Caml.Filename.concat mock_config_dir fn in
36+
if Caml.Sys.file_exists path then Some fn else None
37+
in
38+
kind, payload_path, state_filepath, secrets_filepath, config_filename)
2139

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);
29-
match state_path with
30-
| None ->
31-
Context.set_repo_config ctx repo.url config;
32-
Lwt.return ctx
33-
| Some state_path ->
34-
match Common.get_local_file state_path with
35-
| Error e ->
36-
log#error "failed to read %s: %s" state_path e;
37-
Lwt.return ctx
38-
| Ok file ->
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
40+
let process (kind, path, state_filepath, secrets_filepath, config_filename) =
41+
let make_test_context () =
42+
let secrets_filepath =
43+
Option.value ~default:(Caml.Filename.concat mock_secrets_dir Context.default_secrets_filepath) secrets_filepath
44+
in
45+
let ctx = Context.make ?config_filename ~secrets_filepath ?state_filepath () in
46+
match Context.refresh_state ctx with
47+
| Error e -> fmt_error "failed to read state: %s" e
48+
| Ok ctx ->
49+
match Context.refresh_secrets ctx with
50+
| Error e -> fmt_error "failed to read secrets: %s" e
51+
| Ok ctx -> Ok ctx
4352
in
4453
Stdio.printf "===== file %s =====\n" path;
4554
let headers = [ "x-github-event", kind ] in
46-
match Common.get_local_file path with
55+
match get_local_file path with
4756
| Error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
4857
| Ok event ->
49-
let%lwt ctx = make_test_context event in
58+
match make_test_context () with
59+
| Error e -> Lwt.return @@ log#error "%s" e
60+
| Ok ctx ->
5061
let%lwt _ctx = Action_local.process_github_notification ctx headers event in
5162
Lwt.return_unit
5263

5364
let () =
5465
let payloads = get_mock_payloads () in
55-
let repo : Github_t.repository =
56-
{ name = ""; full_name = ""; url = ""; commits_url = ""; contents_url = ""; pulls_url = ""; issues_url = "" }
57-
in
58-
let ctx = Context.make ~state_filepath:"state.json" () in
59-
Lwt_main.run
60-
( match%lwt Api_local.Github.get_config ~ctx ~repo with
61-
| Error e ->
62-
log#error "%s" e;
63-
Lwt.return_unit
64-
| Ok config ->
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
71-
)
66+
Lwt_main.run (Lwt_list.iter_s process payloads)

0 commit comments

Comments
 (0)