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

(bug) (wip) Bork's eval/bake breaks when there are arguments with whitespaces #13

Open
wants to merge 5 commits into
base: main
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
17 changes: 10 additions & 7 deletions lib/declarations/ok.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ ok () {
echo "not found: $assertion" 1>&2
return 1
fi
argstr=$*
quoted_argstr=
while [ -n "$1" ]; do
quoted_argstr=$(echo "$quoted_argstr '$1'")
shift
argstr=$@
quoted_argstr=''
for i in "$@"; do
case "$i" in
*\'*)
i=`printf "%s" "$i" | sed "s/'/'\"'\"'/g"`
;;
*) : ;;
esac
quoted_argstr="$quoted_argstr '$i'"
done
case $operation in
echo) echo "$fn $argstr" ;;
Expand Down Expand Up @@ -129,5 +134,3 @@ ok () {
;;
esac
}


14 changes: 13 additions & 1 deletion lib/helpers/bake.sh
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
bake () { eval "$*"; }
bake () {
C=''
for i in "$@"; do
case "$i" in
*\'*)
i=`printf "%s" "$i" | sed "s/'/'\"'\"'/g"`
;;
*) : ;;
esac
C="$C '$i'"
done
eval "$C"
}
3 changes: 1 addition & 2 deletions lib/helpers/status_codes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ _status_for () {
$STATUS_FAILED_ARGUMENTS) echo "error (failed arguments)" ;;
$STATUS_FAILED_ARGUMENT_PRECONDITION) echo "error (failed argument precondition)" ;;
$STATUS_FAILED_PRECONDITION) echo "error (failed precondition)" ;;
$STATUS_UNSUPPORTED_PLATFORM) echo "error (unsupported platform)" ;;
$STATUS_UNSUPPORTED_PLATFORM) echo "error (unsupported platform $baking_platform)" ;;
*) echo "unknown status: $1" ;;
esac
}

