1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 13:56:44 +00:00

handle bad diff_command

This commit is contained in:
deadc0de6
2022-06-25 14:58:27 +02:00
parent da272553e0
commit d147dfca5d
3 changed files with 135 additions and 5 deletions

View File

@@ -6,10 +6,12 @@ settings block
"""
import os
from dotdrop.exceptions import YamlException
# local imports
from dotdrop.linktypes import LinkTypes
from dotdrop.dictparser import DictParser
from dotdrop.utils import is_bin_in_path
ENV_WORKDIR = 'DOTDROP_WORKDIR'
@@ -56,6 +58,9 @@ class Settings(DictParser):
key_import_configs = 'import_configs'
key_import_variables = 'import_variables'
# defaults
default_diff_command = 'diff -r -u {0} {1}'
def __init__(self, backup=True, banner=True,
create=True, default_actions=None, dotpath='dotfiles',
ignoreempty=False, import_actions=None, import_configs=None,
@@ -66,7 +71,7 @@ class Settings(DictParser):
impignore=None, workdir='~/.config/dotdrop',
showdiff=False, minversion=None,
func_file=None, filter_file=None,
diff_command='diff -r -u {0} {1}',
diff_command=default_diff_command,
template_dotfile_default=True,
ignore_missing_in_dotdrop=False,
force_chmod=False, chmod_on_import=False,
@@ -108,6 +113,11 @@ class Settings(DictParser):
self.key_prefix = key_prefix
self.key_separator = key_separator
# check diff command
if not is_bin_in_path(self.diff_command):
err = 'bad diff_command: {}'.format(self.diff_command)
raise YamlException(err)
def _serialize_seq(self, name, dic):
"""serialize attribute 'name' into 'dic'"""
seq = getattr(self, name)

View File

@@ -14,7 +14,7 @@ import inspect
import importlib
import filecmp
import itertools
from shutil import rmtree, which
import shutil
import json
import requests
from packaging import version
@@ -176,7 +176,7 @@ def removepath(path, logger=None):
if os.path.islink(path) or os.path.isfile(path):
os.unlink(path)
elif os.path.isdir(path):
rmtree(path)
shutil.rmtree(path)
else:
err = 'Unsupported file type for deletion: {}'.format(path)
raise OSError(err)
@@ -343,10 +343,11 @@ def get_module_from_path(path):
def dependencies_met():
"""make sure all dependencies are met"""
# check unix tools deps
deps = ['file', 'diff']
# diff command is checked in settings.py
deps = ['file']
err = 'The tool \"{}\" was not found in the PATH!'
for dep in deps:
if not which(dep):
if not shutil.which(dep):
raise UnmetDependency(err.format(dep))
# check python deps
err = 'missing python module \"{}\"'
@@ -381,6 +382,13 @@ def dependencies_met():
assert YAML
except ImportError as exc:
raise Exception(err.format('ruamel.yaml')) from exc
# toml
try:
import toml
assert toml
except ImportError as exc:
raise Exception(err.format('toml')) from exc
# pylint: enable=C0415
@@ -494,3 +502,25 @@ def pivot_path(path, newdir, striphome=False, logger=None):
if logger:
logger.dbg('pivot \"{}\" to \"{}\"'.format(path, new))
return new
def is_bin_in_path(command):
"""
check binary from command is in path
"""
bpath = ""
if not command:
return False
try:
binary = command.split(" ")[0]
except ValueError:
return False
if not binary:
return False
try:
bpath = shutil.which(binary)
except shutil.Error:
return False
if not bpath:
return False
return os.path.exists(bpath)

90
tests-ng/bad-diff-cmd.sh Executable file
View File

@@ -0,0 +1,90 @@
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2022, deadc0de6
#
# test bad diff cmd
# 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"
hash coverage 2>/dev/null && bin="coverage run -a --source=dotdrop -m dotdrop.dotdrop" || true
echo "dotdrop path: ${ddpath}"
echo "pythonpath: ${PYTHONPATH}"
# get the helpers
source ${cur}/helpers
echo -e "$(tput setaf 6)==> RUNNING $(basename $BASH_SOURCE) <==$(tput sgr0)"
################################################################
# this is the test
################################################################
basedir=`mktemp -d --suffix='-dotdrop-tests' || mktemp -d`
echo "[+] dotdrop dir: ${basedir}"
clear_on_exit "${basedir}"
# create the config file
cfg="${basedir}/config.yaml"
cat > ${cfg} << _EOF
config:
backup: true
create: true
dotpath: dotfiles
diff_command: xxxxxxxxx {0} {1}
dotfiles:
profiles:
_EOF
set +e
cd ${ddpath} | ${bin} compare -c ${cfg}
[ "$?" = "0" ] && exit 1
out=$(cd ${ddpath} | ${bin} compare -c ${cfg})
echo "${out}" | grep -i 'traceback' && exit 1
cat > ${cfg} << _EOF
config:
backup: true
create: true
dotpath: dotfiles
diff_command:
dotfiles:
profiles:
_EOF
set +e
cd ${ddpath} | ${bin} compare -c ${cfg}
[ "$?" = "0" ] && exit 1
out=$(cd ${ddpath} | ${bin} compare -c ${cfg})
echo "${out}" | grep -i 'traceback' && exit 1
echo "OK"
exit 0