diff --git a/dotdrop/config.py b/dotdrop/config.py index c5c2b76..09bd126 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -70,9 +70,9 @@ class Cfg: key_profiles_imp = 'import' # link values - lnk_parent = 'link' - lnk_nolink = 'nolink' - lnk_children = 'link_children' + lnk_nolink = LinkTypes.NOLINK.name.lower() + lnk_link = LinkTypes.LINK.name.lower() + lnk_children = LinkTypes.LINK_CHILDREN.name.lower() # settings defaults default_dotpath = 'dotfiles' @@ -198,22 +198,12 @@ class Cfg: def _string_to_linktype(self, string): """translate string to linktype""" - if string == self.lnk_parent.lower(): - return LinkTypes.PARENT + if string == self.lnk_link.lower(): + return LinkTypes.LINK elif string == self.lnk_children.lower(): - return LinkTypes.CHILDREN + return LinkTypes.LINK_CHILDREN return LinkTypes.NOLINK - def _linktype_to_string(self, link): - """translate linktype to string""" - if link == LinkTypes.PARENT: - return self.lnk_parent - elif link == LinkTypes.CHILDREN: - return self.lnk_children - elif link == LinkTypes.NOLINK: - return self.lnk_nolink - return self.lnk_nolink - def _parse(self, profile=None): """parse config file""" # parse the settings @@ -602,7 +592,7 @@ class Cfg: self.lnk_settings[self.key_dotfile_link] = self.default_link else: key = self.lnk_settings[self.key_dotfile_link] - if key != self.lnk_parent and \ + if key != self.lnk_link and \ key != self.lnk_children and \ key != self.lnk_nolink: self.log.err('bad value for {}'.format(self.key_dotfile_link)) @@ -612,7 +602,7 @@ class Cfg: self.lnk_settings[self.key_imp_link] = self.default_link_imp else: key = self.lnk_settings[self.key_imp_link] - if key != self.lnk_parent and \ + if key != self.lnk_link and \ key != self.lnk_children and \ key != self.lnk_nolink: self.log.err('bad value for {}'.format(self.key_dotfile_link)) @@ -626,7 +616,7 @@ class Cfg: newkey = self.key_imp_link if key in self.lnk_settings: if self.lnk_settings[key]: - self.lnk_settings[newkey] = self.lnk_parent + self.lnk_settings[newkey] = self.lnk_link else: self.lnk_settings[newkey] = self.lnk_nolink del self.lnk_settings[key] @@ -646,7 +636,7 @@ class Cfg: and type(v[self.key_dotfiles_link]) is bool: # patch link: if v[self.key_dotfiles_link]: - new = self.lnk_parent + new = self.lnk_link else: new = self.lnk_nolink self._modified = True @@ -755,12 +745,12 @@ class Cfg: return False, self._get_long_key(path, keys) return False, self._get_short_key(path, keys) - def new(self, dotfile, profile, link=LinkTypes.NOLINK, debug=False): - """import new dotfile - dotfile key will change and can be empty""" + def new(self, src, dst, profile, link, debug=False): + """import new dotfile""" # keep it short home = os.path.expanduser('~') - dotfile.dst = dotfile.dst.replace(home, '~', 1) + dst = dst.replace(home, '~', 1) + dotfile = Dotfile('', dst, src) # adding new profile if doesn't exist if profile not in self.lnk_profiles: @@ -806,8 +796,8 @@ class Cfg: } # set the link flag - if link != LinkTypes.NOLINK: - val = self._linktype_to_string(link) + if link != self._get_def_link(): + val = link.name.lower() dots[dotfile.key][self.key_dotfiles_link] = val # link it to this profile in the yaml file diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index cc66e55..16310b6 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -15,7 +15,6 @@ from dotdrop.templategen import Templategen from dotdrop.installer import Installer from dotdrop.updater import Updater from dotdrop.comparator import Comparator -from dotdrop.dotfile import Dotfile from dotdrop.config import Cfg from dotdrop.utils import get_tmpdir, remove, strip_home, run from dotdrop.linktypes import LinkTypes @@ -60,9 +59,10 @@ def cmd_install(o): preactions.append(action) if o.debug: LOG.dbg('installing {}'.format(dotfile)) - if hasattr(dotfile, 'link') and dotfile.link == LinkTypes.PARENT: + if hasattr(dotfile, 'link') and dotfile.link == LinkTypes.LINK: r = inst.link(t, dotfile.src, dotfile.dst, actions=preactions) - elif hasattr(dotfile, 'link') and dotfile.link == LinkTypes.CHILDREN: + elif hasattr(dotfile, 'link') and \ + dotfile.link == LinkTypes.LINK_CHILDREN: r = inst.link_children(t, dotfile.src, dotfile.dst, actions=preactions) else: @@ -236,13 +236,16 @@ def cmd_importer(o): strip = os.sep src = src.lstrip(strip) - # create a new dotfile - dotfile = Dotfile('', dst, src) - - linktype = LinkTypes(o.import_link) + # set the link attribute + linktype = o.import_link + if linktype == LinkTypes.LINK_CHILDREN and \ + not os.path.isdir(path): + LOG.err('importing \"{}\" failed!'.format(path)) + ret = False + continue if o.debug: - LOG.dbg('new dotfile: {}'.format(dotfile)) + LOG.dbg('new dotfile: src:{} dst:{}'.format(src, dst)) # prepare hierarchy for dotfile srcf = os.path.join(o.dotpath, src) @@ -259,19 +262,14 @@ def cmd_importer(o): cmd = ['cp', '-R', '-L', dst, srcf] if o.dry: LOG.dry('would run: {}'.format(' '.join(cmd))) - if linktype == LinkTypes.PARENT: - LOG.dry('would symlink {} to {}'.format(srcf, dst)) else: r, _ = run(cmd, raw=False, debug=o.debug, checkerr=True) if not r: LOG.err('importing \"{}\" failed!'.format(path)) ret = False continue - if linktype == LinkTypes.PARENT: - remove(dst) - os.symlink(srcf, dst) - retconf, dotfile = o.conf.new(dotfile, o.profile, - link=linktype, debug=o.debug) + retconf, dotfile = o.conf.new(src, dst, o.profile, + linktype, debug=o.debug) if retconf: LOG.sub('\"{}\" imported'.format(path)) cnt += 1 diff --git a/dotdrop/dotfile.py b/dotdrop/dotfile.py index 178d363..c7e5944 100644 --- a/dotdrop/dotfile.py +++ b/dotdrop/dotfile.py @@ -30,6 +30,9 @@ class Dotfile: self.dst = dst self.src = src self.link = link + # ensure link of right type + if type(link) != LinkTypes: + raise Exception('bad value for link: {}'.format(link)) self.actions = actions self.trans_r = trans_r self.trans_w = trans_w diff --git a/dotdrop/linktypes.py b/dotdrop/linktypes.py index efb1338..59da01f 100644 --- a/dotdrop/linktypes.py +++ b/dotdrop/linktypes.py @@ -3,5 +3,5 @@ from enum import IntEnum class LinkTypes(IntEnum): NOLINK = 0 - PARENT = 1 - CHILDREN = 2 + LINK = 1 + LINK_CHILDREN = 2 diff --git a/dotdrop/options.py b/dotdrop/options.py index d5601a3..41ec196 100644 --- a/dotdrop/options.py +++ b/dotdrop/options.py @@ -33,9 +33,9 @@ CONFIG = 'config.yaml' HOMECFG = '~/.config/{}'.format(NAME) OPT_LINK = { - 'nolink': LinkTypes.NOLINK, - 'link': LinkTypes.PARENT, - 'link_children': LinkTypes.CHILDREN} + LinkTypes.NOLINK.name.lower(): LinkTypes.NOLINK, + LinkTypes.LINK.name.lower(): LinkTypes.LINK, + LinkTypes.LINK_CHILDREN.name.lower(): LinkTypes.LINK_CHILDREN} BANNER = """ _ _ _ __| | ___ | |_ __| |_ __ ___ _ __ @@ -186,12 +186,15 @@ class Options(AttrMonitor): # import link default value self.import_link = self.link_on_import - link = self.args['--link'] - if link: + if self.args['--link']: + # overwrite default import link with cli switch + link = self.args['--link'] if link not in OPT_LINK.keys(): self.log.err('bad option for --link: {}'.format(link)) sys.exit(USAGE) self.import_link = OPT_LINK[link] + if self.debug: + self.log.dbg('link_import value: {}'.format(self.import_link)) # "listfiles" specifics self.listfiles_templateonly = self.args['--template'] diff --git a/tests-ng/deprecated-link.sh b/tests-ng/deprecated-link.sh index 749d340..ad5c9c3 100755 --- a/tests-ng/deprecated-link.sh +++ b/tests-ng/deprecated-link.sh @@ -113,6 +113,7 @@ cd ${ddpath} | ${bin} compare -c ${cfg} -p p1 cat ${cfg} # fail if find some of these entries +echo "========> test for bad entries" set +e grep 'link_children: true' ${cfg} >/dev/null && exit 1 grep 'link_children: false' ${cfg} >/dev/null && exit 1 @@ -123,12 +124,13 @@ grep 'link_by_default: false' ${cfg} >/dev/null && exit 1 set -e # test values have been correctly updated +echo "========> test for updated entries" dotfiles=`cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 | grep -v '^ '` -echo "${dotfiles}" | grep '^f_link ' | grep ', link: parent)' +echo "${dotfiles}" | grep '^f_link ' | grep ', link: link)' echo "${dotfiles}" | grep '^f_nolink ' | grep ', link: nolink)' echo "${dotfiles}" | grep '^f_nolink1 ' | grep ', link: nolink)' -echo "${dotfiles}" | grep '^f_children ' | grep ', link: children)' -echo "${dotfiles}" | grep '^f_children2 ' | grep ', link: children)' +echo "${dotfiles}" | grep '^f_children ' | grep ', link: link_children)' +echo "${dotfiles}" | grep '^f_children2 ' | grep ', link: link_children)' echo "${dotfiles}" | grep '^f_children3 ' | grep ', link: nolink)' ## CLEANING diff --git a/tests-ng/import-link-children.sh b/tests-ng/import-link-children.sh new file mode 100755 index 0000000..fe58c48 --- /dev/null +++ b/tests-ng/import-link-children.sh @@ -0,0 +1,136 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2017, deadc0de6 +# +# test importing link_children +# returns 1 in case of error +# + +# 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'` + +# dotpath +dotpath="${tmps}/dotfiles" +mkdir -p ${dotpath} + +# create the dotfile to import +dt="${tmpd}/directory" +mkdir -p ${dt} +# subdir +dtsub1="${dt}/sub1" +mkdir -p ${dtsub1} +dtsub2="${dt}/sub2" +mkdir -p ${dtsub2} +dtsub3="${dtsub1}/subsub1" +mkdir -p ${dtsub3} +# files +f1="${dt}/file" +subf1="${dtsub1}/file" +subf2="${dtsub2}/file" +subf3="${dtsub3}/file" +touch ${f1} ${subf1} ${subf2} ${subf3} + +# create the config file +cfg="${tmps}/config.yaml" + +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: +profiles: +_EOF + +# import +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 -V --link=link_children ${dt} + +# check is set to link_children +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "d_`basename ${dt}`") +echo ${line} | grep 'link: link_children' + +# checks file exists in dotpath +[ ! -e ${dotpath}/${dt} ] && echo "dotfile not imported" && exit 1 +[ ! -e ${dotpath}/${dtsub1} ] && echo "sub1 not found in dotpath" && exit 1 +[ ! -e ${dotpath}/${dtsub2} ] && echo "sub2 not found in dotpath" && exit 1 +[ ! -e ${dotpath}/${dtsub3} ] && echo "sub3 not found in dotpath" && exit 1 +[ ! -e ${dotpath}/${f1} ] && echo "f1 not found in dotpath" && exit 1 +[ ! -e ${dotpath}/${subf1} ] && echo "subf1 not found in dotpath" && exit 1 +[ ! -e ${dotpath}/${subf2} ] && echo "subf2 not found in dotpath" && exit 1 +[ ! -e ${dotpath}/${subf3} ] && echo "subf3 not found in dotpath" && exit 1 + +# checks file exists in fs +[ ! -e ${dt} ] && echo "dotfile not imported" && exit 1 +[ ! -e ${dtsub1} ] && echo "sub1 not found in fs" && exit 1 +[ ! -e ${dtsub2} ] && echo "sub2 not found in fs" && exit 1 +[ ! -e ${dtsub3} ] && echo "sub3 not found in fs" && exit 1 +[ ! -e ${f1} ] && echo "f1 not found in fs" && exit 1 +[ ! -e ${subf1} ] && echo "subf1 not found in fs" && exit 1 +[ ! -e ${subf2} ] && echo "subf2 not found in fs" && exit 1 +[ ! -e ${subf3} ] && echo "subf3 not found in fs" && exit 1 + +# install +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V + +# checks file have correct type in fs +file ${f1} +[ ! -h ${f1} ] && echo "f1 is not a symlink" && exit 1 +[ -h ${subf1} ] && echo "subf1 is not a regular file" && exit 1 +[ -h ${subf2} ] && echo "subf2 is not a regular file" && exit 1 +[ -h ${subf3} ] && echo "subf3 is not a regular file" && exit 1 +[ ! -h ${dtsub1} ] && echo "dtsub1 is not a symlink" && exit 1 +[ ! -h ${dtsub2} ] && echo "dtsub2 is not a symlink" && exit 1 +[ -h ${dtsub3} ] && echo "dtsub3 is not a regular directory" && exit 1 + +echo "DOTPATH" +tree ${tmps}/dotfiles +echo "FILESYSTEM" +tree ${dt} + +## CLEANING +rm -rf ${tmps} ${tmpd} + +echo "OK" +exit 0 diff --git a/tests-ng/link-value-tests.sh b/tests-ng/link-value-tests.sh new file mode 100755 index 0000000..94669b0 --- /dev/null +++ b/tests-ng/link-value-tests.sh @@ -0,0 +1,479 @@ +#!/usr/bin/env bash +# author: deadc0de6 (https://github.com/deadc0de6) +# Copyright (c) 2019, deadc0de6 +# +# test the behavior when playing with link_dotfile_default +# and link_on_import on import +# returns 1 in case of error +# + +# 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'` + +# create the config file +cfg="${tmps}/config.yaml" + +# ---------------------------------------------------------- +echo -e "\n======> import with all default" +# create the source +rm -rf ${tmpd}/qwert +echo "test" > ${tmpd}/qwert +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 ${df} -V + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "f_`basename ${df}`") +echo ${line} | grep 'link: nolink' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ -h ${df} ] && echo "is symlink" && exit 1 + +# ---------------------------------------------------------- +echo -e "\n======> import with link_on_import=nolink and link_dotfile_default=nolink" +# create the source +rm -rf ${tmpd}/qwert +echo "test" > ${tmpd}/qwert +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + link_on_import: nolink + link_dotfile_default: nolink +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 ${df} -V + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "f_`basename ${df}`") +echo ${line} | grep 'link: nolink' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ -h ${df} ] && echo "is symlink" && exit 1 + +# ---------------------------------------------------------- +echo -e "\n======> import with link_on_import=nolink and link_dotfile_default=nolink and --link=nolink" +# create the source +rm -rf ${tmpd}/qwert +echo "test" > ${tmpd}/qwert +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + link_on_import: nolink + link_dotfile_default: nolink +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 ${df} -V --link=nolink + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "f_`basename ${df}`") +echo ${line} | grep 'link: nolink' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ -h ${df} ] && echo "is symlink" && exit 1 + +# ---------------------------------------------------------- +echo -e "\n======> import with link_on_import=nolink and link_dotfile_default=nolink and --link=link" +# create the source +rm -rf ${tmpd}/qwert +echo "test" > ${tmpd}/qwert +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + link_on_import: nolink + link_dotfile_default: nolink +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 ${df} -V --link=link + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "f_`basename ${df}`") +echo ${line} | grep 'link: link' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ ! -h ${df} ] && echo "not symlink" && exit 1 + +# ---------------------------------------------------------- +echo -e "\n======> import with link_on_import=link and link_dotfile_default=nolink" +# create the source +rm -rf ${tmpd}/qwert +echo "test" > ${tmpd}/qwert +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + link_on_import: link + link_dotfile_default: nolink +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 ${df} -V + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "f_`basename ${df}`") +echo ${line} | grep 'link: link' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ ! -h ${df} ] && echo "not symlink" && exit 1 + +# ---------------------------------------------------------- +echo -e "\n======> import with link_on_import=link and link_dotfile_default=nolink and --link=nolink" +# create the source +rm -rf ${tmpd}/qwert +echo "test" > ${tmpd}/qwert +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + link_on_import: link + link_dotfile_default: nolink +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 ${df} -V --link=nolink + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "f_`basename ${df}`") +echo ${line} | grep 'link: nolink' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ -h ${df} ] && echo "is symlink" && exit 1 + +# ---------------------------------------------------------- +echo -e "\n======> import with link_on_import=nolink and link_dotfile_default=link" +# create the source +rm -rf ${tmpd}/qwert +echo "test" > ${tmpd}/qwert +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + link_on_import: nolink + link_dotfile_default: link +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 ${df} -V --link=nolink + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "f_`basename ${df}`") +echo ${line} | grep 'link: nolink' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ -h ${df} ] && echo "is symlink" && exit 1 + +# ---------------------------------------------------------- +echo -e "\n======> import with link_on_import=link and link_dotfile_default=nolink and --link=nolink" +# create the source +rm -rf ${tmpd}/qwert +echo "test" > ${tmpd}/qwert +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + link_on_import: link + link_dotfile_default: nolink +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 ${df} -V --link=nolink + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "f_`basename ${df}`") +echo ${line} | grep 'link: nolink' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ -h ${df} ] && echo "is symlink" && exit 1 + +# ---------------------------------------------------------- +echo -e "\n======> import with all default and --link=link" +# create the source +rm -rf ${tmpd}/qwert +echo "test" > ${tmpd}/qwert +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} --link=link -p p1 ${df} -V + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "f_`basename ${df}`") +echo ${line} | grep 'link: link' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ ! -h ${df} ] && echo "not a symlink" && exit 1 + +# ---------------------------------------------------------- +echo -e "\n======> import with all default and --link=link_children" +# create the source +rm -rf ${tmpd}/qwert +echo "test" > ${tmpd}/qwert +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +set +e +cd ${ddpath} | ${bin} import -c ${cfg} --link=link_children -p p1 ${df} -V +[ "$?" = "0" ] && echo "link_children with file should fail" && exit 1 +set -e + +# ---------------------------------------------------------- +echo -e "\n======> import with all default and --link=link_children" +# create the source +rm -rf ${tmpd}/qwert +mkdir -p ${tmpd}/qwert +echo "test" > ${tmpd}/qwert/file +mkdir -p ${tmpd}/qwert/directory +echo "test" > ${tmpd}/qwert/directory/file + +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} --link=link_children -p p1 ${df} -V + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "d_`basename ${df}`") +echo ${line} | grep 'link: link_children' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ -h ${df} ] && echo "is a symlink" && exit 1 +[ ! -h ${df}/file ] && echo "file is not a symlink" && exit 1 +[ ! -h ${df}/directory ] && echo "directory is not a symlink" && exit 1 +[ -h ${df}/directory/file ] && echo "directory/file is a symlink" && exit 1 + +echo -e "\n======> import with link_on_import=link_children and link_dotfile_default=nolink" +# create the source +rm -rf ${tmpd}/qwert +mkdir -p ${tmpd}/qwert +echo "test" > ${tmpd}/qwert/file +mkdir -p ${tmpd}/qwert/directory +echo "test" > ${tmpd}/qwert/directory/file + +# clean +rm -rf ${tmps}/dotfiles +mkdir -p ${tmps}/dotfiles +# config file +cat > ${cfg} << _EOF +config: + backup: true + create: true + dotpath: dotfiles + link_on_import: link_children + link_dotfile_default: nolink +dotfiles: +profiles: +_EOF + +# import +df="${tmpd}/qwert" +cd ${ddpath} | ${bin} import -c ${cfg} -p p1 ${df} -V + +# checks +cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V +line=$(cd ${ddpath} | ${bin} listfiles -c ${cfg} -p p1 -V | grep "d_`basename ${df}`") +echo ${line} | grep 'link: link_children' + +# try to install +rm -rf ${tmpd}/qwert +cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -V +[ ! -e ${df} ] && echo "does not exist" && exit 1 +[ -h ${df} ] && echo "is a symlink" && exit 1 +[ ! -h ${df}/file ] && echo "file is not a symlink" && exit 1 +[ ! -h ${df}/directory ] && echo "directory is not a symlink" && exit 1 +[ -h ${df}/directory/file ] && echo "directory/file is a symlink" && exit 1 + +## CLEANING +rm -rf ${tmps} ${tmpd} + +echo "OK" +exit 0 diff --git a/tests/test_config.py b/tests/test_config.py index bfa27bf..c49b903 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -50,15 +50,19 @@ class TestConfig(unittest.TestCase): self.assertTrue(conf.dump() != '') def test_def_link(self): - self._test_link_import('nolink', LinkTypes.PARENT, 'link') + self._test_link_import('nolink', LinkTypes.LINK, 'link') self._test_link_import('nolink', LinkTypes.NOLINK, 'nolink') - self._test_link_import('nolink', LinkTypes.CHILDREN, 'link_children') - self._test_link_import('link', LinkTypes.PARENT, 'link') + self._test_link_import('nolink', + LinkTypes.LINK_CHILDREN, + 'link_children') + self._test_link_import('link', LinkTypes.LINK, 'link') self._test_link_import('link', LinkTypes.NOLINK, 'nolink') - self._test_link_import('link', LinkTypes.CHILDREN, 'link_children') - self._test_link_import('link_children', LinkTypes.PARENT, 'link') + self._test_link_import('link', + LinkTypes.LINK_CHILDREN, + 'link_children') + self._test_link_import('link_children', LinkTypes.LINK, 'link') self._test_link_import('link_children', LinkTypes.NOLINK, 'nolink') - self._test_link_import('link_children', LinkTypes.CHILDREN, + self._test_link_import('link_children', LinkTypes.LINK_CHILDREN, 'link_children') self._test_link_import_fail('whatever') diff --git a/tests/test_import.py b/tests/test_import.py index 7116120..25ef41f 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -112,7 +112,7 @@ class TestImport(unittest.TestCase): o.import_path = dfiles cmd_importer(o) # import symlink - o.import_link = LinkTypes.PARENT + o.import_link = LinkTypes.LINK sfiles = [dotfile6, dotfile7] o.import_path = sfiles cmd_importer(o) @@ -191,12 +191,6 @@ class TestImport(unittest.TestCase): sub4) self.assertTrue(os.path.exists(s4)) - # test symlink on filesystem - self.assertTrue(os.path.islink(dotfile6)) - self.assertTrue(os.path.realpath(dotfile6) == indt6) - self.assertTrue(os.path.islink(dotfile7)) - self.assertTrue(os.path.realpath(dotfile7) == indt7) - cmd_list_profiles(o) cmd_list_files(o) diff --git a/tests/test_install.py b/tests/test_install.py index 424b59d..cf3e8aa 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -17,8 +17,8 @@ from dotdrop.installer import Installer from dotdrop.action import Action from dotdrop.dotdrop import cmd_install from dotdrop.options import BACKUP_SUFFIX -from dotdrop.linktypes import LinkTypes from dotdrop.utils import header +from dotdrop.linktypes import LinkTypes class TestInstall(unittest.TestCase): @@ -57,12 +57,7 @@ exec bspwm f.write(' {}:\n'.format(d.key)) f.write(' dst: {}\n'.format(d.dst)) f.write(' src: {}\n'.format(d.src)) - if d.link == LinkTypes.CHILDREN: - f.write(' link_children: {}\n' - .format(str(d.link == LinkTypes.CHILDREN).lower())) - else: - f.write(' link: {}\n' - .format(str(d.link == LinkTypes.PARENT).lower())) + f.write(' link: {}\n'.format(d.link.name.lower())) if len(d.actions) > 0: f.write(' actions:\n') for action in d.actions: @@ -122,7 +117,8 @@ exec bspwm f5, c5 = create_random_file(tmp) dst5 = os.path.join(dst, get_string(6)) self.addCleanup(clean, dst5) - d5 = Dotfile(get_string(6), dst5, os.path.basename(f5), link=True) + d5 = Dotfile(get_string(6), dst5, + os.path.basename(f5), link=LinkTypes.LINK) # create the dotfile directories in dotdrop dir1 = create_dir(os.path.join(tmp, get_string(6))) @@ -148,7 +144,8 @@ exec bspwm sub4, _ = create_random_file(dir2) self.assertTrue(os.path.exists(sub4)) # make up the dotfile - d7 = Dotfile(get_string(6), dst7, os.path.basename(dir2), link=True) + d7 = Dotfile(get_string(6), dst7, + os.path.basename(dir2), link=LinkTypes.LINK) # to test actions value = get_string(12)