From fd70418ba7d91171f1df6bcdd5e3d567682e67dc Mon Sep 17 00:00:00 2001 From: Simon Li Date: Mon, 11 Dec 2023 00:09:40 +0000 Subject: [PATCH] Test repoprovider UI regexs --- binderhub/tests/test_repoproviders.py | 117 ++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/binderhub/tests/test_repoproviders.py b/binderhub/tests/test_repoproviders.py index df5f63e0e..5d8b1bb5c 100644 --- a/binderhub/tests/test_repoproviders.py +++ b/binderhub/tests/test_repoproviders.py @@ -250,6 +250,38 @@ def test_github_ref(repo, unresolved_ref, resolved_ref): assert resolved_spec == f"{repo}/{ref}" +@pytest.mark.parametrize( + "url,groupdict", + [ + ( + "https://github.com/binder-examples/conda", + {"repo": "binder-examples/conda", "filepath": None, "ref": None}, + ), + ( + "https://github.com/binder-examples/conda/blob/main/index.ipynb", + {"repo": "binder-examples/conda", "ref": "main", "filepath": "index.ipynb"}, + ), + ( + "https://github.com/binder-examples/conda/tree/main/.github/workflows", + { + "repo": "binder-examples/conda", + "ref": "main", + "urlpath": ".github/workflows", + }, + ), + ("https://github.com/binder-examples/conda/pulls", None), + ], +) +def test_github_regex_detect(url, groupdict): + regex_js = GitHubRepoProvider.regex_detect + regex_py = [r.replace("(?<", "(?P<") for r in regex_js] + m = re.match(regex_py[0], url) or re.match(regex_py[1], url) + if groupdict: + assert m.groupdict() == groupdict + else: + assert not m + + def test_not_banned(): provider = GitHubRepoProvider( spec="jupyterhub/zero-to-jupyterhub-k8s/v0.4", banned_specs=["^yuvipanda.*"] @@ -488,6 +520,67 @@ def test_gitlab_ref(unresolved_ref, resolved_ref): assert resolved_spec == quote(namespace, safe="") + f"/{ref}" +@pytest.mark.parametrize( + "url,groupdict", + [ + ( + "https://gitlab.com/owner/repo", + { + "repo": "owner/repo", + "ref": None, + "filepath": None, + }, + ), + ( + "https://gitlab.com/owner/repo/-/tree/branch/folder?ref_type=heads", + {"repo": "owner/repo", "ref": "branch", "urlpath": "folder?ref_type=heads"}, + ), + ( + "https://gitlab.com/owner/repo/-/blob/branch/README.md?ref_type=heads", + { + "repo": "owner/repo", + "ref": "branch", + "filepath": "README.md?ref_type=heads", + }, + ), + ( + "https://gitlab.com/owner/project/repo", + { + "repo": "owner/project/repo", + "ref": None, + "filepath": None, + }, + ), + ( + "https://gitlab.com/owner/project/repo/-/tree/branch/folder?ref_type=heads", + { + "repo": "owner/project/repo", + "ref": "branch", + "urlpath": "folder?ref_type=heads", + }, + ), + ( + "https://gitlab.com/owner/project/repo/-/blob/branch/README.md?ref_type=heads", + { + "repo": "owner/project/repo", + "ref": "branch", + "filepath": "README.md?ref_type=heads", + }, + ), + ("https://gitlab.com/owner/repo/-/merge_requests/123", None), + ], +) +def test_gitlab_regex_detect(url, groupdict): + regex_js = GitLabRepoProvider.regex_detect + regex_py = [r.replace("(?<", "(?P<") for r in regex_js] + assert [r.replace("(?P<", "(?<") for r in regex_py] == regex_js + m = re.match(regex_py[0], url) or re.match(regex_py[1], url) + if groupdict: + assert m.groupdict() == groupdict + else: + assert not m + + @pytest.mark.github_api @pytest.mark.parametrize( "owner, gist_id, unresolved_ref, resolved_ref", @@ -529,6 +622,30 @@ def test_gist_ref(owner, gist_id, unresolved_ref, resolved_ref): assert resolved_spec == f"{owner}/{gist_id}/{ref}" +@pytest.mark.parametrize( + "url,groupdict", + [ + ( + "https://gist.github.com/owner/0123456789abcde0123456789abcde01", + { + "repo": "owner/0123456789abcde0123456789abcde01", + "ref": None, + }, + ), + ("https://gist.github.com/owner", None), + ], +) +def test_gist_regex_detect(url, groupdict): + regex_js = GistRepoProvider.regex_detect + regex_py = [r.replace("(?<", "(?P<") for r in regex_js] + assert [r.replace("(?P<", "(?<") for r in regex_py] == regex_js + m = re.match(regex_py[0], url) + if groupdict: + assert m.groupdict() == groupdict + else: + assert not m + + @pytest.mark.github_api def test_gist_secret(): spec = "{}/{}".format("mariusvniekerk", "bd01411ea4bf4eb8135893ef237398ba")