1
0
Fork 0
mirror of synced 2024-11-22 16:25:34 -05:00

added force option for clean plugin; added tests

This commit is contained in:
dein0s 2017-03-22 13:22:10 +03:00
parent ef558f85d9
commit 2303303eea
3 changed files with 68 additions and 9 deletions

View file

@ -17,18 +17,22 @@ class Clean(dotbot.Plugin):
def _process_clean(self, targets): def _process_clean(self, targets):
success = True success = True
for target in targets: defaults = self._context.defaults().get(self._directive, {})
success &= self._clean(target) force = defaults.get('force', False)
for target in self._dispatch_targets(targets):
if isinstance(targets, dict):
force = targets[target].get('force', force)
success &= self._clean(target, force)
if success: if success:
self._log.info('All targets have been cleaned') self._log.info('All targets have been cleaned')
else: else:
self._log.error('Some targets were not successfully cleaned') self._log.error('Some targets were not successfully cleaned')
return success return success
def _clean(self, target): def _clean(self, target, force):
''' '''
Cleans all the broken symbolic links in target that point to Cleans all the broken symbolic links in target if that point to
a subdirectory of the base directory. a subdirectory of the base directory or forced to clean.
''' '''
if not os.path.isdir(os.path.expanduser(target)): if not os.path.isdir(os.path.expanduser(target)):
self._log.debug('Ignoring nonexistent directory %s' % target) self._log.debug('Ignoring nonexistent directory %s' % target)
@ -36,12 +40,24 @@ class Clean(dotbot.Plugin):
for item in os.listdir(os.path.expanduser(target)): for item in os.listdir(os.path.expanduser(target)):
path = os.path.join(os.path.expanduser(target), item) path = os.path.join(os.path.expanduser(target), item)
if not os.path.exists(path) and os.path.islink(path): if not os.path.exists(path) and os.path.islink(path):
if self._in_directory(path, self._context.base_directory()): points_at = os.path.join(os.path.dirname(path), os.readlink(path))
self._log.lowinfo('Removing invalid link %s -> %s' % if self._in_directory(path, self._context.base_directory()) or force:
(path, os.path.join(os.path.dirname(path), os.readlink(path)))) self._log.lowinfo('Removing invalid link %s -> %s' % (path, points_at))
os.remove(path) os.remove(path)
else:
self._log.lowinfo('Link %s -> %s not removed (force=%s). '\
'You can remove it manually.' % (path, points_at, force))
return True return True
def _dispatch_targets(self, targets):
'''
Returns list of targets to process.
'''
if isinstance(targets, list):
return targets
if isinstance(targets, dict):
return targets.keys()
def _in_directory(self, path, directory): def _in_directory(self, path, directory):
''' '''
Returns true if the path is in the directory. Returns true if the path is in the directory.

View file

@ -0,0 +1,18 @@
test_description='clean forced to remove files linking outside dotfiles directory'
. '../test-lib.bash'
test_expect_success 'setup' '
ln -s /nowhere ~/.g
'
test_expect_success 'run' '
run_dotbot <<EOF
- clean:
~/:
force: true
EOF
'
test_expect_success 'test' '
! test -h ~/.g
'

View file

@ -4,7 +4,8 @@ test_description='defaults setting works'
test_expect_success 'setup' ' test_expect_success 'setup' '
echo "apple" > ${DOTFILES}/f && echo "apple" > ${DOTFILES}/f &&
echo "grape" > ~/f && echo "grape" > ~/f &&
ln -s ~/f ~/.f ln -s ~/f ~/.f &&
ln -s /nowhere ~/.g
' '
test_expect_failure 'run-fail' ' test_expect_failure 'run-fail' '
@ -32,3 +33,27 @@ EOF
test_expect_success 'test' ' test_expect_success 'test' '
grep "apple" ~/.f grep "apple" ~/.f
' '
test_expect_success 'run-fail' '
run_dotbot <<EOF
- clean: ["~"]
EOF
'
test_expect_failure 'test-fail' '
! test -h ~/.g
'
test_expect_success 'run' '
run_dotbot <<EOF
- defaults:
clean:
force: true
- clean: ["~"]
EOF
'
test_expect_success 'test' '
! test -h ~/.g
'