From 6074ec07b3182eac20205b2fbbe1c6e0b9ff3d2b Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 24 Jun 2019 06:24:36 -0400 Subject: [PATCH] handle reimport of file within imported directory --- dotdrop/dotdrop.py | 16 ++++++- tests-ng/import-subfile.sh | 89 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100755 tests-ng/import-subfile.sh diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index 6253d3f..8846694 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -353,7 +353,21 @@ def cmd_importer(o): # prepare hierarchy for dotfile srcf = os.path.join(o.dotpath, src) - if not os.path.exists(srcf): + overwrite = not os.path.exists(srcf) + if o.safe and os.path.exists(srcf): + c = Comparator(debug=o.debug) + diff = c.compare(srcf, dst) + if diff != '': + # files are different, dunno what to do + LOG.log('diff \"{}\" VS \"{}\"'.format(dst, srcf)) + LOG.emph(diff) + # ask user + msg = 'Dotfile \"{}\" already exists, overwrite?'.format(srcf) + overwrite = LOG.ask(msg) + + if o.debug: + LOG.dbg('will overwrite: {}'.format(overwrite)) + if overwrite: cmd = ['mkdir', '-p', '{}'.format(os.path.dirname(srcf))] if o.dry: LOG.dry('would run: {}'.format(' '.join(cmd))) diff --git a/tests-ng/import-subfile.sh b/tests-ng/import-subfile.sh new file mode 100755 index 0000000..5b466e8 --- /dev/null +++ b/tests-ng/import-subfile.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2019, deadc0de6 +# +# test import file in directory +# after having imported directory +# + +# exit on first error +set -e + +# all this crap to get current path +rl="readlink -f" +if ! ${rl} "${0}" >/dev/null 2>&1; then + rl="realpath" + + if ! hash ${rl}; then + echo "\"${rl}\" not found !" && exit 1 + fi +fi +cur=$(dirname "$(${rl} "${0}")") + +#hash dotdrop >/dev/null 2>&1 +#[ "$?" != "0" ] && echo "install dotdrop to run tests" && exit 1 + +#echo "called with ${1}" + +# dotdrop path can be pass as argument +ddpath="${cur}/../" +[ "${1}" != "" ] && ddpath="${1}" +[ ! -d ${ddpath} ] && echo "ddpath \"${ddpath}\" is not a directory" && exit 1 + +export PYTHONPATH="${ddpath}:${PYTHONPATH}" +bin="python3 -m dotdrop.dotdrop" + +echo "dotdrop path: ${ddpath}" +echo "pythonpath: ${PYTHONPATH}" + +# get the helpers +source ${cur}/helpers + +echo -e "\e[96m\e[1m==> RUNNING $(basename $BASH_SOURCE) <==\e[0m" + +################################################################ +# this is the test +################################################################ + +# the dotfile source +tmps=`mktemp -d --suffix='-dotdrop-tests'` +mkdir -p ${tmps}/dotfiles +# the dotfile destination +tmpd=`mktemp -d --suffix='-dotdrop-tests'` +#echo "dotfile destination: ${tmpd}" + +# create the dotfile +mkdir -p ${tmpd}/adir +echo "first" > ${tmpd}/adir/file1 + +# create the config file +cfg="${tmps}/config.yaml" + +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: +profiles: +_EOF +#cat ${cfg} + +# import dir +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 -V ${tmpd}/adir + +# change the file +echo "second" > ${tmpd}/adir/file1 + +# import file +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 -V ${tmpd}/adir/file1 + +# test +[ ! -e ${tmps}/dotfiles/${tmpd}/adir/file1 ] && echo "not exist" && exit 1 +grep 'second' ${tmps}/dotfiles/${tmpd}/adir/file1 + +## CLEANING +rm -rf ${tmps} ${tmpd} + +echo "OK" +exit 0