2017-10-28 07:54:10 -07:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
|
|
|
|
load test_helpers
|
|
|
|
|
2018-06-16 17:19:33 -07:00
|
|
|
banned_commands=(
|
2022-06-24 02:20:27 -07:00
|
|
|
# Process substitution isn't POSIX compliant and cause trouble
|
|
|
|
"<("
|
|
|
|
# Command isn't included in the Ubuntu packages asdf depends on. Also not
|
|
|
|
# defined in POSIX
|
|
|
|
column
|
|
|
|
# echo isn't consistent across operating systems, and sometimes output can
|
|
|
|
# be confused with echo flags. printf does everything echo does and more.
|
|
|
|
echo
|
|
|
|
# It's best to avoid eval as it makes it easier to accidentally execute
|
|
|
|
# arbitrary strings
|
|
|
|
eval
|
|
|
|
# realpath not available by default on OSX.
|
|
|
|
realpath
|
|
|
|
# readlink on OSX behaves differently from readlink on other Unix systems
|
|
|
|
readlink
|
|
|
|
# source isn't POSIX compliant. . behaves the same and is POSIX compliant
|
|
|
|
# Except in fish, where . is deprecated, and will be removed in the future.
|
|
|
|
source
|
2022-12-19 06:08:38 -07:00
|
|
|
# For consistency, [ should be used instead. There is a leading space so 'fail_test', etc. is not matched
|
|
|
|
' test'
|
2019-12-03 10:01:04 -07:00
|
|
|
)
|
2017-10-28 07:54:10 -07:00
|
|
|
|
2021-12-08 19:17:49 -07:00
|
|
|
banned_commands_regex=(
|
2022-06-24 02:20:27 -07:00
|
|
|
# grep -y does not work on alpine and should be "grep -i" either way
|
|
|
|
"grep.* -y"
|
|
|
|
# grep -P is not a valid option in OSX.
|
|
|
|
"grep.* -P"
|
|
|
|
# Ban grep long commands as they do not work on alpine
|
|
|
|
"grep[^|]+--\w{2,}"
|
|
|
|
# sort --sort-version isn't supported everywhere
|
|
|
|
"sort.*-V"
|
|
|
|
"sort.*--sort-versions"
|
2021-12-29 10:10:39 -07:00
|
|
|
|
2022-06-24 02:20:27 -07:00
|
|
|
# ls often gets used when we want to glob for files that match a pattern
|
|
|
|
# or when we want to find all files/directories that match a pattern or are
|
|
|
|
# found in a certain location. Using shell globs is preferred over ls, and
|
|
|
|
# find is better at locating files that are in a certain location or that
|
|
|
|
# match certain filename patterns.
|
|
|
|
# https://github-wiki-see.page/m/koalaman/shellcheck/wiki/SC2012
|
|
|
|
'\bls '
|
2022-05-06 06:21:06 -07:00
|
|
|
|
2022-07-05 06:40:33 -07:00
|
|
|
# Ban recursive asdf calls as they are inefficient and may introduce bugs.
|
2022-06-24 02:20:27 -07:00
|
|
|
# If you find yourself needing to invoke an `asdf` command from within
|
|
|
|
# asdf code, please source the appropriate file and invoke the
|
|
|
|
# corresponding function.
|
|
|
|
'\basdf '
|
2021-12-08 19:17:49 -07:00
|
|
|
)
|
|
|
|
|
2017-10-28 07:54:10 -07:00
|
|
|
setup() {
|
|
|
|
setup_asdf_dir
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
|
|
|
clean_asdf_dir
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "banned commands are not found in source code" {
|
2021-12-08 19:17:49 -07:00
|
|
|
# Assert command is not used in the lib and bin dirs
|
|
|
|
# or expect an explicit comment at end of line, allowing it.
|
|
|
|
# Also ignore matches that are contained in comments or a string or
|
|
|
|
# followed by an underscore (indicating it's a variable and not a
|
|
|
|
# command).
|
2017-10-28 07:54:10 -07:00
|
|
|
for cmd in "${banned_commands[@]}"; do
|
2022-06-24 02:20:27 -07:00
|
|
|
run bash -c "grep -nHR --include \*.bash --include \*.sh '$cmd' asdf.* lib bin\
|
2021-05-21 09:40:20 -07:00
|
|
|
| grep -v '#.*$cmd'\
|
2021-05-24 14:30:51 -07:00
|
|
|
| grep -v '\".*$cmd.*\"' \
|
2021-05-21 09:40:20 -07:00
|
|
|
| grep -v '${cmd}_'\
|
|
|
|
| grep -v '# asdf_allow: $cmd'"
|
|
|
|
|
2022-06-24 02:20:27 -07:00
|
|
|
# Only print output if we've found a banned command
|
|
|
|
#if [ "$status" -ne 1 ]; then
|
|
|
|
if [ "" != "$output" ]; then
|
|
|
|
echo "banned command $cmd: $output"
|
|
|
|
fi
|
2021-12-08 19:17:49 -07:00
|
|
|
|
2022-06-24 02:20:27 -07:00
|
|
|
[ "$status" -eq 1 ]
|
2023-01-14 06:18:44 -07:00
|
|
|
[ "" = "$output" ]
|
2021-12-08 19:17:49 -07:00
|
|
|
done
|
|
|
|
|
|
|
|
for cmd in "${banned_commands_regex[@]}"; do
|
2022-06-24 02:20:27 -07:00
|
|
|
run bash -c "grep -nHRE --include \*.bash --include \*.sh '$cmd' asdf.* lib bin\
|
2021-12-08 19:17:49 -07:00
|
|
|
| grep -v '#.*$cmd'\
|
|
|
|
| grep -v '\".*$cmd.*\"' \
|
|
|
|
| grep -v '${cmd}_'\
|
|
|
|
| grep -v '# asdf_allow: $cmd'"
|
|
|
|
|
2022-06-24 02:20:27 -07:00
|
|
|
# Only print output if we've found a banned command
|
|
|
|
#if [ "$status" -ne 1 ]; then
|
|
|
|
if [ "" != "$output" ]; then
|
|
|
|
echo "banned command $cmd: $output"
|
|
|
|
fi
|
2021-05-21 09:40:20 -07:00
|
|
|
|
2022-06-24 02:20:27 -07:00
|
|
|
[ "$status" -eq 1 ]
|
2023-01-14 06:18:44 -07:00
|
|
|
[ "" = "$output" ]
|
2017-10-28 07:54:10 -07:00
|
|
|
done
|
|
|
|
}
|