Skip to content

Commit

Permalink
FIxed bug in zrun, added -127 arg
Browse files Browse the repository at this point in the history
  • Loading branch information
targendaz2 committed May 13, 2023
1 parent 6fff51a commit 556abf3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 49 deletions.
15 changes: 6 additions & 9 deletions src/zrun.bash
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
BATS_ZSH_WRAPPER="$(dirname "${BASH_SOURCE[0]}")/zsh_wrapper.sh"

zrun() {
CMD="$1"

[ -z "$CMD" ] && return 1
[ -z "$BATS_ZSH_SOURCE" ] && return 1
[ ! -x "$BATS_ZSH_WRAPPER" ] && return 1

"$BATS_ZSH_WRAPPER" "$BATS_ZSH_SOURCE" "$@"
return_code=$?
return $return_code
run_cmd='run'
if [ "$1" = '-127' ]; then
run_cmd+=' -127'
shift
fi
$run_cmd "$BATS_ZSH_WRAPPER" "$BATS_ZSH_SOURCE" "$@"
}
52 changes: 12 additions & 40 deletions test/zrun.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ bats_require_minimum_version 1.5.0
command=''

# When zrun is called with that empty string
run zrun "$command"
zrun "$command"

# Then zrun should fail
assert_failure
Expand All @@ -23,7 +23,7 @@ bats_require_minimum_version 1.5.0
assert_equal "$BATS_ZSH_SOURCE" ''

# When zrun is called
run zrun 'fake_command'
zrun -127 'fake_command'

# Then zrun should fail
assert_failure
Expand All @@ -34,40 +34,12 @@ bats_require_minimum_version 1.5.0
zsource 'test/assets/main.sh'

# When zrun is called
run -127 zrun 'fake_command'
zrun -127 'fake_command'

# Then zrun shouldn't fail
assert_not_equal $status 1
}

@test "zrun fails if BATS_ZSH_SOURCE doesn't exist" {
# Given BATS_ZSH_SOURCE doesn't exist
source_file='test/assets/nonexistent_main.sh'
run [ -e "$source_file" ]
assert_failure
run zsource "$source_file"

# When zrun is called
run zrun 'fake_command'

# Then zrun should fail
assert_failure
}

@test "zrun fails if BATS_ZSH_SOURCE isn't executable" {
# Given BATS_ZSH_SOURCE isn't executable
source_file='test/assets/nonexicutable_main.sh'
run [ -x "$source_file" ]
assert_failure
run zsource "$source_file"

# When zrun is called
run zrun 'fake_command'

# Then zrun should fail
assert_failure
}

@test "zrun fails if BATS_ZSH_WRAPPER doesn't exist" {
zsource 'test/assets/main.sh'

Expand All @@ -77,7 +49,7 @@ bats_require_minimum_version 1.5.0
assert_failure

# When zrun is called
run zrun 'fake_command'
zrun -127 'fake_command'

# Then zrun should fail
assert_failure
Expand All @@ -92,7 +64,7 @@ bats_require_minimum_version 1.5.0
assert_failure

# When zrun is called
run zrun 'fake_command'
zrun -127 fake_command

# Then zrun should fail
assert_failure
Expand All @@ -106,7 +78,7 @@ bats_require_minimum_version 1.5.0
assert_success

# When zrun is called
run -127 zrun 'fake_command'
zrun -127 fake_command

# Then zrun shouldn't fail
assert_not_equal $status 1
Expand All @@ -119,7 +91,7 @@ bats_require_minimum_version 1.5.0
function=successful_function

# When zrun is called with that function's name
run zrun $function
zrun $function

# Then zrun should succeed
assert_success
Expand All @@ -132,7 +104,7 @@ bats_require_minimum_version 1.5.0
function=failing_function

# When zrun is called with that function's name
run zrun $function
zrun $function

# Then zrun should fail
assert_failure
Expand All @@ -145,7 +117,7 @@ bats_require_minimum_version 1.5.0
function=successful_output_function

# When zrun is called with that function's name
run zrun $function
zrun $function

# Then the $output variable should contain that output
assert_equal "$output" "This is output"
Expand All @@ -158,7 +130,7 @@ bats_require_minimum_version 1.5.0
function=failing_output_function

# When zrun is called with that function's name
run zrun $function
zrun $function

# Then the $output variable should contain that output
assert_equal "$output" "This is a failing command"
Expand All @@ -171,7 +143,7 @@ bats_require_minimum_version 1.5.0
function=function_with_arg

# When zrun is called with that function's name
run zrun $function '1arg'
zrun $function '1arg'

# Then the $output variable should contain that output
assert_equal "$output" "arg was '1arg'"
Expand All @@ -184,7 +156,7 @@ bats_require_minimum_version 1.5.0
function=function_with_many_args

# When zrun is called with that function's name
run zrun $function 'arg1' 'arg2' 'arg3'
zrun $function 'arg1' 'arg2' 'arg3'

# Then the $output variable should contain that output
assert_equal "$output" "args were 'arg3' 'arg1' 'arg2'"
Expand Down

0 comments on commit 556abf3

Please sign in to comment.