Skip to content

Commit

Permalink
Consolidate python repoprovider regex tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manics committed Dec 15, 2023
1 parent 78be797 commit f673e1b
Showing 1 changed file with 93 additions and 106 deletions.
199 changes: 93 additions & 106 deletions binderhub/tests/test_repoproviders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"]
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -544,6 +588,7 @@ def test_gitlab_ref(unresolved_ref, resolved_ref):
},
),
(
GitLabRepoProvider,
"https://gitlab.com/owner/project/repo",
{
"repo": "owner/project/repo",
Expand All @@ -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",
Expand All @@ -560,106 +606,47 @@ 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",
"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",
# 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",
"ref": None,
},
),
(
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

0 comments on commit f673e1b

Please sign in to comment.