Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ban ls command #1141

Merged
merged 5 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/commands/command-install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ install_local_tool_versions() {

# Locate all the plugins installed in the system
local plugins_installed
if ls "$plugins_path" &>/dev/null; then
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
for plugin_path in "$plugins_path"/*; do
local plugin_name
plugin_name=$(basename "$plugin_path")
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-latest.bash
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ latest_all() {
local plugins_path
plugins_path=$(get_plugin_path)

if ls "$plugins_path" &>/dev/null; then
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
for plugin_path in "$plugins_path"/*; do
plugin_name=$(basename "$plugin_path")

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-list.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ list_command() {
local plugins_path
plugins_path=$(get_plugin_path)

if ls "$plugins_path" &>/dev/null; then
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
for plugin_path in "$plugins_path"/*; do
plugin_name=$(basename "$plugin_path")
printf "%s\\n" "$plugin_name"
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-plugin-list-all.bash
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugin_list_all_command() {
local plugins_local_path
plugins_local_path="$(get_plugin_path)"

if ls "$plugins_index_path" &>/dev/null; then
if find "$plugins_index_path" -mindepth 1 -type d &>/dev/null; then
(
for index_plugin in "$plugins_index_path"/*; do
index_plugin_name=$(basename "$index_plugin")
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-plugin-list.bash
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ plugin_list_command() {
esac
done

if ls "$plugins_path" &>/dev/null; then
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
(
for plugin_path in "$plugins_path"/*; do
plugin_name=$(basename "$plugin_path")
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-reshim.bash
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ reshim_command() {
local plugins_path
plugins_path=$(get_plugin_path)

if ls "$plugins_path" &>/dev/null; then
if find "$plugins_path" -mindepth 1 -type d &>/dev/null; then
for plugin_path in "$plugins_path"/*; do
plugin_name=$(basename "$plugin_path")
reshim_command "$plugin_name"
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ resolve_symlink() {
# This seems to be the only cross-platform way to resolve symlink paths to
# the real file path.
# shellcheck disable=SC2012
resolved_path=$(ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|')
resolved_path=$(ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|') # asdf_allow: ls '
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not yet found a better cross-platform solution to this.


# Check if resolved path is relative or not by looking at the first character.
# If it is a slash we can assume it's root and absolute. Otherwise we treat it
Expand Down
12 changes: 10 additions & 2 deletions test/banned_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ banned_commands_regex=(
# sort --sort-version isn't supported everywhere
"sort.*-V"
"sort.*--sort-versions"

# 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 '
)

setup() {
Expand All @@ -50,7 +58,7 @@ teardown() {
# followed by an underscore (indicating it's a variable and not a
# command).
for cmd in "${banned_commands[@]}"; do
run bash -c "grep -nHR '$cmd' asdf.* lib bin\
run bash -c "grep -nHR --include \*.bash --include \*.sh '$cmd' asdf.* lib bin\
| grep -v '#.*$cmd'\
| grep -v '\".*$cmd.*\"' \
| grep -v '${cmd}_'\
Expand All @@ -67,7 +75,7 @@ teardown() {
done

for cmd in "${banned_commands_regex[@]}"; do
run bash -c "grep -nHRE '$cmd' asdf.* lib bin\
run bash -c "grep -nHRE --include \*.bash --include \*.sh '$cmd' asdf.* lib bin\
| grep -v '#.*$cmd'\
| grep -v '\".*$cmd.*\"' \
| grep -v '${cmd}_'\
Expand Down