Skip to content

Commit

Permalink
Merge pull request #17 from whiteinge/dashisms
Browse files Browse the repository at this point in the history
Fix dashisms; add test for jq filter with pipe
  • Loading branch information
whiteinge committed Sep 30, 2015
2 parents 879b665 + 36057af commit e319ff1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
42 changes: 29 additions & 13 deletions ok.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ help() {
#
# Positional arguments
#
local fname=$1
local fname="$1"
# Function name to search for; if omitted searches whole file.

if [ $# -gt 0 ]; then
Expand Down Expand Up @@ -148,12 +148,16 @@ __main() {
# -x | Enable xtrace debug logging.
# -y | Answer 'yes' to any prompts.

local cmd ret opt OPTARG OPTIND
local cmd
local ret
local opt
local OPTARG
local OPTIND
local quiet=0
local temp_dir="/tmp/oksh-${random}-${$}"
local summary_fifo="${temp_dir}/oksh_summary.fifo"
local random
random=$(hexdump -n 2 -e '/2 "%u"' /dev/urandom)
random="$(hexdump -n 2 -e '/2 "%u"' /dev/urandom)"

# shellcheck disable=SC2154
trap '
Expand Down Expand Up @@ -396,7 +400,7 @@ _filter_json() {
#
# * (stdin)
# JSON input.
local _filter=$1
local _filter="$1"
# A string of jq filters to apply to the input stream.

_log debug 'Filtering JSON.'
Expand Down Expand Up @@ -558,7 +562,10 @@ _request() {

shift 1

local cmd arg has_stdin trace_curl
local cmd
local arg
local has_stdin
local trace_curl

case $path in
(http*) : ;;
Expand Down Expand Up @@ -624,7 +631,12 @@ _response() {
# output in the same order as each argument; each on a single line. A
# blank line is output for headers that cannot be found.

local hdr val http_version status_code status_text headers output
local hdr
local val
local http_version
local status_code
local status_text
local headers output

_log debug 'Processing response.'

Expand Down Expand Up @@ -711,7 +723,9 @@ _get() {
# Maximum number of 'next' URLs to follow before stopping.
shift 1
local status_code status_text next_url
local status_code
local status_text
local next_url
# If the variable is unset or empty set it to a default value. Functions
# that call this function can pass these parameters in one of two ways:
Expand Down Expand Up @@ -961,7 +975,7 @@ list_repos() {
#
# Positional arguments
#
local user=$1
local user="$1"
# Optional GitHub user login or id for which to list repos.
#
# Keyword arguments
Expand Down Expand Up @@ -1014,7 +1028,8 @@ create_repo() {
_opts_filter "$@"
local url organization
local url
local organization
for arg in "$@"; do
case $arg in
Expand Down Expand Up @@ -1337,7 +1352,7 @@ list_issues() {
#
# Positional arguments
#
local repository=$1
local repository="$1"
# A GitHub repository.
#
# Keyword arguments
Expand All @@ -1354,7 +1369,8 @@ list_issues() {
# direction, since
shift 1
local url qs
local url
local qs
_opts_pagination "$@"
_opts_filter "$@"
Expand All @@ -1379,7 +1395,7 @@ user_issues() {
#
# Positional arguments
#
local repository=$1
local repository="$1"
# A GitHub repository.
#
# Keyword arguments
Expand Down Expand Up @@ -1447,7 +1463,7 @@ labels() {
#
# Positional arguments
#
local repo=$1
local repo="$1"
# A GitHub repository.
#
# Keyword arguments
Expand Down
20 changes: 20 additions & 0 deletions tests/unit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ test_filter_json_args() {
}
}

test_filter_json_pipe() {
# Test for issue #16.

local out
local json='[{"name": "Foo"}]'
local expected_out='Foo'

printf '%s\n' "$json" | $SCRIPT _filter_json '.[] | .["name"]' | {
read -r out

if [ "$expected_out" = "$out" ] ; then
return 0
else
printf 'Expected output does not match output: `%s` != `%s`\n' \
"$expected_out" "$out"
return 1
fi
}
}

test_response_headers() {
# Test that process response outputs headers in deterministic order.

Expand Down

0 comments on commit e319ff1

Please sign in to comment.