Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tests more robust #271

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions nbgitpuller/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,25 @@ def branch_exists(self, branch):
This checks to make sure the branch we are told to access
exists in the repo
"""
try:
heads = subprocess.run(
["git", "ls-remote", "--heads", "--", self.git_url],
capture_output=True,
text=True,
check=True
)
tags = subprocess.run(
["git", "ls-remote", "--tags", "--", self.git_url],
capture_output=True,
text=True,
check=True
)
lines = heads.stdout.splitlines() + tags.stdout.splitlines()
branches = []
for line in lines:
_, ref = line.split()
refs, heads, branch_name = ref.split("/", 2)
branches.append(branch_name)
return branch in branches
except subprocess.CalledProcessError:
m = f"Problem accessing list of branches and/or tags: {self.git_url}"
logging.exception(m)
raise ValueError(m)
heads = subprocess.run(
["git", "ls-remote", "--heads", "--", self.git_url],
capture_output=True,
text=True,
check=True
)
tags = subprocess.run(
["git", "ls-remote", "--tags", "--", self.git_url],
capture_output=True,
text=True,
check=True
)
lines = heads.stdout.splitlines() + tags.stdout.splitlines()
branches = []
for line in lines:
_, ref = line.split()
refs, heads, branch_name = ref.split("/", 2)
branches.append(branch_name)
return branch in branches

def resolve_default_branch(self):
"""
Expand Down
81 changes: 81 additions & 0 deletions tests/repohelpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
Helper classes for creating git repos
"""
import os
import tempfile
import shutil
import subprocess as sp
from uuid import uuid4

from nbgitpuller import GitPuller


class Repository:
def __init__(self, path=None):
if path is None:
path = os.path.join(tempfile.gettempdir(), str(uuid4()))
self.path = path

def __enter__(self):
os.makedirs(self.path, exist_ok=True)
self.git('init', '--bare')
return self

def __exit__(self, *args):
shutil.rmtree(self.path)

def write_file(self, path, content):
with open(os.path.join(self.path, path), 'w') as f:
f.write(content)

def read_file(self, path):
with open(os.path.join(self.path, path)) as f:
return f.read()

def git(self, *args):
return sp.check_output(
['git'] + list(args),
cwd=self.path,
stderr=sp.STDOUT
).decode().strip()


class Remote(Repository):
pass


class Pusher(Repository):
def __init__(self, remote, path=None):
self.remote = remote
super().__init__(path=path)

def __enter__(self):
sp.check_output(['git', 'clone', self.remote.path, self.path])
self.git('config', '--local', 'user.email', '[email protected]')
self.git('config', '--local', 'user.name', 'pusher')
return self

def push_file(self, path, content):
self.write_file(path, content)
self.git('add', path)
self.git('commit', '-am', 'Ignore the message')
self.git('push', 'origin', 'master')


class Puller(Repository):
def __init__(self, remote, path=None, branch="master", *args, **kwargs):
super().__init__(path)
remotepath = "file://%s" % os.path.abspath(remote.path)
self.gp = GitPuller(remotepath, self.path, branch=branch, *args, **kwargs)

def pull_all(self):
for line in self.gp.pull():
print('{}: {}'.format(self.path, line.rstrip()))

def __enter__(self):
print()
self.pull_all()
self.git('config', '--local', 'user.email', '[email protected]')
self.git('config', '--local', 'user.name', 'puller')
return self

80 changes: 47 additions & 33 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from uuid import uuid4
import pytest

from repohelpers import Pusher, Remote

PORT = os.getenv('TEST_PORT', 18888)


Expand Down Expand Up @@ -58,17 +60,22 @@ def test_clone_default(self, tmpdir, backend_type):
"""
jupyterdir = str(tmpdir)
self.start_jupyter(jupyterdir, {}, backend_type)
params = {
'repo': 'https://github.com/binder-examples/jupyter-extension',
'branch': 'master',
}
r = request_api(params)
assert r.code == 200
s = r.read().decode()
print(s)
assert '--branch master' in s
assert "Cloning into '{}/{}'".format(jupyterdir, 'jupyter-extension') in s
assert os.path.isdir(os.path.join(jupyterdir, 'jupyter-extension', '.git'))

with Remote() as remote, Pusher(remote) as pusher:
pusher.push_file('README.md', 'Testing some content')
print(f'path: {remote.path}')
params = {
'repo': remote.path,
'branch': 'master',
}
r = request_api(params)
assert r.code == 200
s = r.read().decode()
print(s)
target_path = os.path.join(jupyterdir, os.path.basename(remote.path))
assert '--branch master' in s
assert f"Cloning into '{target_path}" in s
assert os.path.isdir(os.path.join(target_path, '.git'))

@pytest.mark.parametrize(
"backend_type",
Expand All @@ -84,17 +91,20 @@ def test_clone_targetpath(self, tmpdir, backend_type):
jupyterdir = str(tmpdir)
target = str(uuid4())
self.start_jupyter(jupyterdir, {}, backend_type)
params = {
'repo': 'https://github.com/binder-examples/jupyter-extension',
'branch': 'master',
'targetpath': target,
}
r = request_api(params)
assert r.code == 200
s = r.read().decode()
print(s)
assert "Cloning into '{}/{}'".format(jupyterdir, target) in s
assert os.path.isdir(os.path.join(jupyterdir, target, '.git'))
with Remote() as remote, Pusher(remote) as pusher:
pusher.push_file('README.md', 'Testing some content')
params = {
'repo': remote.path,
'branch': 'master',
'targetpath': target,
}
r = request_api(params)
assert r.code == 200
s = r.read().decode()
print(s)
target_path = os.path.join(jupyterdir, target)
assert f"Cloning into '{target_path}" in s
assert os.path.isdir(os.path.join(target_path, '.git'))

@pytest.mark.parametrize(
"backend_type",
Expand All @@ -111,14 +121,18 @@ def test_clone_parenttargetpath(self, tmpdir, backend_type):
parent = str(uuid4())
target = str(uuid4())
self.start_jupyter(jupyterdir, {'NBGITPULLER_PARENTPATH': parent}, backend_type)
params = {
'repo': 'https://github.com/binder-examples/jupyter-extension',
'branch': 'master',
'targetpath': target,
}
r = request_api(params)
assert r.code == 200
s = r.read().decode()
print(s)
assert "Cloning into '{}/{}/{}'".format(jupyterdir, parent, target) in s
assert os.path.isdir(os.path.join(jupyterdir, parent, target, '.git'))

with Remote() as remote, Pusher(remote) as pusher:
pusher.push_file('README.md', 'Testing some content')
params = {
'repo': remote.path,
'branch': 'master',
'targetpath': target,
}
r = request_api(params)
assert r.code == 200
s = r.read().decode()
print(s)
target_path = os.path.join(jupyterdir, parent, target)
assert f"Cloning into '{target_path}" in s
assert os.path.isdir(os.path.join(target_path, '.git'))
Loading