Update upgrade

Upgrade will only move 2.0.0 paths to 3.0.0 standards
This commit is contained in:
Tim Byrne 2020-11-17 23:01:45 -06:00
parent efe8355659
commit 7997dc9a3d
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
4 changed files with 86 additions and 107 deletions

View File

@ -6,36 +6,38 @@ import pytest
'legacy_path', [ 'legacy_path', [
None, None,
'repo.git', 'repo.git',
'config',
'encrypt',
'files.gpg', 'files.gpg',
'bootstrap',
'hooks/pre_command',
'hooks/post_command',
], ],
) )
@pytest.mark.parametrize(
'override', [True, False], ids=['override', 'no-override'])
@pytest.mark.parametrize( @pytest.mark.parametrize(
'upgrade', [True, False], ids=['upgrade', 'no-upgrade']) 'upgrade', [True, False], ids=['upgrade', 'no-upgrade'])
def test_legacy_warning(tmpdir, runner, yadm, upgrade, legacy_path): def test_legacy_warning(tmpdir, runner, yadm, upgrade, override, legacy_path):
"""Use issue_legacy_path_warning""" """Use issue_legacy_path_warning"""
home = tmpdir.mkdir('home') home = tmpdir.mkdir('home')
if legacy_path: if legacy_path:
home.mkdir(f'.yadm').ensure(legacy_path) home.ensure(f'.config/yadm/{str(legacy_path)}')
override = 'YADM_OVERRIDE_REPO=override' if override else ''
main_args = 'MAIN_ARGS=("upgrade")' if upgrade else '' main_args = 'MAIN_ARGS=("upgrade")' if upgrade else ''
script = f""" script = f"""
XDG_CONFIG_HOME=
XDG_DATA_HOME=
HOME={home} HOME={home}
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
{main_args} {main_args}
{override}
set_yadm_dirs
issue_legacy_path_warning issue_legacy_path_warning
echo "LWI:$LEGACY_WARNING_ISSUED" echo "LWI:$LEGACY_WARNING_ISSUED"
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)
assert run.success assert run.success
assert run.err == '' assert run.err == ''
if legacy_path and not upgrade: if legacy_path and (not upgrade) and (not override):
assert 'Legacy configuration paths have been detected' in run.out assert 'Legacy paths have been detected' in run.out
assert 'LWI:1' in run.out assert 'LWI:1' in run.out
else: else:
assert run.out.rstrip() == 'LWI:0' assert run.out.rstrip() == 'LWI:0'

View File

