[release] merge previous version into master to avoid the increased version number

This commit is contained in:
Sławomir Śledź 2018-03-11 00:55:52 +01:00
commit 47d2c2b4d4
25 changed files with 3712 additions and 14 deletions

1
.version Normal file
View file

@ -0,0 +1 @@
2.0

350
README.md
View file

@ -1 +1,349 @@
# bash-fun
# Introduction
[Introduction to fun.sh library](http://ssledz.github.io/presentations/bash-fun.html#/)
# Quick start
```bash
#!/bin/bash
. <(test -e fun.sh || curl -Ls https://raw.githubusercontent.com/ssledz/bash-fun/master/src/fun.sh > fun.sh; cat fun.sh)
seq 1 4 | sum
```
# Functions overview
|||||||
|------|------|------|------|------|------|
|**plus**|**append**|**buff**|**curry**|**div**|**drop**|
|**factorial**|**filter**|**foldl**|**foldr**|**head**|**join**|
|**lambda**|**last**|**list**|**map**|**mod**|**mul**|
|**prepend**|**product**|**ret**|**revers_str**|**revers**|**scanl**|
|**splitc**|**strip**|**sub**|**sum**|**tail**|**take**|
|**catch**|**try**|**tupl**|**tupr**|**tup**|**tupx**|
|**unlist**|**zip**|**λ**|**with_trampoline**|**res**|**call**|
## *list/unlist*
```bash
$ list 1 2 3
1
2
3
$ list 1 2 3 4 5 | unlist
1 2 3 4 5
```
## *take/drop/tail/head/last*
```bash
$ list 1 2 3 4 | drop 2
3
4
$ list 1 2 3 4 5 | head
1
$ list 1 2 3 4 | tail
2
3
4
$ list 1 2 3 4 5 | last
5
$ list 1 2 3 4 5 | take 2
1
2
```
## *join*
```bash
$ list 1 2 3 4 5 | join ,
1,2,3,4,5
$ list 1 2 3 4 5 | join , [ ]
[1,2,3,4,5]
```
## *map*
```bash
$ seq 1 5 | map λ a . 'echo $((a + 5))'
6
7
8
9
10
$ list a b s d e | map λ a . 'echo $a$(echo $a | tr a-z A-Z)'
aA
bB
sS
dD
eE
```
## *flat map*
```bash
$ seq 2 3 | map λ a . 'seq 1 $a' | join , [ ]
[1,2,1,2,3]
$ list a b c | map λ a . 'echo $a; echo $a | tr a-z A-z' | join , [ ]
[a,A,b,B,c,C]
```
## *filter*
```bash
$ seq 1 10 | filter λ a . '[[ $(mod $a 2) -eq 0 ]] && ret true || ret false'
2
4
6
8
10
```
## *foldl/foldr*
```bash
$ list a b c d | foldl λ acc el . 'echo -n $acc-$el'
a-b-c-d
$ list '' a b c d | foldr λ acc el .\
'if [[ ! -z $acc ]]; then echo -n $acc-$el; else echo -n $el; fi'
d-c-b-a
```
```bash
$ seq 1 4 | foldl λ acc el . 'echo $(($acc + $el))'
10
```
```bash
$ seq 1 4 | foldl λ acc el . 'echo $(mul $(($acc + 1)) $el)'
64 # 1 + (1 + 1) * 2 + (4 + 1) * 3 + (15 + 1) * 4 = 64
$ seq 1 4 | foldr λ acc el . 'echo $(mul $(($acc + 1)) $el)'
56 # 1 + (1 + 1) * 4 + (8 + 1) * 3 + (27 + 1) * 2 = 56
```
## *tup/tupx/tupl/tupr*
```bash
$ tup a 1
(a,1)
$ tup 'foo bar' 1 'one' 2
(foo bar,1,one,2)
$ tup , 1 3
(u002c,1,3)
```
```bash
$ tupl $(tup a 1)
a
$ tupr $(tup a 1)
1
$ tup , 1 3 | tupl
,
$ tup 'foo bar' 1 'one' 2 | tupl
foo bar
$ tup 'foo bar' 1 'one' 2 | tupr
2
```
```bash
$ tup 'foo bar' 1 'one' 2 | tupx 2
1
$ tup 'foo bar' 1 'one' 2 | tupx 1,3
foo bar
one
$ tup 'foo bar' 1 'one' 2 | tupx 2-4
1
one
2
```
## *buff*
```bash
$ seq 1 10 | buff λ a b . 'echo $(($a + $b))'
3
7
11
15
19
$ seq 1 10 | buff λ a b c d e . 'echo $(($a + $b + $c + $d + $e))'
15
40
```
## *zip*
```bash
$ list a b c d e f | zip $(seq 1 10)
(a,1)
(b,2)
(c,3)
(d,4)
(e,5)
(f,6)
```
```bash
$ list a b c d e f | zip $(seq 1 10) | last | tupr
6
```
## *curry*
```bash
add2() {
echo $(($1 + $2))
}
```
```bash
$ curry inc add2 1
```
```bash
$ inc 2
3
$ seq 1 3 | map λ a . 'inc $a'
2
3
4
```
## *try/catch*
```bash
$ echo 'expr 2 / 0' | try λ _ . 'echo 0'
0
$ echo 'expr 2 / 0' | try λ status . 'echo $status'
2
$ echo 'expr 2 / 2' | try λ _ . 'echo 0'
1
```
```bash
try λ _ . 'echo some errors during pull; exit 1' < <(echo git pull)
```
```bash
$ echo 'expr 2 / 0' \
| LANG=en catch λ cmd status val . 'echo cmd=$cmd,status=$status,val=$val'
cmd=expr 2 / 0,status=2,val=(expr:,division,by,zero)
```
```bash
$ echo 'expr 2 / 2' | catch λ _ _ val . 'tupl $val'
1
```
## *scanl*
```bash
$ seq 1 5 | scanl lambda acc el . 'echo $(($acc + $el))'
1
3
6
10
15
```
```bash
$ seq 1 5 | scanl lambda a b . 'echo $(($a + $b))' | last
15
```
## *with_trampoline/res/call*
```bash
factorial() {
fact_iter() {
local product=$1
local counter=$2
local max_count=$3
if [[ $counter -gt $max_count ]]; then
res $product
else
call fact_iter $(echo $counter\*$product | bc) $(($counter + 1)) $max_count
fi
}
with_trampoline fact_iter 1 1 $1
}
```
```bash
$ time factorial 30 | fold -w 70
265252859812191058636308480000000
real 0m1.854s
user 0m0.072s
sys 0m0.368s
```
```bash
time factorial 60 | fold -w 70
8320987112741390144276341183223364380754172606361245952449277696409600
000000000000
real 0m3.635s
user 0m0.148s
sys 0m0.692s
```
```bash
$ time factorial 90 | fold -w 70
1485715964481761497309522733620825737885569961284688766942216863704985
393094065876545992131370884059645617234469978112000000000000000000000
real 0m4.371s
user 0m0.108s
sys 0m0.436s
```
# Examples
```bash
processNames() {
uppercase() {
local str=$1
echo $(tr 'a-z' 'A-Z' <<< ${str:0:1})${str:1}
}
list $@ \
| filter λ name . '[[ ${#name} -gt 1 ]] && ret true || ret false' \
| map λ name . 'uppercase $name' \
| foldl λ acc el . 'echo $acc,$el'
}
processNames adam monika s slawek d daniel Bartek j k
```
```bash
Adam,Monika,Slawek,Daniel,Bartek
```
# Resources
* [Inspiration](https://quasimal.com/posts/2012-05-21-funsh.html)

View file

@ -58,3 +58,102 @@ add3 9
list a b c d | foldl lambda acc el . 'echo -n $acc-$el'
list '' a b c d | foldr lambda acc el . 'if [[ ! -z $acc ]]; then echo -n $acc-$el; else echo -n $el; fi'
seq 1 4 | foldl lambda acc el . 'echo $(($acc + $el))'
#1 - 2 - 3 - 4
seq 1 4 | foldl lambda acc el . 'echo $(($acc - $el))'
#1 - 4 - 3 - 2
seq 1 4 | foldr lambda acc el . 'echo $(($acc - $el))'
#1 + (1 + 1) * 2 + (4 + 1) * 3 + (15 + 1) * 4 = 64
seq 1 4 | foldl lambda acc el . 'echo $(mul $(($acc + 1)) $el)'
#1 + (1 + 1) * 4 + (8 + 1) * 3 + (27 + 1) * 2 = 56
seq 1 4 | foldr lambda acc el . 'echo $(mul $(($acc + 1)) $el)'
tup a 1
tupl $(tup a 1)
tupr $(tup a 1)
tup a 1 | tupl
tup a 1 | tupr
seq 1 10 | buff lambda a b . 'echo $(($a + $b))'
echo 'XX'
seq 1 10 | buff lambda a b c d e . 'echo $(($a + $b + $c + $d + $e))'
list a b c d e f | zip $(seq 1 10)
echo
list a b c d e f | zip $(seq 1 10) | last | tupr
arg='[key1=value1,key2=value2,key3=value3]'
get() {
local pidx=$1
local idx=$2
local arg=$3
echo $arg | tr -d '[]' | cut -d',' -f$idx | cut -d'=' -f$pidx
}
curry get_key get 1
curry get_value get 2
get_key 1 $arg
get_value 1 $arg
seq 1 3 | map lambda a . 'tup $(get_key $a $arg) $(get_value $a $arg)'
echo 'ls /home' | try λ cmd status ret . 'echo $cmd [$status]; echo $ret'
echo '/home' | try λ cmd status ret . 'echo $cmd [$status]; echo $ret'
seq 1 5 | scanl lambda a b . 'echo $(($a + $b))'
seq 1 5 | scanl lambda a b . 'echo $(($a + $b))' | last
seq 2 3 | map lambda a . 'seq 1 $a' | join , [ ]
list a b c | map lambda a . 'echo $a; echo $a | tr a-z A-z' | join , [ ]
echo 0 | cat - <(curl -s https://raw.githubusercontent.com/ssledz/bash-fun/v1.1.1/src/fun.sh) | \
map lambda a . 'list $a' | foldl lambda acc el . 'echo $(($acc + 1))'
echo 0 | cat - <(curl -s curl -s https://raw.githubusercontent.com/ssledz/bash-fun/v1.1.1/src/fun.sh) \
| foldl lambda acc el . 'echo $(($acc + 1))'
factorial() {
fact_iter() {
local product=$1
local counter=$2
local max_count=$3
if [[ $counter -gt $max_count ]]; then
echo $product
else
fact_iter $(echo $counter\*$product | bc) $(($counter + 1)) $max_count
fi
}
fact_iter 1 1 $1
}
factorial_trampoline() {
fact_iter() {
local product=$1
local counter=$2
local max_count=$3
if [[ $counter -gt $max_count ]]; then
res $product
else
call fact_iter $(echo $counter\*$product | bc) $(($counter + 1)) $max_count
fi
}
with_trampoline fact_iter 1 1 $1
}
echo Factorial test
time factorial 30
time factorial_trampoline 30
time factorial 60
time factorial_trampoline 60

47
release.sh Executable file
View file

@ -0,0 +1,47 @@
#!/bin/bash
version=$(cat .version)
release_version=${version%%-SNAPSHOT}
new_version=$(echo $release_version+0.1 | bc)
[[ $? -ne 0 ]] && echo 'Error exiting.' && exit 1
snapshot_version=${new_version}-SNAPSHOT
cat <<EOF
current version : $version
release version : $release_version
new snapshot version: $snapshot_version
EOF
git fetch
echo start the release by creating a new release branch
git checkout -b release/$release_version origin/develop
echo $release_version > ./.version
git add ./.version
git commit -m "[release] prepare release v$release_version"
git tag v$release_version
echo $snapshot_version > ./.version
git add ./.version
git commit -m "[release] prepare for next development iteration"
echo merge the version back into develop
git checkout develop
git merge --no-ff -m "[release] merge release/$release_version into develop" release/$release_version
git checkout master
echo merge the version back into master but use the tagged version instead of the release/$releaseVersion HEAD
git merge --no-ff -m "[release] merge previous version into master to avoid the increased version number" release/$release_version~1
echo get back on the develop branch
git checkout develop
echo finally push everything
git push origin develop master
git push --tags
echo removing the release branch
git branch -D release/$release_version

View file

@ -119,7 +119,7 @@ mul() {
( set -f; echo $(($1 * $2)) )
}
add() {
plus() {
echo $(($1 + $2))
}
@ -167,16 +167,22 @@ revers_str() {
cat - | splitc | revers | join
}
try() {
catch() {
local f="$@"
local cmd=$(cat -)
ret="$(2>&1 $cmd)"
local status=$?
list "$cmd" $status $(list $ret | join \#) | $f
local val=$(2>&1 eval "$cmd"; echo $?)
local cnt=$(list $val | wc -l)
local status=$(list $val | last)
$f < <(list "$cmd" $status $(list $val | take $((cnt - 1)) | unlist | tup))
}
try() {
local f="$@"
catch lambda cmd status val . '[[ $status -eq 0 ]] && tupx 1- $val | unlist || { '"$f"' < <(list $status); }'
}
ret() {
echo $1
echo $@
}
filter() {
@ -187,16 +193,26 @@ filter() {
done
}
stripl() {
local arg=$1
cat - | map lambda l . 'ret ${l##'$arg'}'
}
stripr() {
local arg=$1
cat - | map lambda l . 'ret ${l%%'$arg'}'
}
strip() {
local arg=$1
cat - | map lambda l . 'ret ${l##'$arg'}' | map lambda l . 'ret ${l%%'$arg'}'
cat - | stripl "$arg" | stripr "$arg"
}
buff() {
local cnt=-1
for x in $@; do
[[ $x = '.' ]] && break
cnt=$(add $cnt 1)
cnt=$(plus $cnt 1)
done
local args=''
local i=$cnt
@ -209,7 +225,13 @@ buff() {
}
tup() {
list "$@" | join , '(' ')'
if [[ $# -eq 0 ]]; then
local arg
read arg
tup $arg
else
list "$@" | map lambda x . 'echo ${x/,/u002c}' | join , '(' ')'
fi
}
tupx() {
@ -220,7 +242,7 @@ tupx() {
else
local n=$1
shift
list "$@" | strip '\(' | strip '\)' | unlist | cut -d',' -f${n}
echo "$@" | stripl '(' | stripr ')' | cut -d',' -f${n} | tr ',' '\n' | map lambda x . 'echo ${x/u002c/,}'
fi
}
@ -229,7 +251,7 @@ tupl() {
}
tupr() {
tupx 2 "$@"
tupx 1- "$@" | last
}
zip() {
@ -241,7 +263,7 @@ zip() {
done
}
function curry() {
curry() {
exportfun=$1; shift
fun=$1; shift
params=$*
@ -252,3 +274,25 @@ function curry() {
eval $cmd
}
with_trampoline() {
local f=$1; shift
local args=$@
while [[ $f != 'None' ]]; do
ret=$($f $args)
# echo $ret
f=$(tupl $ret)
args=$(echo $ret | tupx 2- | tr ',' ' ')
done
echo $args
}
res() {
local value=$1
tup "None" $value
}
call() {
local f=$1; shift
local args=$@
tup $f $args
}

15
test/append_test.sh Executable file
View file

@ -0,0 +1,15 @@
#! /bin/bash
testAppendToEmptyList() {
assertEquals 4 "$(list | append 4)"
}
testAppendToOneElementList() {
assertEquals "1 4" "$(list 1 | append 4 | unlist)"
}
testAppendToList() {
assertEquals "1 2 3 4 5 4" "$(list 1 2 3 4 5 | append 4 | unlist)"
}
. ./shunit2-init.sh

19
test/catch_test.sh Executable file
View file

@ -0,0 +1,19 @@
#! /bin/bash
testCatchIfSuccess() {
assertEquals 1 "$(echo 'expr 2 / 2' | catch lambda cmd status val . '[[ $status -eq 0 ]] && tupl $val || echo 0')"
}
testCatchIfError() {
assertEquals 0 $(echo 'expr 2 / 0' | catch lambda cmd status val . '[[ $status -eq 0 ]] && tupl $val || echo 0')
assertEquals 'cmd=expr 2 / 0,status=2,val=(expr:,division,by,zero)' "$(echo 'expr 2 / 0' | echo 'expr 2 / 0' | LANG=en catch lambda cmd status val . 'echo cmd=$cmd,status=$status,val=$val')"
}
testCatchEdgeCases() {
assertEquals 1 "$(echo 'expr 2 / 2' | catch lambda _ _ val . 'tupl $val')"
assertEquals 'expr 2 / 2' "$(echo 'expr 2 / 2' | catch lambda cmd . 'ret $cmd')"
assertEquals 'expr 2 / 2,0' "$(echo 'expr 2 / 2' | catch lambda cmd status . 'ret $cmd,$status')"
assertEquals 'expr 2 / 0,2' "$(echo 'expr 2 / 0' | catch lambda cmd status . 'ret $cmd,$status')"
}
. ./shunit2-init.sh

23
test/drop_test.sh Executable file
View file

@ -0,0 +1,23 @@
#! /bin/bash
testDrop9From10() {
assertEquals 10 $(list {1..10} | drop 9)
}
testDrop8From10() {
assertEquals "9 10" "$(list {1..10} | drop 8 | unlist)"
}
testDropAll() {
assertEquals "" "$(list {1..10} | drop 10)"
}
testDropMoreThanAvailable() {
assertEquals "" "$(list {1..10} | drop 15)"
}
testDropZero() {
assertEquals "1 2 3 4 5 6 7 8 9 10" "$(list {1..10} | drop 0 | unlist)"
}
. ./shunit2-init.sh

16
test/head_test.sh Executable file
View file

@ -0,0 +1,16 @@
#! /bin/bash
testHeadFromList() {
assertEquals 1 $(list {1..10} | head)
assertEquals 5 $(list 5 6 7 | head)
}
testHeadFromOneElementList() {
assertEquals 1 $(list 1 | head)
}
testHeadFromEmptyList() {
assertEquals "" "$(list | head)"
}
. ./shunit2-init.sh

38
test/lambda_test.sh Executable file
View file

@ -0,0 +1,38 @@
#! /bin/bash
testLambdaNoArguments_ifNoInput() {
assertEquals 'Hi there' "$(echo | lambda . 'echo Hi there')"
}
testLambdaNoArguments_ifSomeInputArguments() {
assertEquals 'Hi there' "$(echo 'xx\nyy\nzz' | lambda . 'echo Hi there')"
}
testLambdaOneArgument() {
identity() {
lambda x . '$x'
}
assertEquals "hi there !" "$(identity <<< 'echo hi there !')"
assertEquals 3 $(lambda x . 'echo $(($x + 1))' <<< '2')
assertEquals "hi there !" "$(λ x . 'echo $x' <<< 'hi there !')"
}
testLambdaSymbolTwoArguments() {
assertEquals 3 $(echo -e '1\n2' | lambda x y . 'echo $(($x + $y))')
assertEquals 5 $(echo -e '7\n2' | λ x y . 'echo $(($x - $y))')
}
testLambdaSymbolManyArguments() {
assertEquals 5 $(echo -e '1\n2\n3\n4\n5' | lambda a b c d e . 'echo $(($a + $b + $c + $d - $e))')
}
testLambdaSymbolManyArguments_ifInsufficientNumberOfArgumentsInLambda() {
assertEquals 6 $(echo -e '1\n2\n3\n4\n5' | lambda a b c . 'echo $(($a + $b + $c))')
}
testLambdaSymbolManyArguments_ifInsufficientNumberOfInputArguments() {
echo -e '1\n2' | lambda a b c d e . 'echo $(($a + $b + $c + $d + $e))' 2> /dev/null \
&& fail "There should be syntax error"
}
. ./shunit2-init.sh

16
test/last_test.sh Executable file
View file

@ -0,0 +1,16 @@
#! /bin/bash
testLastFromList() {
assertEquals 10 $(list {1..10} | last)
assertEquals 7 $(list 5 6 7 | last)
}
testLastFromOneElementList() {
assertEquals 1 $(list 1 | last)
}
testLastFromEmptyList() {
assertEquals "" "$(list | last)"
}
. ./shunit2-init.sh

1222
test/lib/shflags Normal file

File diff suppressed because it is too large Load diff

39
test/lib/shlib Normal file
View file

@ -0,0 +1,39 @@
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2008 Kate Ward. All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License).
#
# Author: kate.ward@forestent.com (Kate Ward)
#
# Library of shell functions.
# Convert a relative path into it's absolute equivalent.
#
# This function will automatically prepend the current working directory if the
# path is not already absolute. It then removes all parent references (../) to
# reconstruct the proper absolute path.
#
# Args:
# shlib_path_: string: relative path
# Outputs:
# string: absolute path
shlib_relToAbsPath()
{
shlib_path_=$1
# prepend current directory to relative paths
echo "${shlib_path_}" |grep '^/' >/dev/null 2>&1 \
|| shlib_path_="${PWD}/${shlib_path_}"
# clean up the path. if all seds supported true regular expressions, then
# this is what it would be:
shlib_old_=${shlib_path_}
while true; do
shlib_new_=`echo "${shlib_old_}" |sed 's/[^/]*\/\.\.\/*//;s/\/\.\//\//'`
[ "${shlib_old_}" = "${shlib_new_}" ] && break
shlib_old_=${shlib_new_}
done
echo "${shlib_new_}"
unset shlib_path_ shlib_old_ shlib_new_
}

251
test/lib/versions Executable file
View file

@ -0,0 +1,251 @@
#! /bin/sh
# vim:et:ft=sh:sts=2:sw=2
#
# Versions determines the versions of all installed shells.
#
# Copyright 2008-2017 Kate Ward. All Rights Reserved.
# Released under the Apache 2.0 License.
#
# Author: kate.ward@forestent.com (Kate Ward)
# https://github.com/kward/shlib
#
# This library provides reusable functions that determine actual names and
# versions of installed shells and the OS. The library can also be run as a
# script if set executable.
#
# Disable checks that aren't fully portable (POSIX != portable).
# shellcheck disable=SC2006
ARGV0=`basename "$0"`
LSB_RELEASE='/etc/lsb-release'
VERSIONS_SHELLS="ash /bin/bash /bin/dash /bin/ksh /bin/pdksh /bin/sh /bin/zsh"
true; TRUE=$?
false; FALSE=$?
ERROR=2
UNAME_R=`uname -r`
UNAME_S=`uname -s`
__versions_haveStrings=${ERROR}
versions_osName() {
os_name_='unrecognized'
os_system_=${UNAME_S}
os_release_=${UNAME_R}
case ${os_system_} in
CYGWIN_NT-*) os_name_='Cygwin' ;;
Darwin)
os_name_=`/usr/bin/sw_vers -productName`
os_version_=`versions_osVersion`
case ${os_version_} in
10.4|10.4.[0-9]*) os_name_='Mac OS X Tiger' ;;
10.5|10.5.[0-9]*) os_name_='Mac OS X Leopard' ;;
10.6|10.6.[0-9]*) os_name_='Mac OS X Snow Leopard' ;;
10.7|10.7.[0-9]*) os_name_='Mac OS X Lion' ;;
10.8|10.8.[0-9]*) os_name_='Mac OS X Mountain Lion' ;;
10.9|10.9.[0-9]*) os_name_='Mac OS X Mavericks' ;;
10.10|10.10.[0-9]*) os_name_='Mac OS X Yosemite' ;;
10.11|10.11.[0-9]*) os_name_='Mac OS X El Capitan' ;;
10.12|10.12.[0-9]*) os_name_='macOS Sierra' ;;
10.13|10.13.[0-9]*) os_name_='macOS High Sierra' ;;
*) os_name_='macOS' ;;
esac
;;
FreeBSD) os_name_='FreeBSD' ;;
Linux) os_name_='Linux' ;;
SunOS)
if grep 'OpenSolaris' /etc/release >/dev/null; then
os_name_='OpenSolaris'
else
os_name_='Solaris'
fi
;;
esac
echo ${os_name_}
unset os_name_ os_system_ os_release_ os_version_
}
versions_osVersion() {
os_version_='unrecognized'
os_system_=${UNAME_S}
os_release_=${UNAME_R}
case ${os_system_} in
CYGWIN_NT-*)
os_version_=`expr "${os_release_}" : '\([0-9]*\.[0-9]\.[0-9]*\).*'`
;;
Darwin)
os_version_=`/usr/bin/sw_vers -productVersion`
;;
FreeBSD)
os_version_=`expr "${os_release_}" : '\([0-9]*\.[0-9]*\)-.*'`
;;
Linux)
if [ -r '/etc/os-release' ]; then
os_version_=`awk -F= '$1~/PRETTY_NAME/{print $2}' /etc/os-release \
|sed 's/"//g'`
elif [ -r '/etc/redhat-release' ]; then
os_version_=`cat /etc/redhat-release`
elif [ -r '/etc/SuSE-release' ]; then
os_version_=`head -n 1 /etc/SuSE-release`
elif [ -r "${LSB_RELEASE}" ]; then
if grep -q 'DISTRIB_ID=Ubuntu' "${LSB_RELEASE}"; then
# shellcheck disable=SC2002
os_version_=`cat "${LSB_RELEASE}" \
|awk -F= '$1~/DISTRIB_DESCRIPTION/{print $2}' \
|sed 's/"//g;s/ /-/g'`
fi
fi
;;
SunOS)
if grep 'OpenSolaris' /etc/release >/dev/null; then
os_version_=`grep 'OpenSolaris' /etc/release |awk '{print $2"("$3")"}'`
else
major_=`echo "${os_release_}" |sed 's/[0-9]*\.\([0-9]*\)/\1/'`
minor_=`grep Solaris /etc/release |sed 's/[^u]*\(u[0-9]*\).*/\1/'`
os_version_="${major_}${minor_}"
fi
;;
esac
echo "${os_version_}"
unset os_name_ os_release_ os_version_ major_ minor_
}
versions_shellVersion() {
shell_=$1
shell_present_=${FALSE}
case "${shell_}" in
ash)
[ -x '/bin/busybox' ] && shell_present_=${TRUE}
;;
*)
[ -x "${shell_}" ] && shell_present_=${TRUE}
;;
esac
if [ ${shell_present_} -eq ${FALSE} ]; then
echo 'not installed'
return ${FALSE}
fi
version_=''
case ${shell_} in
*/sh)
# TODO(kward): fix this
## this could be one of any number of shells. try until one fits.
#version_=`versions_shell_bash ${shell_}`
## dash cannot be self determined yet
#[ -z "${version_}" ] && version_=`versions_shell_ksh ${shell_}`
## pdksh is covered in versions_shell_ksh()
#[ -z "${version_}" ] && version_=`versions_shell_zsh ${shell_}`
;;
ash) version_=`versions_shell_ash "${shell_}"` ;;
*/bash) version_=`versions_shell_bash "${shell_}"` ;;
*/dash)
# simply assuming Ubuntu Linux until somebody comes up with a better
# test. the following test will return an empty string if dash is not
# installed.
version_=`versions_shell_dash`
;;
*/ksh) version_=`versions_shell_ksh "${shell_}"` ;;
*/pdksh) version_=`versions_shell_pdksh "${shell_}"` ;;
*/zsh) version_=`versions_shell_zsh "${shell_}"` ;;
*) version_='invalid'
esac
echo "${version_:-unknown}"
unset shell_ version_
}
# The ash shell is included in BusyBox.
versions_shell_ash() {
busybox --help |head -1 |sed 's/BusyBox v\([0-9.]*\) .*/\1/'
}
versions_shell_bash() {
$1 --version 2>&1 |grep 'GNU bash' |sed 's/.*version \([^ ]*\).*/\1/'
}
versions_shell_dash() {
eval dpkg >/dev/null 2>&1
[ $? -eq 127 ] && return # return if dpkg not found
dpkg -l |grep ' dash ' |awk '{print $3}'
}
versions_shell_ksh() {
versions_shell_=$1
versions_version_=''
# Try a few different ways to figure out the version.
if versions_version_=`${versions_shell_} --version : 2>&1`; then
versions_version_=`echo "${versions_version_}" \
|sed 's/.*\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\).*/\1/'`
fi
if [ -z "${versions_version_}" ]; then
_versions_have_strings
versions_version_=`strings "${versions_shell_}" 2>&1 \
|grep Version \
|sed 's/^.*Version \(.*\)$/\1/;s/ s+ \$$//;s/ /-/g'`
fi
if [ -z "${versions_version_}" ]; then
versions_version_=`versions_shell_pdksh "${versions_shell_}"`
fi
echo "${versions_version_}"
unset versions_shell_ versions_version_
}
versions_shell_pdksh() {
_versions_have_strings
strings "$1" 2>&1 \
|grep 'PD KSH' \
|sed -e 's/.*PD KSH \(.*\)/\1/;s/ /-/g'
}
versions_shell_zsh() {
versions_shell_=$1
# Try a few different ways to figure out the version.
# shellcheck disable=SC2016
versions_version_=`echo 'echo ${ZSH_VERSION}' |${versions_shell_}`
if [ -z "${versions_version_}" ]; then
versions_version_=`${versions_shell_} --version 2>&1 |awk '{print $2}'`
fi
echo "${versions_version_}"
unset versions_shell_ versions_version_
}
# Determine if the 'strings' binary installed.
_versions_have_strings() {
[ ${__versions_haveStrings} -ne ${ERROR} ] && return
if eval strings /dev/null >/dev/null 2>&1; then
__versions_haveStrings=${TRUE}
return
fi
echo 'WARN: strings not installed. try installing binutils?' >&2
__versions_haveStrings=${FALSE}
}
versions_main() {
# Treat unset variables as an error.
set -u
os_name=`versions_osName`
os_version=`versions_osVersion`
echo "os: ${os_name} version: ${os_version}"
for shell in ${VERSIONS_SHELLS}; do
shell_version=`versions_shellVersion "${shell}"`
echo "shell: ${shell} version: ${shell_version}"
done
}
if [ "${ARGV0}" = 'versions' ]; then
versions_main "$@"
fi

