diff --git a/lib/utils.sh b/lib/utils.sh index a50cad74..1d380eb5 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -378,5 +378,17 @@ resolve_symlink() { # This seems to be the only cross-platform way to resolve symlink paths to # the real file path. # shellcheck disable=SC2012 - ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|' + resolved_path=$(ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|') + + # 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 + # as relative + case $resolved_path in + /*) + echo "$resolved_path" + ;; + *) + echo "$PWD/$resolved_path" + ;; + esac } diff --git a/test/utils.bats b/test/utils.bats index 3947ce32..ab842816 100644 --- a/test/utils.bats +++ b/test/utils.bats @@ -268,3 +268,25 @@ teardown() { [ "$status" -eq 0 ] [ "$output" = "$PROJECT_DIR/.tool-versions" ] } + +@test "resolve_symlink converts the symlink path to the real file path" { + touch foo + ln -s $(pwd)/foo bar + + run resolve_symlink bar + [ "$status" -eq 0 ] + echo $status + [ "$output" = $(pwd)/foo ] + rm -f foo bar +} + +@test "resolve_symlink converts relative symlink path to the real file path" { + touch foo + ln -s foo bar + + run resolve_symlink bar + [ "$status" -eq 0 ] + echo $status + [ "$output" = $(pwd)/foo ] + rm -f foo bar +}