2019-10-22 18:47:41 -04:00
|
|
|
"""Unit tests: upgrade"""
|
2024-11-11 16:30:41 -05:00
|
|
|
|
2019-10-22 18:47:41 -04:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.parametrize("condition", ["override", "equal", "existing_repo"])
|
2019-10-22 18:47:41 -04:00
|
|
|
def test_upgrade_errors(tmpdir, runner, yadm, condition):
|
|
|
|
"""Test upgrade() error conditions"""
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
home = tmpdir.mkdir("home")
|
|
|
|
yadm_dir = home.join(".config/yadm")
|
|
|
|
yadm_data = home.join(".local/share/yadm")
|
|
|
|
override = ""
|
|
|
|
if condition == "override":
|
|
|
|
override = "override"
|
|
|
|
if condition == "equal":
|
2020-11-18 00:01:45 -05:00
|
|
|
yadm_data = yadm_dir
|
2023-07-10 15:43:17 -04:00
|
|
|
if condition == "existing_repo":
|
|
|
|
yadm_dir.ensure_dir("repo.git")
|
|
|
|
yadm_data.ensure_dir("repo.git")
|
2019-10-22 18:47:41 -04:00
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
|
|
|
YADM_DIR="{yadm_dir}"
|
2020-11-18 00:01:45 -05:00
|
|
|
YADM_DATA="{yadm_data}"
|
|
|
|
YADM_REPO="{yadm_data}/repo.git"
|
|
|
|
YADM_LEGACY_ARCHIVE="files.gpg"
|
|
|
|
YADM_OVERRIDE_REPO="{override}"
|
2019-10-22 18:47:41 -04:00
|
|
|
upgrade
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script)
|
2019-10-22 18:47:41 -04:00
|
|
|
assert run.failure
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "Unable to upgrade" in run.err
|
|
|
|
if condition in ["override", "equal"]:
|
|
|
|
assert "Paths have been overridden" in run.err
|
|
|
|
elif condition == "existing_repo":
|
|
|
|
assert "already exists" in run.err
|
2019-10-22 18:47:41 -04:00
|
|
|
|
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
@pytest.mark.parametrize("condition", ["no-paths", "untracked", "tracked", "submodules"])
|
2019-10-22 18:47:41 -04:00
|
|
|
def test_upgrade(tmpdir, runner, yadm, condition):
|
|
|
|
"""Test upgrade()
|
|
|
|
|
|
|
|
When testing the condition of git-tracked data, "echo" will be used as a
|
|
|
|
mock for git. echo will return true, simulating a positive result from "git
|
|
|
|
ls-files". Also echo will report the parameters for "git mv".
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
legacy_paths = ("config", "encrypt", "bootstrap", "hooks/pre_cmd")
|
|
|
|
home = tmpdir.mkdir("home")
|
|
|
|
yadm_dir = home.join(".config/yadm")
|
|
|
|
yadm_data = home.join(".local/share/yadm")
|
|
|
|
yadm_legacy = home.join(".yadm")
|
2019-10-22 18:47:41 -04:00
|
|
|
|
2023-07-10 15:43:17 -04:00
|
|
|
if condition != "no-paths":
|
|
|
|
yadm_dir.join("repo.git/config").write("test-repo", ensure=True)
|
|
|
|
yadm_dir.join("files.gpg").write("files.gpg", ensure=True)
|
2020-12-29 12:10:18 -05:00
|
|
|
for path in legacy_paths:
|
|
|
|
yadm_legacy.join(path).write(path, ensure=True)
|
2019-10-22 18:47:41 -04:00
|
|
|
|
2019-11-07 08:48:42 -05:00
|
|
|
mock_git = ""
|
2023-07-10 15:43:17 -04:00
|
|
|
if condition != "no-paths":
|
|
|
|
mock_git = f"""
|
2019-11-07 08:48:42 -05:00
|
|
|
function git() {{
|
|
|
|
echo "$@"
|
2021-01-03 18:33:38 -05:00
|
|
|
if [[ "$*" = *"submodule status" ]]; then
|
2024-11-11 16:30:41 -05:00
|
|
|
{'echo " 1234567 mymodule (1.0)"' if condition == 'submodules' else ':'}
|
2021-01-03 18:33:38 -05:00
|
|
|
fi
|
|
|
|
if [[ "$*" = *ls-files* ]]; then
|
2024-11-11 16:30:41 -05:00
|
|
|
return {1 if condition == 'untracked' else 0}
|
2019-11-07 08:48:42 -05:00
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}}
|
2023-07-10 15:43:17 -04:00
|
|
|
"""
|
2019-10-22 18:47:41 -04:00
|
|
|
|
|
|
|
script = f"""
|
|
|
|
YADM_TEST=1 source {yadm}
|
2020-12-29 12:10:18 -05:00
|
|
|
YADM_LEGACY_DIR="{yadm_legacy}"
|
2019-10-22 18:47:41 -04:00
|
|
|
YADM_DIR="{yadm_dir}"
|
2020-11-18 00:01:45 -05:00
|
|
|
YADM_DATA="{yadm_data}"
|
|
|
|
YADM_REPO="{yadm_data}/repo.git"
|
|
|
|
YADM_ARCHIVE="{yadm_data}/archive"
|
2019-11-07 08:48:42 -05:00
|
|
|
GIT_PROGRAM="git"
|
|
|
|
{mock_git}
|
2019-11-05 17:36:05 -05:00
|
|
|
function cd {{ echo "$@";}}
|
2019-10-22 18:47:41 -04:00
|
|
|
upgrade
|
|
|
|
"""
|
2023-07-10 15:43:17 -04:00
|
|
|
run = runner(command=["bash"], inp=script)
|
2019-10-22 18:47:41 -04:00
|
|
|
assert run.success
|
2023-07-10 15:43:17 -04:00
|
|
|
assert run.err == ""
|
|
|
|
if condition == "no-paths":
|
|
|
|
assert "Upgrade is not necessary" in run.out
|
2019-10-22 18:47:41 -04:00
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
for lpath, npath in [("repo.git", "repo.git"), ("files.gpg", "archive")]:
|
|
|
|
expected = f"Moving {yadm_dir.join(lpath)} " f"to {yadm_data.join(npath)}"
|
2019-10-22 18:47:41 -04:00
|
|
|
assert expected in run.out
|
2020-12-29 12:10:18 -05:00
|
|
|
for path in legacy_paths:
|
2023-07-10 15:43:17 -04:00
|
|
|
expected = f"Moving {yadm_legacy.join(path)} " f"to {yadm_dir.join(path)}"
|
2020-12-29 12:10:18 -05:00
|
|
|
assert expected in run.out
|
2023-07-10 15:43:17 -04:00
|
|
|
if condition == "untracked":
|
|
|
|
assert "test-repo" in yadm_data.join("repo.git/config").read()
|
|
|
|
assert "files.gpg" in yadm_data.join("archive").read()
|
2020-12-29 12:10:18 -05:00
|
|
|
for path in legacy_paths:
|
|
|
|
assert path in yadm_dir.join(path).read()
|
2023-07-10 15:43:17 -04:00
|
|
|
elif condition in ["tracked", "submodules"]:
|
|
|
|
expected = f'mv {yadm_dir.join("files.gpg")} ' f'{yadm_data.join("archive")}'
|
2020-11-18 00:01:45 -05:00
|
|
|
assert expected in run.out
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "files tracked by yadm have been renamed" in run.out
|
|
|
|
if condition == "submodules":
|
|
|
|
assert "submodule deinit -- mymodule" in run.out
|
|
|
|
assert "submodule update --init --recursive -- mymodule" in run.out
|
2019-11-07 08:48:42 -05:00
|
|
|
else:
|
2023-07-10 15:43:17 -04:00
|
|
|
assert "submodule deinit" not in run.out
|
|
|
|
assert "submodule update --init --recursive" not in run.out
|