2018-07-11 08:50:42 -04:00
|
|
|
"""Test clone"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
2023-07-10 10:14:33 -04:00
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
BOOTSTRAP_CODE = 123
|
2023-07-10 15:43:17 -04:00
|
|
|
BOOTSTRAP_MSG = "Bootstrap successful"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("remote")
|
2018-07-11 08:50:42 -04:00
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"good_remote, repo_exists, force, conflicts",
|
|
|
|
[
|
2018-07-11 08:50:42 -04:00
|
|
|
(False, False, False, False),
|
|
|
|
(True, False, False, False),
|
|
|
|
(True, True, False, False),
|
|
|
|
(True, True, True, False),
|
|
|
|
(True, False, False, True),
|
2023-07-10 15:43:17 -04:00
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"bad remote",
|
|
|
|
"simple",
|
|
|
|
"existing repo",
|
|
|
|
"-f",
|
|
|
|
"conflicts",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_clone(runner, paths, yadm_cmd, repo_config, ds1, good_remote, repo_exists, force, conflicts):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""Test basic clone operation"""
|
|
|
|
|
2019-10-08 09:40:33 -04:00
|
|
|
# clear out the work path
|
|
|
|
paths.work.remove()
|
|
|
|
paths.work.mkdir()
|
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
# determine remote url
|
2023-07-10 15:43:17 -04:00
|
|
|
remote_url = f"file://{paths.remote}"
|
2018-07-11 08:50:42 -04:00
|
|
|
if not good_remote:
|
2023-07-10 15:43:17 -04:00
|
|
|
remote_url = "file://bad_remote"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
old_repo = None
|
|
|
|
if repo_exists:
|
|
|
|
# put a repo in the way
|
|
|
|
paths.repo.mkdir()
|
2023-07-10 15:43:17 -04:00
|
|
|
old_repo = paths.repo.join("old_repo")
|
|
|
|
old_repo.write("old_repo")
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
if conflicts:
|
2023-07-10 15:43:17 -04:00
|
|
|
ds1.tracked[0].relative.write("conflict")
|
2018-07-11 08:50:42 -04:00
|
|
|
assert ds1.tracked[0].relative.exists()
|
|
|
|
|
|
|
|
# run the clone command
|
2023-07-10 15:43:17 -04:00
|
|
|
args = ["clone", "-w", paths.work]
|
2018-07-11 08:50:42 -04:00
|
|
|
if force:
|
2023-07-10 15:43:17 -04:00
|
|
|
args += ["-f"]
|
2018-07-11 08:50:42 -04:00
|
|
|
args += [remote_url]
|
2020-11-17 15:38:31 -05:00
|
|
|
run = runner(command=yadm_cmd(*args))
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
if not good_remote:
|
|
|
|
# clone should fail
|
|
|
|
assert run.failure
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.out == ""
|
|
|
|
assert "Unable to clone the repository" in run.err
|
2018-07-11 08:50:42 -04:00
|
|
|
assert not paths.repo.exists()
|
|
|
|
elif repo_exists and not force:
|
|
|
|
# can't overwrite data
|
|
|
|
assert run.failure
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.out == ""
|
|
|
|
assert "Git repo already exists" in run.err
|
2018-07-11 08:50:42 -04:00
|
|
|
else:
|
|
|
|
# clone should succeed, and repo should be configured properly
|
|
|
|
assert successful_clone(run, paths, repo_config)
|
|
|
|
|
2020-11-28 12:23:46 -05:00
|
|
|
# these clones should have master as HEAD
|
2023-07-10 15:43:17 -04:00
|
|
|
verify_head(paths, "master")
|
2020-11-28 12:23:46 -05:00
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
# ensure conflicts are handled properly
|
|
|
|
if conflicts:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "NOTE" in run.out
|
|
|
|
assert "Local files with content that differs" in run.out
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# confirm correct Git origin
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=("git", "remote", "-v", "show"), env={"GIT_DIR": paths.repo})
|
2018-07-11 08:50:42 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert f"origin\t{remote_url}" in run.out
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# ensure conflicts are really preserved
|
|
|
|
if conflicts:
|
2021-01-01 18:24:45 -05:00
|
|
|
# test that the conflicts are preserved in the work tree
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=yadm_cmd("status", "-uno", "--porcelain"), cwd=paths.work)
|
2018-07-11 08:50:42 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2021-01-01 18:24:45 -05:00
|
|
|
assert str(ds1.tracked[0].path) in run.out
|
2018-07-11 08:50:42 -04:00
|
|
|
|
2021-01-01 18:24:45 -05:00
|
|
|
# verify content of the conflicts
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=yadm_cmd("diff"), cwd=paths.work)
|
2018-07-11 08:50:42 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert "\n+conflict" in run.out, "conflict overwritten"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# another force-related assertion
|
|
|
|
if old_repo:
|
|
|
|
if force:
|
|
|
|
assert not old_repo.exists()
|
|
|
|
else:
|
|
|
|
assert old_repo.exists()
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("remote")
|
2018-07-11 08:50:42 -04:00
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"bs_exists, bs_param, answer",
|
|
|
|
[
|
|
|
|
(False, "--bootstrap", None),
|
|
|
|
(True, "--bootstrap", None),
|
|
|
|
(True, "--no-bootstrap", None),
|
|
|
|
(True, None, "n"),
|
|
|
|
(True, None, "y"),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"force, missing",
|
|
|
|
"force, existing",
|
|
|
|
"prevent",
|
|
|
|
"existing, answer n",
|
|
|
|
"existing, answer y",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_clone_bootstrap(runner, paths, yadm_cmd, repo_config, bs_exists, bs_param, answer):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""Test bootstrap clone features"""
|
|
|
|
|
|
|
|
# establish a bootstrap
|
|
|
|
create_bootstrap(paths, bs_exists)
|
|
|
|
|
|
|
|
# run the clone command
|
2023-07-10 15:43:17 -04:00
|
|
|
args = ["clone", "-w", paths.work]
|
2018-07-11 08:50:42 -04:00
|
|
|
if bs_param:
|
|
|
|
args += [bs_param]
|
2023-07-10 15:43:17 -04:00
|
|
|
args += [f"file://{paths.remote}"]
|
2018-07-11 08:50:42 -04:00
|
|
|
expect = []
|
|
|
|
if answer:
|
2023-07-10 15:43:17 -04:00
|
|
|
expect.append(("Would you like to execute it now", answer))
|
2020-11-17 15:38:31 -05:00
|
|
|
run = runner(command=yadm_cmd(*args), expect=expect)
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
if answer:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "Would you like to execute it now" in run.out
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
expected_code = 0
|
2023-07-10 15:43:17 -04:00
|
|
|
if bs_exists and bs_param != "--no-bootstrap":
|
2018-07-11 08:50:42 -04:00
|
|
|
expected_code = BOOTSTRAP_CODE
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
if answer == "y":
|
2018-07-11 08:50:42 -04:00
|
|
|
expected_code = BOOTSTRAP_CODE
|
|
|
|
assert BOOTSTRAP_MSG in run.out
|
2023-07-10 15:43:17 -04:00
|
|
|
elif answer == "n":
|
2018-07-11 08:50:42 -04:00
|
|
|
expected_code = 0
|
|
|
|
assert BOOTSTRAP_MSG not in run.out
|
|
|
|
|
|
|
|
assert successful_clone(run, paths, repo_config, expected_code)
|
2023-07-10 15:43:17 -04:00
|
|
|
verify_head(paths, "master")
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
if not bs_exists:
|
|
|
|
assert BOOTSTRAP_MSG not in run.out
|
|
|
|
|
|
|
|
|
|
|
|
def create_bootstrap(paths, exists):
|
|
|
|
"""Create bootstrap file for test"""
|
|
|
|
if exists:
|
2023-07-10 15:43:17 -04:00
|
|
|
paths.bootstrap.write("#!/bin/sh\n" f"echo {BOOTSTRAP_MSG}\n" f"exit {BOOTSTRAP_CODE}\n")
|
2018-07-11 08:50:42 -04:00
|
|
|
paths.bootstrap.chmod(0o775)
|
|
|
|
assert paths.bootstrap.exists()
|
|
|
|
else:
|
|
|
|
assert not paths.bootstrap.exists()
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("remote")
|
2018-07-11 08:50:42 -04:00
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"private_type, in_repo, in_work",
|
|
|
|
[
|
|
|
|
("ssh", False, True),
|
|
|
|
("gnupg", False, True),
|
|
|
|
("ssh", True, True),
|
|
|
|
("gnupg", True, True),
|
|
|
|
("ssh", True, False),
|
|
|
|
("gnupg", True, False),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"open ssh, not tracked",
|
|
|
|
"open gnupg, not tracked",
|
|
|
|
"open ssh, tracked",
|
|
|
|
"open gnupg, tracked",
|
|
|
|
"missing ssh, tracked",
|
|
|
|
"missing gnupg, tracked",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_clone_perms(runner, yadm_cmd, paths, repo_config, private_type, in_repo, in_work):
|
2018-07-11 08:50:42 -04:00
|
|
|
"""Test clone permission-related functions"""
|
|
|
|
|
|
|
|
# update remote repo to include private data
|
|
|
|
if in_repo:
|
2023-07-10 15:43:17 -04:00
|
|
|
rpath = paths.work.mkdir(f".{private_type}").join("related")
|
|
|
|
rpath.write("related")
|
2018-07-11 08:50:42 -04:00
|
|
|
os.system(f'GIT_DIR="{paths.remote}" git add {rpath}')
|
|
|
|
os.system(f'GIT_DIR="{paths.remote}" git commit -m "{rpath}"')
|
|
|
|
rpath.remove()
|
|
|
|
|
|
|
|
# ensure local private data is insecure at the start
|
|
|
|
if in_work:
|
2023-07-10 15:43:17 -04:00
|
|
|
pdir = paths.work.join(f".{private_type}")
|
2018-07-11 08:50:42 -04:00
|
|
|
if not pdir.exists():
|
|
|
|
pdir.mkdir()
|
2023-07-10 15:43:17 -04:00
|
|
|
pfile = pdir.join("existing")
|
|
|
|
pfile.write("existing")
|
2018-07-11 08:50:42 -04:00
|
|
|
pdir.chmod(0o777)
|
|
|
|
pfile.chmod(0o777)
|
|
|
|
else:
|
|
|
|
paths.work.remove()
|
|
|
|
paths.work.mkdir()
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
env = {"HOME": paths.work}
|
|
|
|
run = runner(yadm_cmd("clone", "-d", "-w", paths.work, f"file://{paths.remote}"), env=env)
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
assert successful_clone(run, paths, repo_config)
|
2023-07-10 15:43:17 -04:00
|
|
|
verify_head(paths, "master")
|
2018-07-11 08:50:42 -04:00
|
|
|
if in_work:
|
|
|
|
# private directories which already exist, should be left as they are,
|
|
|
|
# which in this test is "insecure".
|
2023-07-10 15:43:17 -04:00
|
|
|
assert re.search(f"initial private dir perms drwxrwxrwx.+.{private_type}", run.out)
|
|
|
|
assert re.search(f"pre-checkout private dir perms drwxrwxrwx.+.{private_type}", run.out)
|
|
|
|
assert re.search(f"post-checkout private dir perms drwxrwxrwx.+.{private_type}", run.out)
|
2018-07-11 08:50:42 -04:00
|
|
|
else:
|
|
|
|
# private directories which are created, should be done prior to
|
2021-01-01 18:24:45 -05:00
|
|
|
# checkout, and with secure permissions.
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "initial private dir perms" not in run.out
|
|
|
|
assert re.search(f"pre-checkout private dir perms drwx------.+.{private_type}", run.out)
|
|
|
|
assert re.search(f"post-checkout private dir perms drwx------.+.{private_type}", run.out)
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
# standard perms still apply afterwards unless disabled with auto.perms
|
2023-07-10 15:43:17 -04:00
|
|
|
assert oct(paths.work.join(f".{private_type}").stat().mode).endswith(
|
|
|
|
"00"
|
|
|
|
), f".{private_type} has not been secured by auto.perms"
|
2018-07-11 08:50:42 -04:00
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.usefixtures("remote")
|
|
|
|
@pytest.mark.parametrize("branch", ["master", "default", "valid", "invalid"])
|
2020-11-17 15:38:31 -05:00
|
|
|
def test_alternate_branch(runner, paths, yadm_cmd, repo_config, branch):
|
2019-10-07 09:36:32 -04:00
|
|
|
"""Test cloning a branch other than master"""
|
|
|
|
|
|
|
|
# add a "valid" branch to the remote
|
|
|
|
os.system(f'GIT_DIR="{paths.remote}" git checkout -b valid')
|
2023-07-10 15:43:17 -04:00
|
|
|
os.system(f'GIT_DIR="{paths.remote}" git commit ' f'--allow-empty -m "This branch is valid"')
|
|
|
|
if branch != "default":
|
2020-11-28 12:23:46 -05:00
|
|
|
# When branch == 'default', the "default" branch of the remote repo
|
|
|
|
# will remain "valid" to validate identification the correct default
|
|
|
|
# branch by inspecting the repo. Otherwise it will be set back to
|
|
|
|
# "master"
|
|
|
|
os.system(f'GIT_DIR="{paths.remote}" git checkout master')
|
2019-10-07 09:36:32 -04:00
|
|
|
|
|
|
|
# clear out the work path
|
|
|
|
paths.work.remove()
|
|
|
|
paths.work.mkdir()
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
remote_url = f"file://{paths.remote}"
|
2019-10-07 09:36:32 -04:00
|
|
|
|
|
|
|
# run the clone command
|
2023-07-10 15:43:17 -04:00
|
|
|
args = ["clone", "-w", paths.work]
|
|
|
|
if branch not in ["master", "default"]:
|
|
|
|
args += ["-b", branch]
|
2019-10-07 09:36:32 -04:00
|
|
|
args += [remote_url]
|
2020-11-17 15:38:31 -05:00
|
|
|
run = runner(command=yadm_cmd(*args))
|
2019-10-07 09:36:32 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
if branch == "invalid":
|
2019-10-07 09:36:32 -04:00
|
|
|
assert run.failure
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "ERROR: Unable to clone the repository" in run.err
|
2021-01-01 18:24:45 -05:00
|
|
|
assert f"Remote branch {branch} not found in upstream" in run.err
|
2019-10-07 09:36:32 -04:00
|
|
|
else:
|
|
|
|
assert successful_clone(run, paths, repo_config)
|
|
|
|
|
|
|
|
# confirm correct Git origin
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=("git", "remote", "-v", "show"), env={"GIT_DIR": paths.repo})
|
2019-10-07 09:36:32 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
assert f"origin\t{remote_url}" in run.out
|
|
|
|
run = runner(command=yadm_cmd("show"))
|
|
|
|
if branch == "master":
|
|
|
|
assert "Initial commit" in run.out
|
|
|
|
verify_head(paths, "master")
|
2020-11-28 12:23:46 -05:00
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "This branch is valid" in run.out
|
|
|
|
verify_head(paths, "valid")
|
2019-10-07 09:36:32 -04:00
|
|
|
|
|
|
|
|
2018-07-11 08:50:42 -04:00
|
|
|
def successful_clone(run, paths, repo_config, expected_code=0):
|
|
|
|
"""Assert clone is successful"""
|
|
|
|
assert run.code == expected_code
|
2023-07-10 15:43:17 -04:00
|
|
|
assert oct(paths.repo.stat().mode).endswith("00"), "Repo is not secured"
|
|
|
|
assert repo_config("core.bare") == "false"
|
|
|
|
assert repo_config("status.showUntrackedFiles") == "no"
|
|
|
|
assert repo_config("yadm.managed") == "true"
|
2018-07-11 08:50:42 -04:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def remote(paths, ds1_repo_copy):
|
|
|
|
"""Function scoped remote (based on ds1)"""
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
# This is ignored because
|
|
|
|
# @pytest.mark.usefixtures('ds1_remote_copy')
|
|
|
|
# cannot be applied to another fixture.
|
|
|
|
paths.remote.remove()
|
|
|
|
paths.repo.move(paths.remote)
|
2020-11-28 12:23:46 -05:00
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
def test_no_repo(
|
|
|
|
runner,
|
|
|
|
yadm_cmd,
|
|
|
|
):
|
2020-11-28 12:23:46 -05:00
|
|
|
"""Test cloning without specifying a repo"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=yadm_cmd("clone", "-f"))
|
2020-11-28 12:23:46 -05:00
|
|
|
assert run.failure
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.out == ""
|
|
|
|
assert "ERROR: Unable to clone the repository" in run.err
|
|
|
|
assert "repository 'repo.git' does not exist" in run.err
|
2020-11-28 12:23:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def verify_head(paths, branch):
|
|
|
|
"""Assert the local repo has the correct head branch"""
|
2023-07-10 15:43:17 -04:00
|
|
|
assert paths.repo.join("HEAD").read() == f"ref: refs/heads/{branch}\n"
|
2024-07-20 17:58:42 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("remote")
|
|
|
|
def test_clone_subdirectory(runner, paths, yadm_cmd, repo_config):
|
|
|
|
"""Test clone from sub-directory of YADM_WORK"""
|
|
|
|
|
|
|
|
# clear out the work path
|
|
|
|
paths.work.remove()
|
|
|
|
paths.work.mkdir()
|
|
|
|
|
|
|
|
# create sub-directory
|
|
|
|
subdir = paths.work.mkdir("subdir")
|
|
|
|
|
|
|
|
# determine remote url
|
|
|
|
remote_url = f"file://{paths.remote}"
|
|
|
|
|
|
|
|
# run the clone command
|
2024-11-26 07:20:59 -05:00
|
|
|
args = ["clone", "-w", paths.work, remote_url]
|
2024-07-20 17:58:42 -04:00
|
|
|
run = runner(command=yadm_cmd(*args), cwd=subdir)
|
|
|
|
|
|
|
|
# clone should succeed, and repo should be configured properly
|
|
|
|
assert successful_clone(run, paths, repo_config)
|
|
|
|
|
2024-11-26 07:21:18 -05:00
|
|
|
# ensure that no changes found as this is a clean dotfiles clone
|
2024-07-20 17:58:42 -04:00
|
|
|
run = runner(command=yadm_cmd("status", "-uno", "--porcelain"), cwd=subdir)
|
|
|
|
assert run.success
|
|
|
|
assert run.out == ""
|
|
|
|
assert run.err == ""
|