Skip to content

Commit 753830d

Browse files
committed
tests: load secrets and config files per test case
We can now configure secrets, config, state, and incoming payload separately, which lets us test multi-repo behavior more easily.
1 parent 19cc0d1 commit 753830d

File tree

7 files changed

+42
-53
lines changed

7 files changed

+42
-53
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.
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11

22
{
3-
"pipeline_statuses": {
4-
"default": {
5-
"master": "failure"
6-
},
7-
"buildkite/notabot-test": {
8-
"master": "success"
3+
"https://git.ahrefs.com/ahrefs/notabot_test": {
4+
"pipeline_statuses": {
5+
"default": {
6+
"master": "failure"
7+
},
8+
"buildkite/notabot-test": {
9+
"master": "success"
10+
}
911
}
1012
}
11-
}
13+
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
2-
"pipeline_statuses": {
3-
"buildkite/notabot-test": {
4-
"master": "failure"
2+
"https://git.ahrefs.com/ahrefs/notabot_test": {
3+
"pipeline_statuses": {
4+
"buildkite/notabot-test": {
5+
"master": "failure"
6+
}
57
}
68
}
7-
}
9+
}

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-
notabot.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: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
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+
1015
module Action_local = Action.Action (Api_local.Github) (Api_local.Slack)
1116

1217
let get_mock_payloads () =
@@ -19,51 +24,29 @@ let get_mock_payloads () =
1924
let state_path = Caml.Filename.concat mock_state_dir fn in
2025
if Caml.Sys.file_exists state_path then kind, payload_path, Some state_path else kind, payload_path, None)
2126

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-
State.set_repo_config ctx.state ~config ~repo_url:repo.url;
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 ~key:repo.url ~data:repo_state;
41-
State.set_repo_config ctx.state ~repo_url:repo.url ~config;
42-
Lwt.return ctx
27+
let process (kind, path, state_filepath) =
28+
let make_test_context () =
29+
let config_filename = Context.default.config_filename in
30+
let secrets_filepath = Caml.Filename.concat mock_secrets_dir Context.default.secrets_filepath in
31+
let ctx = Context.make ~config_filename ~secrets_filepath ?state_filepath ~verbose:false () in
32+
match Context.refresh_state ctx with
33+
| Error e -> fmt_error "failed to read state: %s" e
34+
| Ok ctx ->
35+
match Context.refresh_secrets ctx with
36+
| Error e -> fmt_error "failed to read secrets: %s" e
37+
| Ok ctx -> Ok ctx
4338
in
4439
Stdio.printf "===== file %s =====\n" path;
4540
let headers = [ "x-github-event", kind ] in
46-
match Common.get_local_file path with
41+
match get_local_file path with
4742
| Error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
4843
| Ok event ->
49-
let%lwt ctx = make_test_context event in
44+
match make_test_context () with
45+
| Error e -> Lwt.return @@ log#error "%s" e
46+
| Ok ctx ->
5047
let%lwt _ctx = Action_local.process_github_notification ctx headers event in
5148
Lwt.return_unit
5249

5350
let () =
5451
let payloads = get_mock_payloads () in
55-
let repo : Github_t.repository = { name = ""; full_name = ""; url = ""; commits_url = ""; contents_url = "" } in
56-
let ctx = Context.make ~state_filepath:"state.json" () in
57-
Lwt_main.run
58-
( match%lwt Api_local.Github.get_config ~ctx ~repo with
59-
| Error e ->
60-
log#error "%s" e;
61-
Lwt.return_unit
62-
| Ok config ->
63-
match Context.refresh_secrets ctx with
64-
| Ok ctx -> Lwt_list.iter_s (process ~secrets:(Option.value_exn ctx.secrets) ~config) payloads
65-
| Error e ->
66-
log#error "failed to read secrets:";
67-
log#error "%s" e;
68-
Lwt.return_unit
69-
)
52+
Lwt_main.run (Lwt_list.iter_s process payloads)

0 commit comments

Comments
 (0)