From 2bc679084f4a557eab3e505e5480113ae42806af Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sun, 7 Oct 2018 20:21:48 -0400 Subject: [PATCH 1/4] Add test for the resolve_symlink function. --- test/utils.bats | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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 +} From 4c4590c6e651207913f5c7c0f2ca364d1899c52b Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sun, 7 Oct 2018 20:36:29 -0400 Subject: [PATCH 2/4] Update resolve_symlink function so it always returns absolute paths. --- lib/utils.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/utils.sh b/lib/utils.sh index a50cad74..63ba8d4c 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -378,5 +378,15 @@ 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 + if [ ${resolved_path:0:1} = "/" ]; then + echo "$resolved_path" + else + dir=$(cd $(dirname "$symlink"); pwd) + echo "$dir/$resolved_path" + fi } From e0cdec39f997f4ca09a8ec0bbd2b702e0b573d27 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sun, 7 Oct 2018 20:47:27 -0400 Subject: [PATCH 3/4] Address shellcheck errors in resolve_symlink function. --- lib/utils.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utils.sh b/lib/utils.sh index 63ba8d4c..fb58b625 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -383,10 +383,10 @@ resolve_symlink() { # 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 - if [ ${resolved_path:0:1} = "/" ]; then + if [ "${resolved_path:0:1}" = "/" ]; then echo "$resolved_path" else - dir=$(cd $(dirname "$symlink"); pwd) + dir=$(cd "$(dirname "$symlink")" || return; pwd) echo "$dir/$resolved_path" fi } From d6b01e35ec2feadb189b215c5189c36062dd5eea Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sun, 7 Oct 2018 21:33:45 -0400 Subject: [PATCH 4/4] Simplify resolve_symlink function. --- lib/utils.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/utils.sh b/lib/utils.sh index fb58b625..1d380eb5 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -383,10 +383,12 @@ resolve_symlink() { # 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 - if [ "${resolved_path:0:1}" = "/" ]; then - echo "$resolved_path" - else - dir=$(cd "$(dirname "$symlink")" || return; pwd) - echo "$dir/$resolved_path" - fi + case $resolved_path in + /*) + echo "$resolved_path" + ;; + *) + echo "$PWD/$resolved_path" + ;; + esac }