1
1
open Base
2
2
open Lib
3
+ open Common
3
4
4
5
let log = Devkit.Log. from " test"
5
6
7
+ let () = Devkit.Log. set_loglevels " error"
8
+
6
9
let mock_payload_dir = Caml.Filename. concat Caml.Filename. parent_dir_name " mock_payloads"
7
10
8
11
let mock_state_dir = Caml.Filename. concat Caml.Filename. parent_dir_name " mock_states"
9
12
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
+
10
17
module Action_local = Action. Action (Api_local. Github ) (Api_local. Slack )
11
18
12
19
let get_mock_payloads () =
@@ -16,56 +23,44 @@ let get_mock_payloads () =
16
23
|> List. filter_map ~f: (fun fn -> Github. event_of_filename fn |> Option. map ~f: (fun kind -> kind, fn))
17
24
|> List. map ~f: (fun (kind , fn ) ->
18
25
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)
21
39
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
43
52
in
44
53
Stdio. printf " ===== file %s =====\n " path;
45
54
let headers = [ " x-github-event" , kind ] in
46
- match Common. get_local_file path with
55
+ match get_local_file path with
47
56
| Error e -> Lwt. return @@ log#error " failed to read %s: %s" path e
48
57
| 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 ->
50
61
let % lwt _ctx = Action_local. process_github_notification ctx headers event in
51
62
Lwt. return_unit
52
63
53
64
let () =
54
65
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