2020-12-30 18:41:02 -05:00
|
|
|
"""Test upgrade"""
|
|
|
|
|
|
|
|
import os
|
2023-07-10 10:14:33 -04:00
|
|
|
|
2020-12-30 18:41:02 -05:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-07-10 15:43:17 -04:00
|
|
|
"versions",
|
|
|
|
[
|
|
|
|
("1.12.0", "2.5.0"),
|
|
|
|
("1.12.0",),
|
|
|
|
("2.5.0",),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"1.12.0 -> 2.5.0 -> latest",
|
|
|
|
"1.12.0 -> latest",
|
|
|
|
"2.5.0 -> latest",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize("submodule", [False, True], ids=["no submodule", "with submodules"])
|
2020-12-30 18:41:02 -05:00
|
|
|
def test_upgrade(tmpdir, runner, versions, submodule):
|
|
|
|
"""Upgrade tests"""
|
|
|
|
# pylint: disable=too-many-statements
|
2023-07-10 15:43:17 -04:00
|
|
|
home = tmpdir.mkdir("HOME")
|
|
|
|
env = {"HOME": str(home)}
|
|
|
|
runner(["git", "config", "--global", "init.defaultBranch", "master"], env=env)
|
|
|
|
runner(["git", "config", "--global", "protocol.file.allow", "always"], env=env)
|
2020-12-30 18:41:02 -05:00
|
|
|
|
|
|
|
if submodule:
|
2023-07-10 15:43:17 -04:00
|
|
|
ext_repo = tmpdir.mkdir("ext_repo")
|
|
|
|
ext_repo.join("afile").write("some data")
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
for cmd in (("init",), ("add", "afile"), ("commit", "-m", "test")):
|
|
|
|
run = runner(["git", "-C", str(ext_repo), *cmd])
|
2020-12-30 18:41:02 -05:00
|
|
|
assert run.success
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
os.environ.pop("XDG_CONFIG_HOME", None)
|
|
|
|
os.environ.pop("XDG_DATA_HOME", None)
|
2020-12-30 18:41:02 -05:00
|
|
|
|
|
|
|
def run_version(version, *args, check_stderr=True):
|
2023-07-10 15:43:17 -04:00
|
|
|
yadm = f"yadm-{version}" if version else "/yadm/yadm"
|
2020-12-30 18:41:02 -05:00
|
|
|
run = runner([yadm, *args], shell=True, cwd=str(home), env=env)
|
|
|
|
assert run.success
|
|
|
|
if check_stderr:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
2020-12-30 18:41:02 -05:00
|
|
|
return run
|
|
|
|
|
|
|
|
# Initialize the repo with the first version
|
|
|
|
first = versions[0]
|
2023-07-10 15:43:17 -04:00
|
|
|
run_version(first, "init")
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
home.join("file").write("some data")
|
|
|
|
run_version(first, "add", "file")
|
|
|
|
run_version(first, "commit", "-m", '"First commit"')
|
2020-12-30 18:41:02 -05:00
|
|
|
|
|
|
|
if submodule:
|
|
|
|
# When upgrading via 2.5.0 we can't have a submodule that's been added
|
|
|
|
# after being cloned as 2.5.0 fails the upgrade in that case.
|
2023-07-10 15:43:17 -04:00
|
|
|
can_upgrade_cloned_submodule = "2.5.0" not in versions[1:]
|
2021-01-17 13:07:36 -05:00
|
|
|
if can_upgrade_cloned_submodule:
|
2020-12-30 18:41:02 -05:00
|
|
|
# Check out a repo and then add it as a submodule
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(["git", "-C", str(home), "clone", str(ext_repo), "b"])
|
2020-12-30 18:41:02 -05:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
run_version(first, "submodule", "add", str(ext_repo), "b")
|
2020-12-30 18:41:02 -05:00
|
|
|
|
|
|
|
# Add submodule without first checking it out
|
2023-07-10 15:43:17 -04:00
|
|
|
run_version(first, "submodule", "add", str(ext_repo), "a", check_stderr=False)
|
|
|
|
run_version(first, "submodule", "add", str(ext_repo), "c", check_stderr=False)
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
run_version(first, "commit", "-m", '"Add submodules"')
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
for path in (".yadm", ".config/yadm"):
|
2020-12-30 18:41:02 -05:00
|
|
|
yadm_dir = home.join(path)
|
|
|
|
if yadm_dir.exists():
|
|
|
|
break
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
yadm_dir.join("bootstrap").write("init stuff")
|
|
|
|
run_version(first, "add", yadm_dir.join("bootstrap"))
|
|
|
|
run_version(first, "commit", "-m", "bootstrap")
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
yadm_dir.join("encrypt").write("secret")
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
hooks_dir = yadm_dir.mkdir("hooks")
|
|
|
|
hooks_dir.join("pre_status").write("status")
|
|
|
|
hooks_dir.join("post_commit").write("commit")
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
run_version(first, "config", "local.class", "test")
|
|
|
|
run_version(first, "config", "foo.bar", "true")
|
2020-12-30 18:41:02 -05:00
|
|
|
|
|
|
|
# Run upgrade with intermediate versions and latest
|
|
|
|
latest = None
|
|
|
|
for version in versions[1:] + (latest,):
|
2023-07-10 15:43:17 -04:00
|
|
|
run = run_version(version, "upgrade", check_stderr=not submodule)
|
2020-12-30 18:41:02 -05:00
|
|
|
if submodule:
|
|
|
|
lines = run.err.splitlines()
|
2021-01-17 13:07:36 -05:00
|
|
|
if can_upgrade_cloned_submodule:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "Migrating git directory of" in lines[0]
|
|
|
|
assert str(home.join("b/.git")) in lines[1]
|
|
|
|
assert str(yadm_dir.join("repo.git/modules/b")) in lines[2]
|
2020-12-30 18:41:02 -05:00
|
|
|
del lines[:3]
|
|
|
|
for line in lines:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert line.startswith("Submodule")
|
|
|
|
assert "registered for path" in line
|
2020-12-30 18:41:02 -05:00
|
|
|
|
|
|
|
# Verify result for the final upgrade
|
2023-07-10 15:43:17 -04:00
|
|
|
run_version(latest, "status")
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
run = run_version(latest, "show", "HEAD:file")
|
|
|
|
assert run.out == "some data"
|
2020-12-30 18:41:02 -05:00
|
|
|
|
|
|
|
if submodule:
|
2021-01-17 13:07:36 -05:00
|
|
|
if can_upgrade_cloned_submodule:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert home.join("b/afile").read() == "some data"
|
|
|
|
assert home.join("a/afile").read() == "some data"
|
|
|
|
assert home.join("c/afile").read() == "some data"
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
yadm_dir = home.join(".config/yadm")
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
assert yadm_dir.join("bootstrap").read() == "init stuff"
|
|
|
|
assert yadm_dir.join("encrypt").read() == "secret"
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
hooks_dir = yadm_dir.join("hooks")
|
|
|
|
assert hooks_dir.join("pre_status").read() == "status"
|
|
|
|
assert hooks_dir.join("post_commit").read() == "commit"
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
run = run_version(latest, "config", "local.class")
|
|
|
|
assert run.out.rstrip() == "test"
|
2020-12-30 18:41:02 -05:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
run = run_version(latest, "config", "foo.bar")
|
|
|
|
assert run.out.rstrip() == "true"
|