3 changes: 1 addition & 2 deletions lib/helpers/system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ needs_exec () {
[ -z "$2" ] && running_status=0 || running_status=$2

# was seeing some weirdness on this where $1 would have carraige returns sometimes, so it's quoted.
path=$(bake "which $1")
path=$(bake which "$1")

if [ "$?" -gt 0 ]; then
echo "missing required exec: $1"
Expand Down Expand Up @@ -44,4 +44,3 @@ baking_platform_is () {
[ "$baking_platform" = $1 ]
return $?
}

9 changes: 9 additions & 0 deletions test/help-bake.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bats

. test/helpers.sh

@test "bake will escape its arguments before eval" {
run bake ls -la "foo bar"
run baked_output
[ "$output" = " 'ls' '-la' 'foo bar'" ]
}
23 changes: 14 additions & 9 deletions test/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ p () {
md5c=$(md5cmd $platform)
baking_responder=
baking_file=$(mktemp -t bork_test.XXXXXX)
bake () {
echo "$*" >> $baking_file;
key=$(echo "$*" | eval $md5c)
handler=$(bag get responders $key)
p "looking up $* at $key, found $handler"
if [ -n "$handler" ]; then
eval $handler
NEW_BAKE_FN=$(cat <<HERE
bake ()
$(declare -f bake | tail -n +2 | sed -E 's|eval "\$C"||' | sed -e 's|^}$||')
echo "\$C" >> $baking_file
key=\$(echo "\$C" | eval \$md5c)
handler=\$(bag get responders \$key)
p "looking up \$C at \$key, found \$handler"
if [ -n "\$handler" ]; then
eval \$handler
else
baking_responder $*
baking_responder \$C
fi
return
}
HERE
)
eval "$NEW_BAKE_FN"

# overwrite this in your tests
baking_responder () { :; }

Expand All @@ -36,4 +42,3 @@ respond_to () {
bag set responders "$key" "$2"
}
return_with () { return $1; }

36 changes: 18 additions & 18 deletions test/type-git.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ dir_exists=1
dir_listing=$(echo 'foo'; echo 'bar')
git_status=
setup () {
respond_to "[ ! -d bork ]" "dir_exists_handler"
respond_to "ls -A bork" "dir_listing_handler"
respond_to "git status -uno -b --porcelain" "git_status_handler"
respond_to " '[' '!' '-d' 'bork' ']'" "dir_exists_handler"
respond_to " 'ls' '-A' 'bork'" "dir_listing_handler"
respond_to " 'git' 'status' '-uno' '-b' '--porcelain'" "git_status_handler"
}
dir_exists_handler () { [ "$dir_exists" -eq 0 ]; }
dir_listing_handler () { echo "$dir_listing"; }
git_status_handler () { echo "$git_status"; }

@test "git status: returns FAILED_PRECONDITION when git exec is missing" {
respond_to "which git" "return 1"
respond_to " 'which' 'git'" "return 1"
run git status $repo
[ "$status" -eq $STATUS_FAILED_PRECONDITION ]
}
Expand All @@ -41,7 +41,7 @@ git_status_handler () { echo "$git_status"; }
}

@test "git status: returns CONFLICT_CLOBBER when the directory is not empty and not a git repository" {
respond_to "git fetch" "return_with 1"
respond_to " 'git' 'fetch'" "return_with 1"
run git status $repo
[ "$status" -eq $STATUS_CONFLICT_CLOBBER ]
echo "$output" | grep -E "bork exists"
Expand Down Expand Up @@ -92,8 +92,8 @@ git_status_handler () { echo "$git_status"; }
}

@test "git status: returns OK with directory argument and repo is current" {
respond_to "[ ! -d /Users/skylar/code/bork ]" "return 1"
respond_to "ls -A /Users/skylar/code/bork" "dir_listing_handler"
respond_to " '[' '!' '-d' '/Users/skylar/code/bork' ']'" "return 1"
respond_to " 'ls' '-A' '/Users/skylar/code/bork'" "dir_listing_handler"
git_status="## master"
run git status /Users/skylar/code/bork [email protected]:skylarmacdonald/bork
[ "$status" -eq $STATUS_OK ]
Expand All @@ -103,33 +103,33 @@ git_status_handler () { echo "$git_status"; }
run git install $repo
[ "$status" -eq 0 ]
run baked_output
[[ "mkdir -p bork" == ${lines[0]} ]]
[[ "git clone -b master $repo bork" == ${lines[1]} ]]
[[ " 'mkdir' '-p' 'bork'" == ${lines[0]} ]]
[[ " 'git' 'clone' '-b' 'master' '$repo' 'bork'" == ${lines[1]} ]]
}

@test "git install: with target argument, performs clone" {
run git install /Users/skylar/code/bork $repo
[ "$status" -eq 0 ]
run baked_output
[[ "mkdir -p /Users/skylar/code/bork" == ${lines[0]} ]]
[[ "git clone -b master $repo /Users/skylar/code/bork" == ${lines[1]} ]]
[[ " 'mkdir' '-p' '/Users/skylar/code/bork'" == ${lines[0]} ]]
[[ " 'git' 'clone' '-b' 'master' '$repo' '/Users/skylar/code/bork'" == ${lines[1]} ]]
}

@test "git install: uses specified branch" {
run git install $repo --branch=experimental
[ "$status" -eq 0 ]
run baked_output
[[ "mkdir -p bork" == ${lines[0]} ]]
[[ "git clone -b experimental $repo bork" == ${lines[1]} ]]
[[ " 'mkdir' '-p' 'bork'" == ${lines[0]} ]]
[[ " 'git' 'clone' '-b' 'experimental' '$repo' 'bork'" == ${lines[1]} ]]
}

@test "git upgrade: merges to new ref, echoes changelog" {
run git upgrade $repo
[ "$status" -eq 0 ]
run baked_output
[[ "cd bork" == ${lines[0]} ]]
[[ "git reset --hard" == ${lines[1]} ]]
[[ "git pull" == ${lines[2]} ]]
[[ "git checkout master" == ${lines[3]} ]]
[[ "git log HEAD@{2}.." == ${lines[4]} ]]
[[ " 'cd' 'bork'" == ${lines[0]} ]]
[[ " 'git' 'reset' '--hard'" == ${lines[1]} ]]
[[ " 'git' 'pull'" == ${lines[2]} ]]
[[ " 'git' 'checkout' 'master'" == ${lines[3]} ]]
[[ " 'git' 'log' 'HEAD@{2}..'" == ${lines[4]} ]]
}
24 changes: 12 additions & 12 deletions types/git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ git_url=$2
shift 2
next=$1
if [ -n "$next" ] && [ ${next:0:1} != '-' ]; then
target_dir=$git_url
target_dir="$git_url"
git_url=$1
shift
else
Expand All @@ -20,8 +20,6 @@ else
fi

branch=$(arguments get branch $*)


if [[ ! -z $branch ]]; then
git_branch=$branch
else
Expand All @@ -39,13 +37,13 @@ case $action in
needs_exec "git" || return $STATUS_FAILED_PRECONDITION

# if the directory is missing, it's missing
bake [ ! -d $target_dir ] && return $STATUS_MISSING
bake [ ! -d "$target_dir" ] && return $STATUS_MISSING

# if the directory is present but empty, it's missing
target_dir_contents=$(str_item_count "$(bake ls -A $target_dir)")
target_dir_contents=$(str_item_count $(bake ls -A "$target_dir"))
[ "$target_dir_contents" -eq 0 ] && return $STATUS_MISSING

bake cd $target_dir
bake cd "$target_dir"
# fetch from the remote without fast-forwarding
# this *does* change the local repository's pointers and takes longer
# up front, but I believe in the grand scheme is the right thing to do.
Expand All @@ -63,6 +61,7 @@ case $action in
fi

git_stat=$(bake git status -uno -b --porcelain)
echo "$git_stat"
git_first_line=$(echo "$git_stat" | head -n 1)

git_divergence=$(str_get_field "$git_first_line" 3)
Expand All @@ -77,9 +76,11 @@ case $action in
return $STATUS_CONFLICT_UPGRADE
fi

str_matches "$(str_get_field "$git_first_line" 2)" "$git_branch"
echo "porcelain: ${git_first_line}"
current_git_branch=$(str_get_field "$git_first_line" 2)
str_matches "$current_git_branch" "$git_branch"
if [ "$?" -ne 0 ]; then
echo "local git repository is on incorrect branch"
echo "local git repository is on incorrect branch: $current_git_branch"
return $STATUS_MISMATCH_UPGRADE
fi

Expand All @@ -90,12 +91,12 @@ case $action in
return $STATUS_OK ;;

install)
bake mkdir -p $target_dir
bake git clone -b $git_branch $git_url $target_dir
bake mkdir -p "$target_dir"
bake git clone -b "$git_branch" "$git_url" "$target_dir"
;;

upgrade)
bake cd $target_dir
bake cd "$target_dir"
bake git reset --hard
bake git pull
bake git checkout $git_branch
Expand All @@ -105,4 +106,3 @@ case $action in

*) return 1 ;;
esac

4 changes: 2 additions & 2 deletions types/github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ case $action in
repo=$1
shift
fi
args="$*"
args="$@"
if [ -n "$(arguments get ssh $*)" ]; then
url="[email protected]:$(echo $repo).git"
args=$(echo "$args" | sed -E 's|--ssh||')
else
url="https://github.com/$(echo $repo).git"
fi
eval "$git_call $action $target_dir $url $args"
eval "$git_call $action '$target_dir' '$url' $args"
;;

*) return 1 ;;
Expand Down