Skip to content

Commit

Permalink
Merge c1ba9c8 into c2098c4
Browse files Browse the repository at this point in the history
  • Loading branch information
mbland committed Nov 10, 2016
2 parents c2098c4 + c1ba9c8 commit 2fca194
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
19 changes: 11 additions & 8 deletions lib/internal/command_descriptions
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,16 @@ _@go.format_summary() {

_@go.command_summary() {
local cmd_path="$1"
local __go_cmd_name
__go_cmd_desc=''

if ! _@go.check_command_path "$cmd_path"; then
if ! _@go.check_command_path_and_parse_command_name "$cmd_path"; then
return 1
fi

local line
local summary=''
local summary_pattern='^# [[:alnum:]]'
local cmd_name="${1##*/}"
cmd_name="${cmd_name//\// }"

while read -r line; do
line="${line%$'\r'}"
Expand All @@ -103,9 +102,10 @@ _@go.command_summary() {
# way less hacky/more readable.
_@go.command_description() {
local cmd_path="$1"
local __go_cmd_name
__go_cmd_desc=''

if ! _@go.check_command_path "$cmd_path"; then
if ! _@go.check_command_path_and_parse_command_name "$cmd_path"; then
return 1
fi

Expand All @@ -116,8 +116,6 @@ _@go.command_description() {
local preformatted_pattern='^# *[[:graph:]]'
local summary_item
local summary
local cmd_name="${1##*/}"
cmd_name="${cmd_name//\// }"

while read -r line; do
line="${line%$'\r'}"
Expand Down Expand Up @@ -168,18 +166,23 @@ _@go.command_description() {

_@go.filter_description_line() {
line="${line//\{\{go\}\}/$_GO_CMD}"
line="${line//\{\{cmd\}\}/${cmd_name}}"
line="${line//\{\{cmd\}\}/${__go_cmd_name}}"
line="${line//\{\{root\}\}/$_GO_ROOTDIR}"
}

_@go.check_command_path() {
_@go.check_command_path_and_parse_command_name() {
local cmd_path="$1"
local subcommand_pattern='/([^/]+\.d/.*)'

if [[ -z "$cmd_path" ]]; then
echo "ERROR: no command script specified" >&2
return 1
elif [[ ! -e "$cmd_path" ]]; then
echo "ERROR: command script \"$cmd_path\" does not exist" >&2
return 1
elif [[ "$cmd_path" =~ $subcommand_pattern ]]; then
__go_cmd_name="${BASH_REMATCH[1]//.d\// }"
else
__go_cmd_name="${cmd_path##*/}"
fi
}
43 changes: 37 additions & 6 deletions tests/command-descriptions.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,54 @@

load environment
load assertions
load script_helper

setup() {
. 'lib/internal/command_descriptions'
}

@test "$SUITE: check command path passes" {
run _@go.check_command_path go
assert_success ''
teardown() {
rm -rf "$TEST_GO_ROOTDIR"
}

create_cmd_path_and_name_go_script() {
create_test_go_script \
". '$_GO_CORE_DIR/lib/internal/command_descriptions'" \
'declare __go_cmd_name' \
'if _@go.check_command_path_and_parse_command_name "$@"; then' \
' echo "__go_cmd_name: $__go_cmd_name"' \
'else' \
' exit 1' \
'fi'
}

@test "$SUITE: check command path and parse command name passes" {
create_cmd_path_and_name_go_script
create_test_command_script 'foo'
run "$TEST_GO_SCRIPT" "$TEST_GO_SCRIPTS_DIR/foo"
assert_success '__go_cmd_name: foo'
}

@test "$SUITE: check sub-command path and parse command name passes" {
create_cmd_path_and_name_go_script
create_test_command_script 'foo'
mkdir "$TEST_GO_SCRIPTS_DIR/foo.d"
create_test_command_script 'foo.d/bar'
mkdir "$TEST_GO_SCRIPTS_DIR/foo.d/bar.d"
create_test_command_script 'foo.d/bar.d/baz'
run "$TEST_GO_SCRIPT" "$TEST_GO_SCRIPTS_DIR/foo.d/bar.d/baz"
assert_success '__go_cmd_name: foo bar baz'
}

@test "$SUITE: check command path errors if no path is specified" {
run _@go.check_command_path
create_cmd_path_and_name_go_script
run "$TEST_GO_SCRIPT"
assert_failure 'ERROR: no command script specified'
}

@test "$SUITE: check command path errors if the path doesn't exist" {
run _@go.check_command_path foobar
create_cmd_path_and_name_go_script
run "$TEST_GO_SCRIPT" foobar
assert_failure 'ERROR: command script "foobar" does not exist'
}

Expand All @@ -34,7 +65,7 @@ setup() {

@test "$SUITE: filter description line" {
local _GO_CMD='test-go'
local cmd_name='test-command'
local __go_cmd_name='test-command'

local line='The script is {{go}}, '
line+='the command is {{cmd}}, and '
Expand Down
23 changes: 23 additions & 0 deletions tests/command-descriptions/from-file.bats
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,26 @@ Indented lines that look like tables (there are two or more adjacent spaces afte
# '[args...]' above.
assert_equal "$expected" "$__go_cmd_desc" 'command description'
}

@test "$SUITE: format subcommand description" {
mkdir -p "$TEST_GO_SCRIPTS_DIR/root-command.d/node-command.d"
create_test_command_script 'root-command.d/node-command.d/leaf-command' '#
# Leaf command that does something in {{root}}
#
# Usage: {{go}} {{cmd}} [args...]
echo The command script starts now.
'

local _GO_CMD='test-go'
local expected=("Leaf command that does something in $_GO_ROOTDIR"
''
"Usage: $_GO_CMD root-command node-command leaf-command [args...]"
'')
local __go_cmd_desc
_@go.command_description \
"$TEST_GO_SCRIPTS_DIR/root-command.d/node-command.d/leaf-command"

local IFS=$'\n'
assert_equal "${expected[*]}" "$__go_cmd_desc"
}

0 comments on commit 2fca194

Please sign in to comment.