25
test/list_test.sh Executable file
View file

@ -0,0 +1,25 @@
#! /bin/bash
testListFromOneElement() {
assertEquals 1 $(list 1)
}
testListFromEmpty() {
assertEquals "" "$(list)"
}
testListUnlist() {
assertEquals "1 3 6" "$(list 1 3 6 | unlist)"
}
testList() {
list=$(cat <<EOF
1
3
6
EOF
)
assertEquals "$list" "$(list 1 3 6)"
}
. ./shunit2-init.sh

33
test/map_test.sh Executable file
View file

@ -0,0 +1,33 @@
#!/bin/bash
testMapEmptyList() {
assertEquals "" "$(list | map lambda x . 'echo $(($x + 1))')"
}
testMapEmptyList_ifNoArgumentsInLambda() {
assertEquals "" "$(list | map lambda . 'echo 3')"
}
testMapOneElementList() {
assertEquals "3" "$(list 2 | map lambda x . 'echo $(($x + 1))')"
}
testMapList() {
assertEquals "2 3 4 5 6" "$(list {1..5} | map lambda x . 'echo $(($x + 1))' | unlist)"
}
testMapList_ifNoArgumentsInLambda() {
assertEquals "9 9 9 9 9" "$(list {1..5} | map lambda . 'echo 9' | unlist)"
}
testMapList_ifManyArgumentsInLambda() {
list {1..5} | map lambda x y . 'echo $(($x + $y))' 2> /dev/null \
&& fail "There should be syntax error, because map is an one argument operation"
}
testFlatMap() {
assertEquals "1 2 3 2 3 3" "$(list {1..3} | map lambda x . 'seq $x 3' | unlist)"
assertEquals "d e h l l l o o r w" "$(list hello world | map lambda x . 'command fold -w 1 <<< $x' | sort | unlist)"
}
. ./shunit2-init.sh