@ -1,45 +1,38 @@
"""Unit tests: upgrade""" """Unit tests: upgrade"""
import pytest import pytest
LEGACY_PATHS = [
'config',
'encrypt',
'files.gpg',
'bootstrap',
'hooks/pre_command',
'hooks/post_command',
]
# used: @pytest.mark.parametrize('condition', ['override', 'equal', 'existing_repo'])
# YADM_DIR
# YADM_LEGACY_DIR
# GIT_PROGRAM
@pytest.mark.parametrize('condition', ['equal', 'existing_repo'])
def test_upgrade_errors(tmpdir, runner, yadm, condition): def test_upgrade_errors(tmpdir, runner, yadm, condition):
"""Test upgrade() error conditions""" """Test upgrade() error conditions"""
home = tmpdir.mkdir('home') home = tmpdir.mkdir('home')
yadm_dir = home.join('.config/yadm') yadm_dir = home.join('.config/yadm')
legacy_dir = home.join('.yadm') yadm_data = home.join('.local/share/yadm')
override = ''
if condition == 'override':
override = 'override'
if condition == 'equal': if condition == 'equal':
legacy_dir = yadm_dir yadm_data = yadm_dir
if condition == 'existing_repo': if condition == 'existing_repo':
yadm_dir.ensure_dir('repo.git') yadm_dir.ensure_dir('repo.git')
legacy_dir.ensure_dir('repo.git') yadm_data.ensure_dir('repo.git')
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
YADM_DIR="{yadm_dir}" YADM_DIR="{yadm_dir}"
YADM_REPO="{yadm_dir}/repo.git" YADM_DATA="{yadm_data}"
YADM_LEGACY_DIR="{legacy_dir}" YADM_REPO="{yadm_data}/repo.git"
YADM_LEGACY_ARCHIVE="files.gpg"
YADM_OVERRIDE_REPO="{override}"
upgrade upgrade
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)
assert run.failure assert run.failure
assert run.err == '' assert run.err == ''
assert 'Unable to upgrade' in run.out assert 'Unable to upgrade' in run.out
if condition == 'equal': if condition in ['override', 'equal']:
assert 'has been resolved as' in run.out assert 'Paths have been overridden' in run.out
if condition == 'existing_repo': if condition == 'existing_repo':
assert 'already exists' in run.out assert 'already exists' in run.out
@ -55,12 +48,11 @@ def test_upgrade(tmpdir, runner, yadm, condition):
""" """
home = tmpdir.mkdir('home') home = tmpdir.mkdir('home')
yadm_dir = home.join('.config/yadm') yadm_dir = home.join('.config/yadm')
legacy_dir = home.join('.yadm') yadm_data = home.join('.local/share/yadm')
if condition != 'no-paths': if condition != 'no-paths':
legacy_dir.join('repo.git/config').write('test-repo', ensure=True) yadm_dir.join('repo.git/config').write('test-repo', ensure=True)
for lpath in LEGACY_PATHS: yadm_dir.join('files.gpg').write('files.gpg', ensure=True)
legacy_dir.join(lpath).write(lpath, ensure=True)
mock_git = "" mock_git = ""
if condition in ['tracked', 'submodules']: if condition in ['tracked', 'submodules']:
@ -77,8 +69,9 @@ def test_upgrade(tmpdir, runner, yadm, condition):
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
YADM_DIR="{yadm_dir}" YADM_DIR="{yadm_dir}"
YADM_REPO="{yadm_dir}/repo.git" YADM_DATA="{yadm_data}"
YADM_LEGACY_DIR="{legacy_dir}" YADM_REPO="{yadm_data}/repo.git"
YADM_ARCHIVE="{yadm_data}/archive"
GIT_PROGRAM="git" GIT_PROGRAM="git"
{mock_git} {mock_git}
function cd {{ echo "$@";}} function cd {{ echo "$@";}}
@ -90,21 +83,20 @@ def test_upgrade(tmpdir, runner, yadm, condition):
if condition == 'no-paths': if condition == 'no-paths':
assert 'Upgrade is not necessary' in run.out assert 'Upgrade is not necessary' in run.out
else: else:
for lpath in LEGACY_PATHS + ['repo.git']: for (lpath, npath) in [
('repo.git', 'repo.git'), ('files.gpg', 'archive')]:
expected = ( expected = (
f'Moving {legacy_dir.join(lpath)} ' f'Moving {yadm_dir.join(lpath)} '
f'to {yadm_dir.join(lpath)}') f'to {yadm_data.join(npath)}')
assert expected in run.out assert expected in run.out
if condition == 'untracked': if condition == 'untracked':
assert 'test-repo' in yadm_dir.join('repo.git/config').read() assert 'test-repo' in yadm_data.join('repo.git/config').read()
for lpath in LEGACY_PATHS: assert 'files.gpg' in yadm_data.join('archive').read()
assert lpath in yadm_dir.join(lpath).read()
elif condition in ['tracked', 'submodules']: elif condition in ['tracked', 'submodules']:
for lpath in LEGACY_PATHS: expected = (
expected = ( f'mv {yadm_dir.join("files.gpg")} '
f'mv {legacy_dir.join(lpath)} ' f'{yadm_data.join("archive")}')
f'{yadm_dir.join(lpath)}') assert expected in run.out
assert expected in run.out
assert 'files tracked by yadm have been renamed' in run.out assert 'files tracked by yadm have been renamed' in run.out
if condition == 'submodules': if condition == 'submodules':
assert 'submodule deinit -f .' in run.out assert 'submodule deinit -f .' in run.out

95
yadm
View File

