1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 15:39:43 +00:00

add src=key for dotfile if not present (for #150)

This commit is contained in:
deadc0de6
2019-05-31 20:31:24 +02:00
parent 806b4690b2
commit 8f8c39ce9c
2 changed files with 113 additions and 4 deletions

View File

@@ -103,6 +103,7 @@ class CfgYaml:
# dotfiles
self.dotfiles = self._get_entry(self.yaml_dict, self.key_dotfiles)
self.dotfiles = self._norm_dotfiles(self.dotfiles)
if self.debug:
self.log.dbg('dotfiles: {}'.format(self.dotfiles))
@@ -114,7 +115,7 @@ class CfgYaml:
# actions
self.actions = self._get_entry(self.yaml_dict, self.key_actions,
mandatory=False)
self.actions = self._patch_actions(self.actions)
self.actions = self._norm_actions(self.actions)
if self.debug:
self.log.dbg('actions: {}'.format(self.actions))
@@ -246,7 +247,7 @@ class CfgYaml:
return allvars
def _patch_actions(self, actions):
def _norm_actions(self, actions):
"""
ensure each action is either pre or post explicitely
action entry of the form {action_key: (pre|post, action)}
@@ -262,6 +263,19 @@ class CfgYaml:
new[k] = (self.action_pre, v)
return new
def _norm_dotfiles(self, dotfiles):
"""add 'src' as 'key if not present"""
if not dotfiles:
return dotfiles
new = {}
for k, v in dotfiles.items():
if self.key_dotfile_src not in v:
v[self.key_dotfile_src] = k
new[k] = v
else:
new[k] = v
return new
def _get_variables_dict(self, profile, seen, sub=False):
"""return enriched variables"""
variables = {}
@@ -352,7 +366,7 @@ class CfgYaml:
self.log.dbg('import actions from {}'.format(path))
self.actions = self._import_sub(path, self.key_actions,
self.actions, mandatory=False,
patch_func=self._patch_actions)
patch_func=self._norm_actions)
# profiles -> import
for k, v in self.profiles.items():
@@ -365,7 +379,8 @@ class CfgYaml:
current = v.get(self.key_dotfiles, [])
path = self.resolve_path(p)
current = self._import_sub(path, self.key_dotfiles,
current, mandatory=False)
current, mandatory=False,
path_func=self._norm_dotfiles)
v[self.key_dotfiles] = current
def _resolve_import_configs(self):

94
tests-ng/dotfile-no-src.sh Executable file
View File

@@ -0,0 +1,94 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2019, deadc0de6
#
# test dotfiles with no 'src'
# returns 1 in case of error
#
# exit on first error
set -e
#set -v
# 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
echo "dotfiles source (dotpath): ${tmps}"
# the dotfile destination
tmpd=`mktemp -d --suffix='-dotdrop-tests'`
echo "dotfiles destination: ${tmpd}"
# create the config file
cfg="${tmps}/config.yaml"
cat > ${cfg} << _EOF
config:
backup: true
create: true
dotpath: dotfiles
dotfiles:
abc:
dst: ${tmpd}/abc
profiles:
p1:
dotfiles:
- ALL
_EOF
#cat ${cfg}
# create the dotfiles
echo "abc" > ${tmps}/dotfiles/abc
###########################
# test install and compare
###########################
# install
cd ${ddpath} | ${bin} install -f -c ${cfg} -p p1 -b -V
[ "$?" != "0" ] && exit 1
# checks
[ ! -e ${tmpd}/abc ] && exit 1
grep 'abc' ${tmpd}/abc
## CLEANING
rm -rf ${tmps} ${tmpd} ${tmpx} ${tmpy}
echo "OK"
exit 0