Skip to content

Commit 561fa96

Browse files
committed
lib/helpers: remove log message from _command_exists() et al
- and move them to `lib/utilities`
1 parent 0717965 commit 561fa96

File tree

4 files changed

+101
-101
lines changed

4 files changed

+101
-101
lines changed

lib/helpers.bash

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,6 @@ case "$OSTYPE" in
1919
'darwin'*) BASH_IT_SED_I_PARAMETERS=('-i' '') ;;
2020
esac
2121

22-
function _command_exists() {
23-
_about 'checks for existence of a command'
24-
_param '1: command to check'
25-
_param '2: (optional) log message to include when command not found'
26-
_example '$ _command_exists ls && echo exists'
27-
_group 'lib'
28-
local msg="${2:-Command '$1' does not exist}"
29-
if type -t "$1" > /dev/null; then
30-
return 0
31-
else
32-
_log_debug "$msg"
33-
return 1
34-
fi
35-
}
36-
37-
function _binary_exists() {
38-
_about 'checks for existence of a binary'
39-
_param '1: binary to check'
40-
_param '2: (optional) log message to include when binary not found'
41-
_example '$ _binary_exists ls && echo exists'
42-
_group 'lib'
43-
local msg="${2:-Binary '$1' does not exist}"
44-
if type -P "$1" > /dev/null; then
45-
return 0
46-
else
47-
_log_debug "$msg"
48-
return 1
49-
fi
50-
}
51-
52-
function _completion_exists() {
53-
_about 'checks for existence of a completion'
54-
_param '1: command to check'
55-
_param '2: (optional) log message to include when completion is found'
56-
_example '$ _completion_exists gh && echo exists'
57-
_group 'lib'
58-
local msg="${2:-Completion for '$1' already exists}"
59-
if complete -p "$1" &> /dev/null; then
60-
_log_debug "$msg"
61-
return 0
62-
else
63-
return 1
64-
fi
65-
}
66-
6722
function _bash_it_homebrew_check() {
6823
if _binary_exists 'brew'; then
6924
# Homebrew is installed
@@ -200,22 +155,6 @@ function bash-it() {
200155
fi
201156
}
202157

203-
function _is_function() {
204-
_about 'sets $? to true if parameter is the name of a function'
205-
_param '1: name of alleged function'
206-
_param '2: (optional) log message to include when function not found'
207-
_group 'lib'
208-
_example '$ _is_function ls && echo exists'
209-
_group 'lib'
210-
local msg="${2:-Function '$1' does not exist}"
211-
if LC_ALL=C type -t "$1" | _bash-it-egrep -q 'function'; then
212-
return 0
213-
else
214-
_log_debug "$msg"
215-
return 1
216-
fi
217-
}
218-
219158
function _bash-it-aliases() {
220159
_about 'summarizes available bash_it aliases'
221160
_group 'lib'

lib/utilities.bash

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,42 @@ function _bash-it-egrep() {
7878
"${BASH_IT_GREP:-/usr/bin/grep}" -E "$@"
7979
}
8080

81+
function _command_exists() {
82+
: _about 'checks for existence of a command'
83+
: _param '1: command to check'
84+
: _example '$ _command_exists ls && echo exists'
85+
: _group 'lib'
86+
87+
type -t "${1?}" > /dev/null
88+
}
89+
90+
function _binary_exists() {
91+
: _about 'checks for existence of a binary'
92+
: _param '1: binary to check'
93+
: _example '$ _binary_exists ls && echo exists'
94+
: _group 'lib'
95+
96+
type -P "${1?}" > /dev/null
97+
}
98+
99+
function _completion_exists() {
100+
: _about 'checks for existence of a completion'
101+
: _param '1: command to check'
102+
: _example '$ _completion_exists gh && echo exists'
103+
: _group 'lib'
104+
105+
complete -p "${1?}" &> /dev/null
106+
}
107+
108+
function _is_function() {
109+
: _about 'sets $? to true if parameter is the name of a function'
110+
: _param '1: name of alleged function'
111+
: _example '$ _is_function ls && echo exists'
112+
: _group 'lib'
113+
114+
declare -F "${1?}" > /dev/null
115+
}
116+
81117
###########################################################################
82118
# Component-specific functions (component is either an alias, a plugin, or a
83119
# completion).

test/lib/helpers.bats

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,46 +26,6 @@ function local_setup {
2626
assert_file_exist "$BASH_IT/profiles/test-bad-type.bash_it"
2727
}
2828

29-
@test "helpers: _command_exists function exists" {
30-
run type -a _command_exists &> /dev/null
31-
assert_success
32-
}
33-
34-
@test "helpers: _command_exists function positive test ls" {
35-
run _command_exists ls
36-
assert_success
37-
}
38-
39-
@test "helpers: _command_exists function positive test bash-it" {
40-
run _command_exists bash-it
41-
assert_success
42-
}
43-
44-
@test "helpers: _command_exists function negative test" {
45-
run _command_exists __addfkds_dfdsjdf
46-
assert_failure
47-
}
48-
49-
@test "helpers: _binary_exists function exists" {
50-
run type -a _binary_exists &> /dev/null
51-
assert_success
52-
}
53-
54-
@test "helpers: _binary_exists function positive test ls" {
55-
run _binary_exists ls
56-
assert_success
57-
}
58-
59-
@test "helpers: _binary_exists function negative test function" {
60-
run _binary_exists _binary_exists
61-
assert_failure
62-
}
63-
64-
@test "helpers: _binary_exists function negative test" {
65-
run _binary_exists __addfkds_dfdsjdf
66-
assert_failure
67-
}
68-
6929
@test "helpers: bash-it help aliases ag" {
7030
run bash-it help aliases "ag"
7131
assert_line -n 0 "ag='ag --smart-case --pager=\"less -MIRFX'"

test/lib/utilities.bats

100644100755
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,71 @@ function local_setup {
77
setup_test_fixture
88
}
99

10+
@test "utilities: _is_function: _command_exists" {
11+
run _is_function _command_exists
12+
assert_success
13+
}
14+
15+
@test "utilities: _command_exists function positive test ls" {
16+
run _command_exists ls
17+
assert_success
18+
}
19+
20+
@test "utilities: _command_exists function positive test bash-it" {
21+
run _command_exists bash-it
22+
assert_success
23+
}
24+
25+
@test "utilities: _command_exists function negative test" {
26+
run _command_exists "__addfkds_dfdsjdf_${RANDOM:-}"
27+
assert_failure
28+
}
29+
30+
@test "utilities: _is_function: _binary_exists" {
31+
run _is_function _binary_exists
32+
assert_success
33+
}
34+
35+
@test "utilities: _binary_exists function positive test ls" {
36+
run _binary_exists ls
37+
assert_success
38+
}
39+
40+
@test "utilities: _binary_exists function negative test function" {
41+
run _binary_exists _binary_exists
42+
assert_failure
43+
}
44+
45+
@test "utilities: _binary_exists function negative test" {
46+
run _binary_exists "__addfkds_dfdsjdf_${RANDOM:-}"
47+
assert_failure
48+
}
49+
50+
@test "utilities: _is_function: _completion_exists" {
51+
run _is_function _completion_exists
52+
assert_success
53+
}
54+
55+
@test "utilities: _is_function new function" {
56+
local teh_new_func="__addfkds_dfdsjdf_${RANDOM:-}"
57+
run _is_function "${teh_new_func?}"
58+
assert_failure
59+
60+
cite "${teh_new_func?}"
61+
run _is_function "${teh_new_func?}"
62+
assert_success
63+
}
64+
65+
@test "utilities: _command_exists new function" {
66+
local teh_new_func="__addfkds_dfdsjdf_${RANDOM:-}"
67+
run _command_exists "${teh_new_func?}"
68+
assert_failure
69+
70+
cite "${teh_new_func?}"
71+
run _command_exists "${teh_new_func?}"
72+
assert_success
73+
}
74+
1075
@test "_bash-it-component-item-is-enabled() - for a disabled item" {
1176
run _bash-it-component-item-is-enabled aliases svn
1277
assert_failure

0 commit comments

Comments
 (0)