Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing list insert error under BSD sed #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion liblist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ list_push_back () {
#
list_insert () {
test "$#" -ne 3 && return 1
i="$2"; [ "$i" != '$' ] && i=$((i+1)); echo "$3" | sed "${i}i${1}"
i="$2"; [ "$i" != '$' ] && i=$((i+1)); echo "$3" | sed "${i}i\\
${1}
"
}

##
Expand Down
4 changes: 3 additions & 1 deletion liblist_unsafe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ list_push_back () {
#
list_insert () {
test "$#" -ne 3 && return 1; i="$3"; [ "$i" != '$' ] && i=$((i+1))
eval "$1=\"\$(echo \"\$$1\" | sed \"${i}i${2}\")\""
eval "$1=\"\$(echo \"\$$1\" | sed \"${i}i\\\\
${2}
\")\""
}

##
Expand Down
40 changes: 40 additions & 0 deletions tests/test_freebsd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh

# Test to make sure BSD sed and GNU sed work for
# list insertion. Assumes that both version of
# sed are present in PATH

set -e

. ./liblist.sh


insert_test() {
type sed
lst="$(list 'C' 'A' 'B')"
lst_expected="$(list 'D' 'C' 'A' 'B')"

lst="$(list_insert "D" 0 "$lst")"

if [ "$lst" == "$lst_expected" ]; then
echo "test: insert pass"
return 0
else
echo "test: insert fail"
return 1
fi
}

insert_test_gsed() {
# Keep gsed override wrapped
sed() {
gsed "$@"
}
insert_test
# Make sure this doesnt pollute other tests
unset -f sed
}

insert_test
echo '--------------------------'
insert_test_gsed
40 changes: 40 additions & 0 deletions tests/test_unsafe_freebsd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh

# Test to make sure BSD sed and GNU sed work for
# list insertion. Assumes that both version of
# sed are present in PATH

set -e

. ./liblist_unsafe.sh


insert_test() {
type sed
lst="$(list 'C' 'A' 'B')"
lst_expected="$(list 'D' 'C' 'A' 'B')"

list_insert lst "D" 0

if [ "$lst" == "$lst_expected" ]; then
echo "test: insert pass"
return 0
else
echo "test: insert fail"
return 1
fi
}

insert_test_gsed() {
# Keep gsed override wrapped
sed() {
gsed "$@"
}
# Make sure this doesnt pollute other tests
insert_test
unset -f sed
}

insert_test
echo '--------------------------'
insert_test_gsed