@ -25,7 +25,7 @@ VERSION=2.5.0
YADM_WORK="$HOME" YADM_WORK="$HOME"
YADM_DIR= YADM_DIR=
YADM_DATA= YADM_DATA=
YADM_LEGACY_DIR="${HOME}/.yadm" YADM_LEGACY_ARCHIVE="files.gpg"
# these are the default paths relative to YADM_DIR # these are the default paths relative to YADM_DIR
YADM_CONFIG="config" YADM_CONFIG="config"
@ -1348,53 +1348,45 @@ function upgrade() {
local actions_performed local actions_performed
actions_performed=0 actions_performed=0
local repo_moved
repo_moved=0
local repo_updates local repo_updates
repo_updates=0 repo_updates=0
[ "$YADM_DIR" = "$YADM_LEGACY_DIR" ] && \ [[ -n "${YADM_OVERRIDE_REPO}${YADM_OVERRIDE_ARCHIVE}" || "$YADM_DATA" = "$YADM_DIR" ]] && \
error_out "Unable to upgrade. yadm dir has been resolved as '$YADM_LEGACY_DIR'." error_out "Unable to upgrade. Paths have been overridden with command line options"
# handle legacy repo # handle legacy repo
if [ -d "$YADM_LEGACY_DIR/repo.git" ]; then if [ -d "$YADM_DIR/repo.git" ]; then
# legacy repo detected, it must be moved to YADM_REPO # legacy repo detected, it must be moved to YADM_REPO
if [ -e "$YADM_REPO" ]; then if [ -e "$YADM_REPO" ]; then
error_out "Unable to upgrade. '$YADM_REPO' already exists. Refusing to overwrite it." error_out "Unable to upgrade. '$YADM_REPO' already exists. Refusing to overwrite it."
else else
actions_performed=1 actions_performed=1
echo "Moving $YADM_LEGACY_DIR/repo.git to $YADM_REPO" repo_moved=1
echo "Moving $YADM_DIR/repo.git to $YADM_REPO"
assert_parent "$YADM_REPO" assert_parent "$YADM_REPO"
mv "$YADM_LEGACY_DIR/repo.git" "$YADM_REPO" mv "$YADM_DIR/repo.git" "$YADM_REPO"
fi
fi
GIT_DIR="$YADM_REPO"
export GIT_DIR
# handle legacy archive
if [ -e "$YADM_DIR/$YADM_LEGACY_ARCHIVE" ]; then
actions_performed=1
echo "Moving $YADM_DIR/$YADM_LEGACY_ARCHIVE to $YADM_ARCHIVE"
assert_parent "$YADM_ARCHIVE"
# test to see if path is "tracked" in repo, if so 'git mv' must be used
if "$GIT_PROGRAM" ls-files --error-unmatch "$YADM_DIR/$YADM_LEGACY_ARCHIVE" &> /dev/null; then
"$GIT_PROGRAM" mv "$YADM_DIR/$YADM_LEGACY_ARCHIVE" "$YADM_ARCHIVE" && repo_updates=1
else
mv -i "$YADM_DIR/$YADM_LEGACY_ARCHIVE" "$YADM_ARCHIVE"
fi fi
fi fi
# handle other legacy paths
GIT_DIR="$YADM_REPO"
export GIT_DIR
for legacy_path in \
"$YADM_LEGACY_DIR/config" \
"$YADM_LEGACY_DIR/encrypt" \
"$YADM_LEGACY_DIR/files.gpg" \
"$YADM_LEGACY_DIR/bootstrap" \
"$YADM_LEGACY_DIR"/hooks/{pre,post}_* \
; \
do
if [ -e "$legacy_path" ]; then
new_filename=${legacy_path#$YADM_LEGACY_DIR/}
new_filename="$YADM_DIR/$new_filename"
actions_performed=1
echo "Moving $legacy_path to $new_filename"
assert_parent "$new_filename"
# test to see if path is "tracked" in repo, if so 'git mv' must be used
if "$GIT_PROGRAM" ls-files --error-unmatch "$legacy_path" &> /dev/null; then
"$GIT_PROGRAM" mv "$legacy_path" "$new_filename" && repo_updates=1
else
mv -i "$legacy_path" "$new_filename"
fi
fi
done
# handle submodules, which need to be reinitialized # handle submodules, which need to be reinitialized
if [ "$actions_performed" -ne 0 ]; then if [ "$repo_moved" -ne 0 ]; then
cd_work "Upgrade submodules" cd_work "Upgrade submodules"
if "$GIT_PROGRAM" ls-files --error-unmatch .gitmodules &> /dev/null; then if "$GIT_PROGRAM" ls-files --error-unmatch .gitmodules &> /dev/null; then
"$GIT_PROGRAM" submodule deinit -f . "$GIT_PROGRAM" submodule deinit -f .
@ -1592,24 +1584,17 @@ function issue_legacy_path_warning() {
# no warnings during upgrade # no warnings during upgrade
[[ "${MAIN_ARGS[*]}" =~ upgrade ]] && return [[ "${MAIN_ARGS[*]}" =~ upgrade ]] && return
# no warnings if YADM_DIR is resolved as the leacy path # no warnings if overrides have been provided
[ "$YADM_DIR" = "$YADM_LEGACY_DIR" ] && return [[ -n "${YADM_OVERRIDE_REPO}${YADM_OVERRIDE_ARCHIVE}" || "$YADM_DATA" = "$YADM_DIR" ]] && return
# no warnings if the legacy directory doesn't exist
[ ! -d "$YADM_LEGACY_DIR" ] && return
# test for legacy paths # test for legacy paths
local legacy_found local legacy_found
legacy_found=() legacy_found=()
# this is ordered by importance # this is ordered by importance
for legacy_path in \ for legacy_path in \
"$YADM_LEGACY_DIR/$YADM_REPO" \ "$YADM_DIR/$YADM_REPO" \
"$YADM_LEGACY_DIR/$YADM_CONFIG" \ "$YADM_DIR/$YADM_LEGACY_ARCHIVE" \
"$YADM_LEGACY_DIR/$YADM_ENCRYPT" \ ;
"$YADM_LEGACY_DIR/files.gpg" \
"$YADM_LEGACY_DIR/$YADM_BOOTSTRAP" \
"$YADM_LEGACY_DIR/$YADM_HOOKS"/{pre,post}_* \
; \
do do
[ -e "$legacy_path" ] && legacy_found+=("$legacy_path") [ -e "$legacy_path" ] && legacy_found+=("$legacy_path")
done done
@ -1624,21 +1609,21 @@ function issue_legacy_path_warning() {
cat <<EOF cat <<EOF
**WARNING** **WARNING**
Legacy configuration paths have been detected. Legacy paths have been detected.
Beginning with version 2.0.0, yadm uses the XDG Base Directory Specification With version 3.0.0, yadm uses the XDG Base Directory Specification
to find its configurations. Read more about this change here: to find its configurations and data. Read more about this change here:
https://yadm.io/docs/upgrade_from_1 https://yadm.io/docs/upgrade_from_2
In your environment, the configuration directory has been resolved to: In your environment, the data directory has been resolved to:
$YADM_DIR $YADM_DATA
To remove this warning do one of the following: To remove this warning do one of the following:
* Run "yadm upgrade" to move the yadm data to the new directory. (RECOMMENDED) * Run "yadm upgrade" to move the yadm data to the new paths. (RECOMMENDED)
* Manually move yadm configurations to the directory listed above. * Manually move yadm data to new default paths.
* Specify your preferred yadm directory with -Y each execution. * Specify your preferred paths with --yadm-data and --yadm-archive each execution.
Legacy paths detected: Legacy paths detected:
${path_list} ${path_list}

12
yadm.1
View File

@ -293,21 +293,21 @@ https://github.com/elasticdog/transcrypt
for details. for details.
.TP .TP
.B upgrade .B upgrade
Version 2 of yadm uses a different directory for storing your configurations. Version 3 of yadm uses a different directory for storing data.
When you start to use version 2 for the first time, you may see warnings about When you start to use version 3 for the first time, you may see warnings about
moving your data to this new directory. moving your data to this new directory.
The easiest way to accomplish this is by running "yadm upgrade". The easiest way to accomplish this is by running "yadm upgrade".
This command will start by moving your yadm repo to the new path. This command will start by moving your yadm repo to the new path.
Next it will move any configuration data to the new path. Next it will move any archive data.
If the configurations are tracked within your yadm repo, this command will If the archive is tracked within your yadm repo, this command will
"stage" the renaming of those files in the repo's index. "stage" the renaming of that file in the repo's index.
Upgrading will also re-initialize all submodules you have added (otherwise they Upgrading will also re-initialize all submodules you have added (otherwise they
will be broken when the repo moves). will be broken when the repo moves).
After running "yadm upgrade", you should run "yadm status" to review changes After running "yadm upgrade", you should run "yadm status" to review changes
which have been staged, and commit them to your repository. which have been staged, and commit them to your repository.
You can read You can read
https://yadm.io/docs/upgrade_from_1 https://yadm.io/docs/upgrade_from_2
for more information. for more information.
.TP .TP
.B version .B version