15
test/prepend_test.sh Executable file
View file

@ -0,0 +1,15 @@
#! /bin/bash
testPrependToEmptyList() {
assertEquals 4 "$(list | prepend 4)"
}
testPrependToOneElementList() {
assertEquals "4 1" "$(list 1 | prepend 4 | unlist)"
}
testPrependToList() {
assertEquals "4 1 2 3 4 5" "$(list 1 2 3 4 5 | prepend 4 | unlist)"
}
. ./shunit2-init.sh

1137
test/shunit2 Executable file

File diff suppressed because it is too large Load diff

8
test/shunit2-init.sh Normal file
View file

@ -0,0 +1,8 @@
#!/bin/bash
oneTimeSetUp() {
. ../src/fun.sh
}
# Load shUnit2.
. ./shunit2

15
test/tail_test.sh Executable file
View file

@ -0,0 +1,15 @@
#! /bin/bash
testTailFrom10() {
assertEquals "2 3 4 5 6 7 8 9 10" "$(list {1..10} | tail | unlist)"
}
testTailFromOneElementList() {
assertEquals "" "$(list 1 | tail)"
}
testTailFromEmptyList() {
assertEquals "" "$(list | tail)"
}
. ./shunit2-init.sh

23
test/take_test.sh Executable file
View file

