mirror of
1
0
Fork 0

Support both `[]` and `?` glob patterns

This commit is contained in:
Eric Engstrom 2021-04-15 23:28:19 -05:00
parent dcb3743115
commit f56e903cee
No known key found for this signature in database
GPG Key ID: 9232FD58D13AAAB2
3 changed files with 113 additions and 7 deletions

View File

@ -60,15 +60,14 @@ class Link(dotbot.Plugin):
self._log.warning("Globbing couldn't find anything matching " + str(path))
success = False
continue
glob_star_loc = path.find('*')
if glob_star_loc == -1 and destination[-1] == '/':
if len(glob_results) == 1 and destination[-1] == '/':
self._log.error("Ambiguous action requested.")
self._log.error("No wildcard in glob, directory use undefined: " +
destination + " -> " + str(glob_results))
self._log.warning("Did you want to link the directory or into it?")
success = False
continue
elif glob_star_loc == -1 and len(glob_results) == 1:
elif len(glob_results) == 1 and destination[-1] != '/':
# perform a normal link operation
if create:
success &= self._create(destination)
@ -77,11 +76,11 @@ class Link(dotbot.Plugin):
success &= self._link(path, destination, relative, canonical_path, ignore_missing)
else:
self._log.lowinfo("Globs from '" + path + "': " + str(glob_results))
glob_base = path[:glob_star_loc]
if glob_base.endswith('/.') or glob_base == '.':
glob_base = path[:glob_star_loc - 1]
for glob_full_item in glob_results:
glob_item = glob_full_item[len(glob_base):]
# Find common dirname between pattern and the item:
glob_dirname = os.path.dirname(os.path.commonprefix([path, glob_full_item]))
glob_item = (glob_full_item if len(glob_dirname) == 0 else glob_full_item[len(glob_dirname) + 1:])
# where is it going
glob_link_destination = os.path.join(destination, glob_item)
if create:
success &= self._create(glob_link_destination)

1
test/.gitignore vendored
View File

@ -1 +1,2 @@
.vagrant/
*.log

View File

@ -0,0 +1,106 @@
test_description='link glob, all patterns'
. '../test-lib.bash'
allfruit=(apple apricot banana cherry currant cantalope)
test_expect_success 'glob patterns setup' '
mkdir ${DOTFILES}/conf &&
for fruit in "${allfruit[@]}"; do
echo "${fruit}" > ${DOTFILES}/conf/${fruit}
echo "dot-${fruit}" > ${DOTFILES}/conf/.${fruit}
done
'
test_expect_success 'glob patterns: "conf/*"' '
run_dotbot -v <<EOF
- defaults:
link:
glob: true
create: true
- link:
~/globtest: conf/*
EOF
for fruit in "${allfruit[@]}"; do
grep "${fruit}" ~/globtest/${fruit} &&
test \! -e ~/globtest/.${fruit}
done
'
test_expect_success 'reset' 'rm -rf ~/globtest'
test_expect_success 'glob pattern: "conf/.*"' '
run_dotbot -v <<EOF
- defaults:
link:
glob: true
create: true
- link:
~/globtest: conf/.*
EOF
for fruit in "${allfruit[@]}"; do
test \! -e ~/globtest/${fruit} &&
grep "dot-${fruit}" ~/globtest/.${fruit}
done
'
test_expect_success 'reset 2' 'rm -rf ~/globtest'
test_expect_success 'glob pattern: "conf/[bc]*"' '
run_dotbot -v <<EOF
- defaults:
link:
glob: true
create: true
- link:
~/globtest: conf/[bc]*
EOF
for fruit in "${allfruit[@]}"; do
[[ $fruit = [bc]* ]] &&
grep "${fruit}" ~/globtest/${fruit} ||
test \! -e ~/globtest/${fruit} &&
test \! -e ~/globtest/.${fruit}
done
'
test_expect_success 'reset 3' 'rm -rf ~/globtest'
test_expect_success 'glob pattern: "conf/*e"' '
run_dotbot -v <<EOF
- defaults:
link:
glob: true
create: true
- link:
~/globtest: conf/*e
EOF
for fruit in "${allfruit[@]}"; do
[[ $fruit = *e ]] &&
grep "${fruit}" ~/globtest/${fruit} ||
test \! -e ~/globtest/${fruit} &&
test \! -e ~/globtest/.${fruit}
done
'
test_expect_success 'reset 4' 'rm -rf ~/globtest'
test_expect_success 'glob pattern: "conf/??r*"' '
run_dotbot -v <<EOF
- defaults:
link:
glob: true
create: true
- link:
~/globtest: conf/??r*
EOF
for fruit in "${allfruit[@]}"; do
[[ $fruit = ??r* ]] &&
grep "${fruit}" ~/globtest/${fruit} ||
test \! -e ~/globtest/${fruit} &&
test \! -e ~/globtest/.${fruit}
done
'