Skip to content

Commit 0d2779d

Browse files
committed
tests: add unit tests for parsing of GH links
Check for different possible url schemes and other variations.
1 parent 117ac2c commit 0d2779d

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

test/dune

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
(executable
2-
(name test)
1+
(executables
2+
(names test github_link_test)
33
(libraries lib base base.caml devkit devkit.core extlib lwt.unix stdio
44
yojson)
55
(preprocess
@@ -21,3 +21,8 @@
2121
(alias runtest)
2222
(action
2323
(diff slack_payloads.expected slack_payloads.out)))
24+
25+
(rule
26+
(alias runtest)
27+
(action
28+
(run ./github_link_test.exe)))

test/github_link_test.ml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
open Base
2+
open Printf
3+
open Lib
4+
open Github
5+
open Github_t
6+
7+
let mk_repo ?(scheme = "https") prefix prefix_api : repository =
8+
{
9+
name = "monorepo";
10+
full_name = "ahrefs/monorepo";
11+
url = sprintf "%s://%s/ahrefs/monorepo" scheme prefix;
12+
commits_url = sprintf "%s://%s/repos/ahrefs/monorepo/commits{/sha}" scheme prefix_api;
13+
contents_url = sprintf "%s://%s/repos/ahrefs/monorepo/contents/{+path}" scheme prefix_api;
14+
}
15+
16+
let enterprise_repo1 = mk_repo "example.org" "example.org/api/v3"
17+
18+
let enterprise_repo2 = mk_repo "example.org/path/to/git" "example.org/path/to/git/api/v3"
19+
20+
let enterprise_repo_insecure = mk_repo ~scheme:"http" "example.org" "example.org/api/v3"
21+
22+
let github_repo = mk_repo "github.com" "api.github.com"
23+
24+
let commit_cases prefix repo =
25+
[
26+
sprintf "https://%s/ahrefs/monorepo/commit/69c42640" prefix, Some (Commit (repo, "69c42640"));
27+
sprintf "https://%s/ahrefs/monorepo/commit/69c42640/" prefix, Some (Commit (repo, "69c42640"));
28+
sprintf "https://%s/ahrefs/monorepo/commit/69c42640?arg1=123" prefix, Some (Commit (repo, "69c42640"));
29+
sprintf "https://%s/ahrefs/monorepo/commit/" prefix, None;
30+
sprintf "https://%s/ahrefs/monorepo/commit" prefix, None;
31+
]
32+
33+
let other_cases =
34+
[
35+
"http://github.com/ahrefs/monorepo/commit/69c42640", Some (Commit (github_repo, "69c42640"));
36+
"http://example.org/ahrefs/monorepo/commit/69c42640", Some (Commit (enterprise_repo_insecure, "69c42640"));
37+
"abc", None;
38+
]
39+
40+
let cases =
41+
List.concat
42+
[
43+
commit_cases "github.com" github_repo;
44+
commit_cases "www.github.com" github_repo;
45+
commit_cases "example.org" enterprise_repo1;
46+
commit_cases "example.org/path/to/git" enterprise_repo2;
47+
other_cases;
48+
]
49+
50+
let () = List.iter cases ~f:(fun (input, expected) -> assert (Poly.equal (gh_link_of_string input) expected))

0 commit comments

Comments
 (0)