From 6c329e8a839c1b5eeaf2e545b4f4084c3a8830f7 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Wed, 13 Apr 2016 17:14:50 -0700 Subject: provide a realpath implementation Not all distros have a `realpath` command. Provide a function that uses the real command if available else use the fish builtin. Fixes #2932 --- tests/fish_realpath.err | 3 +++ tests/fish_realpath.in | 39 +++++++++++++++++++++++++++++++++++++++ tests/fish_realpath.out | 5 +++++ tests/fish_realpath.status | 1 + 4 files changed, 48 insertions(+) create mode 100644 tests/fish_realpath.err create mode 100644 tests/fish_realpath.in create mode 100644 tests/fish_realpath.out create mode 100644 tests/fish_realpath.status (limited to 'tests') diff --git a/tests/fish_realpath.err b/tests/fish_realpath.err new file mode 100644 index 00000000..f12c3618 --- /dev/null +++ b/tests/fish_realpath.err @@ -0,0 +1,3 @@ +fish_realpath: Invalid path: /this/better/be/an/invalid/path +fish_realpath: Invalid path: nonexistent-file +fish_realpath: Invalid path: ../test/data/fish-symlink/nonexistent-file-relative-to-a-symlink diff --git a/tests/fish_realpath.in b/tests/fish_realpath.in new file mode 100644 index 00000000..f25309c9 --- /dev/null +++ b/tests/fish_realpath.in @@ -0,0 +1,39 @@ +# $XDG_DATA_HOME can itself be a relative path. So force it to an absolute +# path so we can remove it from any resolved paths below. This is needed +# because the contents of the fish_realpath.out file can't include any $PWD +# data since $PWD isn't under our control. +set -l data_home_realpath (fish_realpath $XDG_DATA_HOME) + +# A bogus absolute path is handled correctly and sets a failure status. +if not fish_realpath /this/better/be/an/invalid/path + echo first invalid path handled okay +end + +# A non-existent file relative to $PWD fails. +fish_realpath nonexistent-file + +# The simplest absolute path should undergo no transformation. +fish_realpath / + +# A single symlink to a directory is correctly resolved. +ln -s fish $XDG_DATA_HOME/fish-symlink +set -l real_path (fish_realpath $XDG_DATA_HOME/fish-symlink) +string replace "$data_home_realpath/" "" $real_path + +# A nonexistent file relative to a valid symlink to a directory fails. +# This depends on the symlink created by the previous test. +set -l real_path (fish_realpath $XDG_DATA_HOME/fish-symlink/nonexistent-file-relative-to-a-symlink) + +# A path with two symlinks, first to a directory, second to a file, is correctly resolved. +ln -s fish $XDG_DATA_HOME/fish-symlink2 +touch $XDG_DATA_HOME/fish/real_file +ln -s real_file $XDG_DATA_HOME/fish/symlink_file +set -l real_path (fish_realpath $XDG_DATA_HOME/fish-symlink/symlink_file) +string replace "$data_home_realpath/" "" $real_path + +# The $PWD should undergo no further transformations because it should already +# be a "realpath". +set -l real_path (fish_realpath $PWD) +string replace $PWD "pwd-resolved-to-itself" $real_path + +exit 0 diff --git a/tests/fish_realpath.out b/tests/fish_realpath.out new file mode 100644 index 00000000..d14bca83 --- /dev/null +++ b/tests/fish_realpath.out @@ -0,0 +1,5 @@ +first invalid path handled okay +/ +fish +fish/real_file +pwd-resolved-to-itself diff --git a/tests/fish_realpath.status b/tests/fish_realpath.status new file mode 100644 index 00000000..573541ac --- /dev/null +++ b/tests/fish_realpath.status @@ -0,0 +1 @@ +0 -- cgit v1.2.3