Skip to content

Commit c19e8e3

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

File tree

4 files changed

+101
-101
lines changed

4 files changed

+101
-101
lines changed

lib/helpers.bash

-61
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,6 @@ else
2222
BASH_IT_SED_I_PARAMETERS=('-i' '')
2323
fi
2424

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

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

lib/utilities.bash

+36
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,42 @@ function _bash-it-egrep() {
7272
"${BASH_IT_GREP:-/usr/bin/grep}" -E "$@"
7373
}
7474

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

test/lib/helpers.bats

-40
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,6 @@ function local_setup() {
2020
assert_file_exist "$BASH_IT/profiles/test-bad-type.bash_it"
2121
}
2222

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

test/lib/utilities.bats

100644100755
+65
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,71 @@ function local_setup_file() {
66
setup_libs "helpers"
77
}
88

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

0 commit comments

Comments
 (0)