Skip to content

Commit

Permalink
Test without connecting to github
Browse files Browse the repository at this point in the history
Try accessing the local git repo in the project root first when cloning
example_package. Only if that doesn't work, fall back to online github.

Also adds some extra local path tests to init/test_unit.
Also removes one unnecessary os.getcwd() call.
Also removes the old "abc" template string hack since we test the
current template now.
  • Loading branch information
j4b6ski committed Dec 9, 2024
1 parent 28b5c6a commit e91a376
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
7 changes: 1 addition & 6 deletions src/sinol_make/commands/init/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def download_template(self, tmpdir, template_paths = [DEFAULT_TEMPLATE, DEFAULT_
path = os.path.join(tmpdir, subdir)
else:
path = os.path.join(tmpdir, 'template')
shutil.copytree(os.path.join(os.getcwd(), template, subdir), path)
shutil.copytree(os.path.join(template, subdir), path)

if os.path.exists(os.path.join(path, '.git')):
shutil.rmtree(os.path.join(path, '.git'))
Expand All @@ -70,9 +70,6 @@ def move_folder(self):
raise
for file in files:
dest_filename = file
# TODO: remove the old 'abc' template
if file[:3] == 'abc':
dest_filename = self.task_id + file[3:]
if file[:len(self.TEMPLATE_ID)] == self.TEMPLATE_ID:
dest_filename = self.task_id + file[len(self.TEMPLATE_ID):]
shutil.move(os.path.join(root, file), os.path.join(mapping[root], dest_filename))
Expand All @@ -87,8 +84,6 @@ def update_task_id(self):
except UnicodeDecodeError:
# ignore non-text files
continue
# TODO: remove the old "abc" template
file_data = file_data.replace('abc', self.task_id)
file_data = file_data.replace(self.TEMPLATE_ID, self.task_id)

with open(path, 'w') as file:
Expand Down
16 changes: 13 additions & 3 deletions tests/commands/init/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@


@pytest.mark.parametrize("temp_workdir", [''], indirect=True)
def test_simple(capsys, temp_workdir):
def test_init_clones_default_template(capsys, request, temp_workdir):
"""
Test `init` command.
"""
parser = configure_parsers()
args = parser.parse_args(["init", "xyz"])
args = ["init", "xyz"]

# try to avoid connecting to github when cloning example_package
git_dir = os.path.join(request.config.rootdir, '.git')
if os.path.exists(git_dir):
git_local_url = os.path.join('file://', request.config.rootdir)
args.extend(["-t", git_local_url, "example_package"])
# currently this does fallback on github if the local repo is not available
# if needed we could take a dependency on gitpython and mock up a repo

args = parser.parse_args(args)
command = Command()
command.run(args)
out = capsys.readouterr().out
Expand All @@ -23,7 +33,7 @@ def test_simple(capsys, temp_workdir):

for file in expected_files:
assert os.path.isfile(os.path.join(os.getcwd(), file))

# Check if task id is correctly set
with open(os.path.join(os.getcwd(), 'config.yml')) as config_file:
config_file_data = config_file.read()
Expand Down
30 changes: 27 additions & 3 deletions tests/commands/init/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,32 @@
from sinol_make.commands.init import Command


def test_if_download_successful():
def copy_template(rootdir):
template_path = [rootdir, Command.DEFAULT_SUBDIR]
command = Command()
with tempfile.TemporaryDirectory() as tmpdir:
tmp_dir = command.download_template(tmpdir)
assert os.path.isfile(os.path.join(tmp_dir,'config.yml'))
tmp_dir = command.download_template(tmpdir, template_path)
assert os.path.isfile(os.path.join(tmp_dir, 'config.yml'))


def test_clones_default_template(request):
# try to avoid connecting to github when cloning example_package
git_dir = os.path.join(request.config.rootdir, '.git')
if os.path.exists(git_dir):
git_local_url = os.path.join('file://', request.config.rootdir)
copy_template(git_local_url)
else:
# currently this does fallback on github if the local repo is not available
# if needed we could take a dependency on gitpython and mock up a repo
copy_template(Command.DEFAULT_TEMPLATE)


def test_copies_local_template_absolute_path(request):
rootdir_absolute = str(request.config.rootdir)
copy_template(rootdir_absolute)


def test_copies_local_template_relative_path(request):
os.chdir(os.path.join(request.config.rootdir, '..'))
rootdir_relative = os.path.relpath(request.config.rootdir, os.getcwd())
copy_template(rootdir_relative)

0 comments on commit e91a376

Please sign in to comment.