fix: warn if .tool-versions or asdfrc contains carriage returns (#1561)

This commit is contained in:
Edwin Kofler 2023-05-28 06:36:53 -07:00 committed by GitHub
parent 8e2aa5d726
commit 097f7733d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -373,6 +373,8 @@ get_asdf_config_value_from_file() {
return 1
fi
util_validate_no_carriage_returns "$config_path"
local result
result=$(grep -E "^\s*$key\s*=\s*" "$config_path" | head | sed -e 's/^[^=]*= *//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "$result" ]; then
@ -465,6 +467,8 @@ find_file_upwards() {
search_path=$PWD
while [ "$search_path" != "/" ]; do
if [ -f "$search_path/$name" ]; then
util_validate_no_carriage_returns "$search_path/$name"
printf "%s\n" "${search_path}/$name"
return 0
fi
@ -863,6 +867,15 @@ util_resolve_user_path() {
fi
}
# @description Check if a file contains carriage returns. If it does, print a warning.
util_validate_no_carriage_returns() {
local file_path="$1"
if grep -qr $'\r' "$file_path"; then
printf '%s\n' "asdf: Warning: File $file_path contains carriage returns. Please remove them." >&2
fi
}
get_plugin_remote_url() {
local plugin_name="$1"
local plugin_path

View File

@ -460,3 +460,16 @@ EOF
[ "$status" -eq 0 ]
[ "$output" = "$message" ]
}
@test "prints warning if .tool-versions file has carriage returns" {
ASDF_CONFIG_FILE="$BATS_TEST_TMPDIR/asdfrc"
cat >"$ASDF_CONFIG_FILE" <<<$'key2 = value2\r'
[[ "$(get_asdf_config_value "key1" 2>&1)" = *"contains carriage returns"* ]]
}
@test "prints if asdfrc config file has carriage returns" {
cat >".tool-versions" <<<$'nodejs 19.6.0\r'
[[ "$(find_tool_versions 2>&1)" = *"contains carriage returns"* ]]
}