From 329344570769bac0d4c2767354bc2f49029751b2 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Fri, 15 Dec 2023 13:14:37 +0000 Subject: [PATCH] Consolidate python repoprovider regex tests --- binderhub/tests/test_repoproviders.py | 199 ++++++++++++-------------- 1 file changed, 93 insertions(+), 106 deletions(-) diff --git a/binderhub/tests/test_repoproviders.py b/binderhub/tests/test_repoproviders.py index a17a61575..b0f111f16 100644 --- a/binderhub/tests/test_repoproviders.py +++ b/binderhub/tests/test_repoproviders.py @@ -250,38 +250,6 @@ 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.*"] @@ -520,10 +488,84 @@ def test_gitlab_ref(unresolved_ref, resolved_ref): assert resolved_spec == quote(namespace, safe="") + f"/{ref}" +@pytest.mark.github_api +@pytest.mark.parametrize( + "owner, gist_id, unresolved_ref, resolved_ref", + # https://gist.github.com/mariusvniekerk/8a658f7f63b13768d1e75fa2464f5092.git + [ + ("mariusvniekerk", "8a658f7f63b13768d1e75fa2464f5092", "", True), + ("mariusvniekerk", "8a658f7f63b13768d1e75fa2464f5092", "HEAD", True), + ("mariusvniekerk", "8a658f7f63b13768d1e75fa2464f5092", "master", True), + ( + "mariusvniekerk", + "8a658f7f63b13768d1e75fa2464f5092", + "7daa381aae8409bfe28193e2ed8f767c26371237", + "7daa381aae8409bfe28193e2ed8f767c26371237", + ), + ("mariusvniekerk", "8a658f7f63b13768d1e75fa2464f5092", "nosuchref", None), + ], +) +def test_gist_ref(owner, gist_id, unresolved_ref, resolved_ref): + spec = f"{owner}/{gist_id}/{unresolved_ref}" + + provider = GistRepoProvider(spec=spec) + slug = provider.get_build_slug() + assert slug == gist_id + full_url = provider.get_repo_url() + assert full_url == f"https://gist.github.com/{owner}/{gist_id}.git" + ref = IOLoop().run_sync(provider.get_resolved_ref) + if resolved_ref is True: + # True means it should resolve, but don't check value + assert ref is not None + assert is_valid_sha1(ref) + else: + assert ref == resolved_ref + if not resolved_ref: + # we are done here if we don't expect to resolve + return + ref_url = IOLoop().run_sync(provider.get_resolved_ref_url) + assert ref_url == f"https://gist.github.com/{owner}/{gist_id}/{ref}" + resolved_spec = IOLoop().run_sync(provider.get_resolved_spec) + assert resolved_spec == f"{owner}/{gist_id}/{ref}" + + +@pytest.mark.github_api +def test_gist_secret(): + spec = "{}/{}".format("mariusvniekerk", "bd01411ea4bf4eb8135893ef237398ba") + + provider = GistRepoProvider(spec=spec) + with pytest.raises(ValueError): + IOLoop().run_sync(provider.get_resolved_ref) + + provider = GistRepoProvider(spec=spec, allow_secret_gist=True) + assert IOLoop().run_sync(provider.get_resolved_ref) is not None + + @pytest.mark.parametrize( - "url,groupdict", + "provider,url,groupdict", [ ( + GitHubRepoProvider, + "https://github.com/binder-examples/conda", + {"repo": "binder-examples/conda", "filepath": None, "ref": None}, + ), + ( + GitHubRepoProvider, + "https://github.com/binder-examples/conda/blob/main/index.ipynb", + {"repo": "binder-examples/conda", "ref": "main", "filepath": "index.ipynb"}, + ), + ( + GitHubRepoProvider, + "https://github.com/binder-examples/conda/tree/main/.github/workflows", + { + "repo": "binder-examples/conda", + "ref": "main", + "urlpath": ".github/workflows", + }, + ), + (GitHubRepoProvider, "https://github.com/binder-examples/conda/pulls", None), + ( + GitLabRepoProvider, "https://gitlab.com/owner/repo", { "repo": "owner/repo", @@ -532,10 +574,12 @@ def test_gitlab_ref(unresolved_ref, resolved_ref): }, ), ( + GitLabRepoProvider, "https://gitlab.com/owner/repo/-/tree/branch/folder?ref_type=heads", {"repo": "owner/repo", "ref": "branch", "urlpath": "folder?ref_type=heads"}, ), ( + GitLabRepoProvider, "https://gitlab.com/owner/repo/-/blob/branch/README.md?ref_type=heads", { "repo": "owner/repo", @@ -544,6 +588,7 @@ def test_gitlab_ref(unresolved_ref, resolved_ref): }, ), ( + GitLabRepoProvider, "https://gitlab.com/owner/project/repo", { "repo": "owner/project/repo", @@ -552,6 +597,7 @@ def test_gitlab_ref(unresolved_ref, resolved_ref): }, ), ( + GitLabRepoProvider, "https://gitlab.com/owner/project/repo/-/tree/branch/folder?ref_type=heads", { "repo": "owner/project/repo", @@ -560,6 +606,7 @@ def test_gitlab_ref(unresolved_ref, resolved_ref): }, ), ( + GitLabRepoProvider, "https://gitlab.com/owner/project/repo/-/blob/branch/README.md?ref_type=heads", { "repo": "owner/project/repo", @@ -567,65 +614,13 @@ def test_gitlab_ref(unresolved_ref, resolved_ref): "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", - # https://gist.github.com/mariusvniekerk/8a658f7f63b13768d1e75fa2464f5092.git - [ - ("mariusvniekerk", "8a658f7f63b13768d1e75fa2464f5092", "", True), - ("mariusvniekerk", "8a658f7f63b13768d1e75fa2464f5092", "HEAD", True), - ("mariusvniekerk", "8a658f7f63b13768d1e75fa2464f5092", "master", True), ( - "mariusvniekerk", - "8a658f7f63b13768d1e75fa2464f5092", - "7daa381aae8409bfe28193e2ed8f767c26371237", - "7daa381aae8409bfe28193e2ed8f767c26371237", + GitLabRepoProvider, + "https://gitlab.com/owner/repo/-/merge_requests/123", + None, ), - ("mariusvniekerk", "8a658f7f63b13768d1e75fa2464f5092", "nosuchref", None), - ], -) -def test_gist_ref(owner, gist_id, unresolved_ref, resolved_ref): - spec = f"{owner}/{gist_id}/{unresolved_ref}" - - provider = GistRepoProvider(spec=spec) - slug = provider.get_build_slug() - assert slug == gist_id - full_url = provider.get_repo_url() - assert full_url == f"https://gist.github.com/{owner}/{gist_id}.git" - ref = IOLoop().run_sync(provider.get_resolved_ref) - if resolved_ref is True: - # True means it should resolve, but don't check value - assert ref is not None - assert is_valid_sha1(ref) - else: - assert ref == resolved_ref - if not resolved_ref: - # we are done here if we don't expect to resolve - return - ref_url = IOLoop().run_sync(provider.get_resolved_ref_url) - assert ref_url == f"https://gist.github.com/{owner}/{gist_id}/{ref}" - resolved_spec = IOLoop().run_sync(provider.get_resolved_spec) - assert resolved_spec == f"{owner}/{gist_id}/{ref}" - - -@pytest.mark.parametrize( - "url,groupdict", - [ ( + GistRepoProvider, "https://gist.github.com/owner/0123456789abcde0123456789abcde01", { "repo": "owner/0123456789abcde0123456789abcde01", @@ -633,33 +628,25 @@ def test_gist_ref(owner, gist_id, unresolved_ref, resolved_ref): }, ), ( + GistRepoProvider, "https://gist.github.com/owner/0123456789abcde0123456789abcde01/sha", { "repo": "owner/0123456789abcde0123456789abcde01", "ref": "sha", }, ), - ("https://gist.github.com/owner", None), + (GistRepoProvider, "https://gist.github.com/owner", None), ], ) -def test_gist_regex_detect(url, groupdict): - regex_js = GistRepoProvider.regex_detect +def test_provider_regex_detect(provider, url, groupdict): + regex_js = provider.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) + m = None + for r in regex_py: + m = re.match(r, url) + if m: + break if groupdict: assert m.groupdict() == groupdict else: assert not m - - -@pytest.mark.github_api -def test_gist_secret(): - spec = "{}/{}".format("mariusvniekerk", "bd01411ea4bf4eb8135893ef237398ba") - - provider = GistRepoProvider(spec=spec) - with pytest.raises(ValueError): - IOLoop().run_sync(provider.get_resolved_ref) - - provider = GistRepoProvider(spec=spec, allow_secret_gist=True) - assert IOLoop().run_sync(provider.get_resolved_ref) is not None