From 936a60c3e3a54b70472422bc122db92b9f5da410 Mon Sep 17 00:00:00 2001 From: Matthew Lyon Date: Sat, 27 Jan 2018 23:49:25 -0800 Subject: [PATCH 1/4] I hate everything --- lib/helpers/bake.sh | 14 +++++++++++++- test/help-bake.bats | 9 +++++++++ test/helpers.sh | 23 ++++++++++++++--------- 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 test/help-bake.bats diff --git a/lib/helpers/bake.sh b/lib/helpers/bake.sh index f4b0dc0..ecb5766 100644 --- a/lib/helpers/bake.sh +++ b/lib/helpers/bake.sh @@ -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" +} diff --git a/test/help-bake.bats b/test/help-bake.bats new file mode 100644 index 0000000..267eee0 --- /dev/null +++ b/test/help-bake.bats @@ -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'" ] +} diff --git a/test/helpers.sh b/test/helpers.sh index 0b4c005..5822ad4 100644 --- a/test/helpers.sh +++ b/test/helpers.sh @@ -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 <> $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 () { :; } @@ -36,4 +42,3 @@ respond_to () { bag set responders "$key" "$2" } return_with () { return $1; } - From 034f351f6e47091e17cba20947d60743d664fd67 Mon Sep 17 00:00:00 2001 From: Matthew Lyon Date: Sun, 28 Jan 2018 00:06:22 -0800 Subject: [PATCH 2/4] fix which output --- lib/helpers/status_codes.sh | 3 +-- lib/helpers/system.sh | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/helpers/status_codes.sh b/lib/helpers/status_codes.sh index 3e7cf82..afbce17 100644 --- a/lib/helpers/status_codes.sh +++ b/lib/helpers/status_codes.sh @@ -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 } - diff --git a/lib/helpers/system.sh b/lib/helpers/system.sh index d259ff6..8ebd1a4 100644 --- a/lib/helpers/system.sh +++ b/lib/helpers/system.sh @@ -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" @@ -44,4 +44,3 @@ baking_platform_is () { [ "$baking_platform" = $1 ] return $? } - From a43a13db85e7a94fda8643b1dd67b72038e247bd Mon Sep 17 00:00:00 2001 From: Matthew Lyon Date: Sun, 28 Jan 2018 13:38:43 -0800 Subject: [PATCH 3/4] string escape ok declarations through to git/github types --- lib/declarations/ok.sh | 17 ++++++++++------- types/git.sh | 24 ++++++++++++------------ types/github.sh | 4 ++-- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/declarations/ok.sh b/lib/declarations/ok.sh index 99b9973..9c5f8ac 100644 --- a/lib/declarations/ok.sh +++ b/lib/declarations/ok.sh @@ -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" ;; @@ -129,5 +134,3 @@ ok () { ;; esac } - - diff --git a/types/git.sh b/types/git.sh index b0160ae..213aa7b 100755 --- a/types/git.sh +++ b/types/git.sh @@ -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 @@ -20,8 +20,6 @@ else fi branch=$(arguments get branch $*) - - if [[ ! -z $branch ]]; then git_branch=$branch else @@ -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. @@ -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) @@ -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 @@ -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 @@ -105,4 +106,3 @@ case $action in *) return 1 ;; esac - diff --git a/types/github.sh b/types/github.sh index 3c04af9..b515b6d 100755 --- a/types/github.sh +++ b/types/github.sh @@ -30,14 +30,14 @@ case $action in repo=$1 shift fi - args="$*" + args="$@" if [ -n "$(arguments get ssh $*)" ]; then url="git@github.com:$(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 ;; From e5e41a8e13ca340fe51e51989fc1a2e560a93455 Mon Sep 17 00:00:00 2001 From: Matthew Lyon Date: Sun, 28 Jan 2018 14:29:59 -0800 Subject: [PATCH 4/4] fix git/github tests for quoting --- test/type-git.bats | 36 ++++++++++++++++++------------------ test/type-github.bats | 4 ++-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/test/type-git.bats b/test/type-git.bats index 0544b1b..b8bfa76 100755 --- a/test/type-git.bats +++ b/test/type-git.bats @@ -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 ] } @@ -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" @@ -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/mattly/code/bork ]" "return 1" - respond_to "ls -A /Users/mattly/code/bork" "dir_listing_handler" + respond_to " '[' '!' '-d' '/Users/mattly/code/bork' ']'" "return 1" + respond_to " 'ls' '-A' '/Users/mattly/code/bork'" "dir_listing_handler" git_status="## master" run git status /Users/mattly/code/bork git@github.com:mattly/bork [ "$status" -eq $STATUS_OK ] @@ -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/mattly/code/bork $repo [ "$status" -eq 0 ] run baked_output - [[ "mkdir -p /Users/mattly/code/bork" == ${lines[0]} ]] - [[ "git clone -b master $repo /Users/mattly/code/bork" == ${lines[1]} ]] + [[ " 'mkdir' '-p' '/Users/mattly/code/bork'" == ${lines[0]} ]] + [[ " 'git' 'clone' '-b' 'master' '$repo' '/Users/mattly/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]} ]] } diff --git a/test/type-github.bats b/test/type-github.bats index 7abc0d8..d0b5102 100755 --- a/test/type-github.bats +++ b/test/type-github.bats @@ -8,7 +8,7 @@ git_call="intercept_git" @test "github status: handles implicit target" { run github status mattly/bork - [ "$output" = "status https://github.com/mattly/bork.git" ] + [ "$output" = "status https://github.com/mattly/bork.git" ] } @test "github status: handles explicit target" { @@ -18,7 +18,7 @@ git_call="intercept_git" @test "github status: handles --ssh argument" { run github status mattly/bork --ssh - [ "$output" = "status git@github.com:mattly/bork.git" ] + [ "$output" = "status git@github.com:mattly/bork.git" ] } @test "github compile: outputs git type via include_assertion" {