@ -0,0 +1,23 @@
#! /bin/bash
testTake9From10() {
assertEquals "1 2 3 4 5 6 7 8 9" "$(list {1..10} | take 9 | unlist)"
}
testTake8From10() {
assertEquals "1 2 3 4 5 6 7 8" "$(list {1..10} | take 8 | unlist)"
}
testTakeAll() {
assertEquals "1 2 3 4 5 6 7 8 9 10" "$(list {1..10} | take 10 | unlist)"
}
testTakeMoreThanAvailable() {
assertEquals "1 2 3 4 5 6 7 8 9 10" "$(list {1..10} | take 15 | unlist)"
}
testTakeZero() {
assertEquals "" "$(list {1..10} | take 0 | unlist)"
}
. ./shunit2-init.sh

163
test/test_runner Executable file
View file

@ -0,0 +1,163 @@
#! /bin/sh
# vim:et:ft=sh:sts=2:sw=2
#
# Unit test suite runner.
#
# Copyright 2008-2017 Kate Ward. All Rights Reserved.
# Released under the Apache 2.0 license.
#
# Author: kate.ward@forestent.com (Kate Ward)
# https://github.com/kward/shlib
#
# This script runs all the unit tests that can be found, and generates a nice
# report of the tests.
#
### ShellCheck (http://www.shellcheck.net/)
# Disable source following.
# shellcheck disable=SC1090,SC1091
# expr may be antiquated, but it is the only solution in some cases.
# shellcheck disable=SC2003
# Return if test_runner already loaded.
[ -z "${RUNNER_LOADED:-}" ] || return 0
RUNNER_LOADED=0
RUNNER_ARGV0=$(basename "$0")
RUNNER_SHELLS='/bin/bash'
RUNNER_TEST_SUFFIX='_test.sh'
runner_warn() { echo "runner:WARN $*" >&2; }
runner_error() { echo "runner:ERROR $*" >&2; }
runner_fatal() { echo "runner:FATAL $*" >&2; exit 1; }
runner_usage() {
echo "usage: ${RUNNER_ARGV0} [-e key=val ...] [-s shell(s)] [-t test(s)]"
}
_runner_tests() { echo ./*${RUNNER_TEST_SUFFIX} |sed 's#./##g'; }
_runner_testName() {
# shellcheck disable=SC1117
_runner_testName_=$(expr "${1:-}" : "\(.*\)${RUNNER_TEST_SUFFIX}")
if [ -n "${_runner_testName_}" ]; then
echo "${_runner_testName_}"
else
echo 'unknown'
fi
unset _runner_testName_
}
main() {
# Find and load versions library.
for _runner_dir_ in . ${LIB_DIR:-lib}; do
if [ -r "${_runner_dir_}/versions" ]; then
_runner_lib_dir_="${_runner_dir_}"
break
fi
done
[ -n "${_runner_lib_dir_}" ] || runner_fatal 'Unable to find versions library.'
. "${_runner_lib_dir_}/versions" || runner_fatal 'Unable to load versions library.'
unset _runner_dir_ _runner_lib_dir_
# Process command line flags.
env=''
while getopts 'e:hs:t:' opt; do
case ${opt} in
e) # set an environment variable
key=$(expr "${OPTARG}" : '\([^=]*\)=')
val=$(expr "${OPTARG}" : '[^=]*=\(.*\)')
# shellcheck disable=SC2166
if [ -z "${key}" -o -z "${val}" ]; then
runner_usage
exit 1
fi
eval "${key}='${val}'"
eval "export ${key}"
env="${env:+${env} }${key}"
;;
h) runner_usage; exit 0 ;; # help output
s) shells=${OPTARG} ;; # list of shells to run
t) tests=${OPTARG} ;; # list of tests to run
*) runner_usage; exit 1 ;;
esac
done
shift "$(expr ${OPTIND} - 1)"
# Fill shells and/or tests.
shells=${shells:-${RUNNER_SHELLS}}
tests=${tests:-$(_runner_tests)}
# Error checking.
if [ -z "${tests}" ]; then
runner_error 'no tests found to run; exiting'
exit 1
fi
cat <<EOF
#------------------------------------------------------------------------------
# System data.
#
$ uname -mprsv
$(uname -mprsv)
OS Name: $(versions_osName)
OS Version: $(versions_osVersion)
### Test run info.
shells: ${shells}
tests: ${tests}
EOF
for key in ${env}; do
eval "echo \"${key}=\$${key}\""
done
# Run tests.
for shell in ${shells}; do
echo
cat <<EOF
#------------------------------------------------------------------------------
# Running the test suite with ${shell}.
#
EOF
# Check for existence of shell.
shell_bin=${shell}
shell_name=''
shell_present=${FALSE}
case ${shell} in
ash)
shell_bin=$(which busybox)
[ $? -eq "${TRUE}" ] && shell_present="${TRUE}"
shell_bin="${shell_bin} ash"
shell_name=${shell}
;;
*)
[ -x "${shell_bin}" ] && shell_present="${TRUE}"
shell_name=$(basename "${shell}")
;;
esac
if [ "${shell_present}" -eq "${FALSE}" ]; then
runner_warn "unable to run tests with the ${shell_name} shell"
continue
fi
shell_version=$(versions_shellVersion "${shell}")
echo "shell name: ${shell_name}"
echo "shell version: ${shell_version}"
# Execute the tests.
for t in ${tests}; do
echo
echo "--- Executing the '$(_runner_testName "${t}'")' test suite. ---"
# ${shell_bin} needs word splitting.
# shellcheck disable=SC2086
( exec ${shell_bin} "./${t}" 2>&1; )
done
done
}
# Execute main() if this is run in standalone mode (i.e. not from a unit test).
[ -z "${SHUNIT_VERSION}" ] && main "$@"

11
test/try_test.sh Executable file
View file

@ -0,0 +1,11 @@
#! /bin/bash
testTry() {
assertEquals 1 "$(echo 'expr 2 / 2' | try lambda _ . 'ret 0')"
assertEquals 0 "$(echo 'expr 2 / 0' | try lambda _ . 'ret 0')"
assertEquals 2 "$(echo 'expr 2 / 0' | try lambda status . 'ret $status')"
assertEquals 'already up to date' "$(echo 'echo already up to date' | try lambda _ . 'ret error')"
assertEquals 'error exit 1' "$(try λ _ . 'echo "error"; echo exit 1' < <(echo fgit pull) | unlist)"
}
. ./shunit2-init.sh

69
test/tup_test.sh Executable file
View file

@ -0,0 +1,69 @@
#! /bin/bash
testTupIfEmpty() {
assertEquals '()' $(tup '')
}
testTupIfOneElement() {
assertEquals '(1)' $(tup 1)
assertEquals '(")' $(tup '"')
assertEquals "(')" $(tup "'")
assertEquals "(u002c)" $(tup ",")
assertEquals "(()" $(tup "(")
assertEquals "())" $(tup ")")
}
testTupHappyPath() {
assertEquals '(1,2,3,4,5)' $(tup 1 2 3 4 5)
assertEquals '(a-1,b-2,c-3)' $(tup 'a-1' 'b-2' 'c-3')
assertEquals '(a b,c d e,f)' "$(tup 'a b' 'c d e' 'f')"
}
testTupxHappyPath() {
assertEquals '4' $(tup 4 5 1 4 | tupx 1)
assertEquals '5' $(tup 4 5 1 4 | tupx 2)
assertEquals '1' $(tup 4 5 1 4 | tupx 3)
assertEquals '4' $(tup 4 5 1 4 | tupx 4)
}
testTupxIfEmpty() {
assertEquals '' "$(tup '' | tupx 1)"
assertEquals '' "$(tup '' | tupx 5)"
}
testTupxIfZeroIndex() {
assertEquals '' "$(tup 1 3 | tupx 0 2>/dev/null)"
}
testTupxIfSpecialChars() {
assertEquals ',' "$(tup ',' | tupx 1)"
assertEquals '(' "$(tup '(' | tupx 1)"
assertEquals ')' "$(tup ')' | tupx 1)"
assertEquals '()' "$(tup '()' | tupx 1)"
assertEquals '(' "$(tup '(' ')' | tupx 1)"
assertEquals '(' "$(tup '(' '(' | tupx 1)"
assertEquals ')' "$(tup ')' ')' | tupx 1)"
assertEquals ',' "$(tup 'u002c' | tupx 1)"
}
testTupxRange() {
assertEquals '4 5' "$(tup 4 5 1 4 | tupx 1-2 | unlist)"
assertEquals '4 4' "$(tup 4 5 1 4 | tupx 1,4 | unlist)"
assertEquals '4 5 4' "$(tup 4 5 1 4 | tupx 1,2,4 | unlist)"
}
testTupl() {
assertEquals '4' "$(tup 4 5 | tupl)"
assertEquals '4' "$(tup 4 5 6 | tupl)"
assertEquals '6' "$(tup 6 | tupl)"
assertEquals 'foo bar' "$(tup 'foo bar' 1 'one' 2 | tupl)"
}
testTupr() {
assertEquals '5' "$(tup 4 5 | tupr)"
assertEquals '5' "$(tup 1 4 5 | tupr)"
assertEquals '5' "$(tup 5 | tupr)"
}
. ./shunit2-init.sh

21
test/unlist_test.sh Executable file
View file

@ -0,0 +1,21 @@
#! /bin/bash
testUnlistFromList() {
list=$(cat <<EOF
1
2
6
EOF
)
assertEquals "1 2 6" "$(echo $list | unlist)"
}
testUnlistFromEmptyList() {
assertEquals "" "$(echo | unlist)"
}
testUnlistFromOneElementList() {
assertEquals "1" "$(echo 1 | unlist)"
}
. ./shunit2-init.sh