From 5b861f5c93e4c4e2c1672ff37ac4c498b2073e80 Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Fri, 29 Aug 2014 20:04:26 +0400 Subject: [PATCH] bak plugin: tests added --- plugins/bak/bak.load | 53 +++++++++-------- plugins/bak/tests/helper.fish | 8 +++ plugins/bak/tests/test_bak.fish | 100 ++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 24 deletions(-) create mode 100644 plugins/bak/tests/helper.fish create mode 100755 plugins/bak/tests/test_bak.fish diff --git a/plugins/bak/bak.load b/plugins/bak/bak.load index acbc636..960d565 100644 --- a/plugins/bak/bak.load +++ b/plugins/bak/bak.load @@ -4,44 +4,49 @@ function __bak_help $argv[1] SOURCE..." end +function __bak_name + # trim / for directories + set file (echo $argv[1] | sed 's/\/$//') + echo "$file."(date +'%Y%m%d_%H%M%S')".bak" +end + function __bak set program $argv[2] if [ (count $argv) -gt 2 ] for file in $argv[3..-1] - function bak_name - # trim / for directories - set file (echo $argv[1] | sed 's/\/$//') - echo "$file.(date +"%Y%m%d_%H%M%S").bak" - end - - eval $program $file (bak_name $file) + eval $program $file (__bak_name $file) end else __bak_help $argv[1] end end +set -g __bak_re '(.*)\.[0-9]{8,8}_[0-9]{6,6}\.bak$' + +function __is_bak + set file $argv[1] + echo "$file" | grep -Eq $__bak_re +end + +function __bak_normalized + set file $argv[1] + echo "$file" | sed -E "s/$__bak_re/\1/g" +end + function __unbak set program $argv[2] if [ (count $argv) -gt 2 ] for file in $argv[3..-1] - set re_bak '(.*)\.[0-9]{8,8}_[0-9]{6,6}\.bak' - set file $argv[1] - set normalized (echo "$file" | sed -r "s/$re_bak/\1/g") - - function is_bak - echo "$file" | perl -ne "print if /$re_bak/" > /dev/null - end - - if test ! -e $file - echo "File \"$file\" not exists! Cannot unbak \"$file\"." - else if not is_bak $file - echo "File \"$file\" don't meet bak files convention! Cannot unbak \"$file\"." - else if test -e $normalized - echo "File \"$normalized\" exists! Cannot unbak \"$file\"." - else - eval $program $file $normalized - end + set normalized (__bak_normalized $file) + if test ! -e $file + echo "File \"$file\" not exists! Cannot unbak \"$file\"." + else if not __is_bak $file + echo "File \"$file\" don't meet bak files convention! Cannot unbak \"$file\"." + else if test -e $normalized + echo "File \"$normalized\" exists! Cannot unbak \"$file\"." + else + eval $program $file $normalized + end end else __bak_help $argv[1] diff --git a/plugins/bak/tests/helper.fish b/plugins/bak/tests/helper.fish new file mode 100644 index 0000000..d631e30 --- /dev/null +++ b/plugins/bak/tests/helper.fish @@ -0,0 +1,8 @@ +set -l fish_tank /usr/local/share/fish-tank/tank.fish +if not test -e $fish_tank + echo 'error: fish-tank is required to run these tests (https://github.com/terlar/fish-tank)' + exit 1 +end + +source $fish_tank +source (dirname (status -f))/../*.fish diff --git a/plugins/bak/tests/test_bak.fish b/plugins/bak/tests/test_bak.fish new file mode 100755 index 0000000..3f17730 --- /dev/null +++ b/plugins/bak/tests/test_bak.fish @@ -0,0 +1,100 @@ +function suite_bak + function setup +return 0 + end + + function teardown + rm -rf $test_dir/* + end + + function test_is_bak + assert (__is_bak '.ccnet.20140817_234302.bak') + assert (__is_bak 'file with spaces.20140817_234302.bak') + assert (not __is_bak '.ccnet.bak') + end + + function test_normalized + assert_equal '.ccnet' (__bak_normalized '.ccnet.20140817_234302.bak') + assert_equal 'file with spaces' (__bak_normalized 'file with spaces.20140817_234302.bak') + end + + function test_mv_single + touch a + mvbak a + assert __is_bak (ls) + end + + function test_mv_multiple + touch a b + mvbak a b + for f in (ls) + assert __is_bak $f + end + end + + function test_unmv_single + touch a + mvbak a + unmvbak (ls) + assert_equal a (ls) + end + + function test_unmv_multiple + set files (seq 4) + touch $files + mvbak $files + unmvbak (ls) + assert_equal "$files" (ls) + end + + function test_cp_single + touch a + cpbak a + assert_equal (echo -e 'a' (__bak_name a) | sort) (ls | sort) + end + + function test_cp_multiple + set files (seq 4) + touch $files + cpbak $files + for f in $files + set files_bak "$files_bak\n"(__bak_name $f) + end + set expected (begin; echo $files | sed 's/ /\n/g'; echo -e $files_bak; end | sort | grep -v '^$') + assert_equal "$expected" (ls | sort) + end + + function test_uncp_single + touch a + cpbak a + rm a + uncpbak (ls) + assert_equal (echo -e 'a' (__bak_name a) | sort) (ls | sort) + end + + function test_cp_multiple + set files (seq 4) + touch $files + cpbak $files + rm $files + uncpbak (ls) + for f in $files + set files_bak "$files_bak\n"(__bak_name $f) + end + set expected (begin; echo $files | sed 's/ /\n/g'; echo -e $files_bak; end | sort | grep -v '^$') + end +end + +if not set -q tank_running + source (dirname (status -f))/helper.fish + + set -g test_dir /tmp/bak_test + mkdir -p $test_dir + pushd + cd $test_dir + + tank_run + + popd + set -e test_dir +end