2018-02-12 22:18:35 +00:00
|
|
|
import os
|
2019-04-07 20:36:29 +00:00
|
|
|
import subprocess
|
2018-02-12 22:18:35 +00:00
|
|
|
from tempfile import TemporaryDirectory
|
2022-10-31 22:32:14 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2018-10-12 14:45:07 +00:00
|
|
|
from repo2docker.contentproviders import Git
|
2018-02-12 22:18:35 +00:00
|
|
|
|
|
|
|
|
2018-12-17 08:18:30 +00:00
|
|
|
def test_clone(repo_with_content):
|
2018-10-12 14:28:49 +00:00
|
|
|
"""Test simple git clone to a target dir"""
|
2018-12-17 08:18:30 +00:00
|
|
|
upstream, sha1 = repo_with_content
|
2018-02-12 22:18:35 +00:00
|
|
|
|
2018-12-17 08:18:30 +00:00
|
|
|
with TemporaryDirectory() as clone_dir:
|
2019-05-31 09:10:17 +00:00
|
|
|
spec = {"repo": upstream}
|
2018-12-17 08:18:30 +00:00
|
|
|
git_content = Git()
|
|
|
|
for _ in git_content.fetch(spec, clone_dir):
|
|
|
|
pass
|
2019-05-31 09:10:17 +00:00
|
|
|
assert os.path.exists(os.path.join(clone_dir, "test"))
|
2018-12-17 08:18:30 +00:00
|
|
|
|
|
|
|
assert git_content.content_id == sha1[:7]
|
2018-02-12 22:18:35 +00:00
|
|
|
|
2018-10-12 14:28:49 +00:00
|
|
|
|
2019-04-07 20:36:29 +00:00
|
|
|
def test_submodule_clone(repo_with_submodule):
|
|
|
|
"""Test git clone containing a git submodule."""
|
2019-04-08 13:26:44 +00:00
|
|
|
upstream, expected_sha1_upstream, expected_sha1_submod = repo_with_submodule
|
2019-04-07 20:36:29 +00:00
|
|
|
|
2019-11-05 17:01:06 +00:00
|
|
|
# check that checking out a branch where there are no submodule
|
|
|
|
# indeed doesn't get any submodule, even though they are in master
|
|
|
|
with TemporaryDirectory() as clone_dir2:
|
|
|
|
submod_dir = os.path.join(clone_dir2, "submod") # set by fixture
|
|
|
|
spec = {"repo": upstream, "ref": "branch-without-submod"}
|
|
|
|
git_content = Git()
|
|
|
|
for _ in git_content.fetch(spec, clone_dir2):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert os.path.exists(os.path.join(clone_dir2, "test"))
|
2022-10-24 12:30:08 +00:00
|
|
|
assert not os.path.exists(os.path.join(submod_dir, "requirements.txt"))
|
2019-11-05 17:01:06 +00:00
|
|
|
|
2019-04-07 20:36:29 +00:00
|
|
|
with TemporaryDirectory() as clone_dir:
|
2019-05-31 09:10:17 +00:00
|
|
|
submod_dir = os.path.join(clone_dir, "submod") # set by fixture
|
|
|
|
spec = {"repo": upstream}
|
2019-04-07 20:36:29 +00:00
|
|
|
git_content = Git()
|
|
|
|
for _ in git_content.fetch(spec, clone_dir):
|
|
|
|
pass
|
2019-05-31 09:10:17 +00:00
|
|
|
assert os.path.exists(os.path.join(clone_dir, "test"))
|
2022-10-24 12:30:08 +00:00
|
|
|
assert os.path.exists(os.path.join(submod_dir, "requirements.txt"))
|
2019-04-07 20:36:29 +00:00
|
|
|
|
|
|
|
# get current sha1 of submodule
|
2019-05-31 09:10:17 +00:00
|
|
|
cmd = ["git", "rev-parse", "HEAD"]
|
2019-04-07 20:36:29 +00:00
|
|
|
sha1 = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=submod_dir)
|
|
|
|
submod_sha1 = sha1.stdout.read().decode().strip()
|
|
|
|
|
2019-04-08 13:26:44 +00:00
|
|
|
assert git_content.content_id == expected_sha1_upstream[:7]
|
|
|
|
assert submod_sha1[:7] == expected_sha1_submod[:7]
|
2019-04-07 20:36:29 +00:00
|
|
|
|
|
|
|
|
2018-12-17 08:18:30 +00:00
|
|
|
def test_bad_ref(repo_with_content):
|
2018-12-13 07:32:50 +00:00
|
|
|
"""
|
|
|
|
Test trying to checkout a ref that doesn't exist
|
|
|
|
"""
|
2018-12-17 08:18:30 +00:00
|
|
|
upstream, sha1 = repo_with_content
|
|
|
|
with TemporaryDirectory() as clone_dir:
|
2019-05-31 09:10:17 +00:00
|
|
|
spec = {"repo": upstream, "ref": "does-not-exist"}
|
2018-12-17 08:18:30 +00:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
for _ in Git().fetch(spec, clone_dir):
|
|
|
|
pass
|
|
|
|
|
2018-10-12 14:28:49 +00:00
|
|
|
|
|
|
|
def test_always_accept():
|
|
|
|
# The git content provider should always accept a spec
|
2019-05-31 09:10:17 +00:00
|
|
|
assert Git().detect("/tmp/doesnt-exist", ref="1234")
|
|
|
|
assert Git().detect("/tmp/doesnt-exist")
|
2018-10-12 14:28:49 +00:00
|
|
|
# a path that exists
|
2019-05-31 09:10:17 +00:00
|
|
|
assert Git().detect("/etc", ref="1234")
|
2018-10-12 14:28:49 +00:00
|
|
|
# a remote URL
|
2019-05-31 09:10:17 +00:00
|
|
|
assert Git().detect("https://example.com/path/here")
|