Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make fish's realpath compatible with GNU realpath
After implementing `builtin fish_realpath` it was noticed that it did not behave like GNU `realpath` without options. Which is super annoying since that was the whole point of implementing the command. Major failure on my part since I wrote the unit tests to match the behavior of the existing `wrealpath()` function that I simply exposed as a builtin command. Rather than actually verifying it behaved in a manner compatible with GNU realpath. Also, while the decision to call the builtin `fish_realpath` seemed to make sense at the time of the original commit further reflection has shown that to be a silly, idiosyncratic, thing to have done. So rename it to simply `realpath`. Fixes 3400
- Loading branch information
Kurtis Rader
committed
Oct 5, 2016
1 parent
d389b22
commit f7f39b8
Showing
11 changed files
with
170 additions
and
102 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
\section realpath realpath - Convert a path to an absolute path without symlinks | ||
|
||
\subsection realpath-synopsis Synopsis | ||
\fish{synopsis} | ||
realpath path | ||
\endfish | ||
|
||
\subsection realpath-description Description | ||
|
||
This is implemented as a function and a builtin. The function will attempt to use an external realpath command if one can be found. Otherwise it falls back to the builtin. The builtin does not support any options. It's meant to be used only by scripts which need to be portable. The builtin implementation behaves like GNU realpath when invoked without any options (which is the most common use case). In general scripts should not invoke the builtin directly. They should just use `realpath`. | ||
|
||
If the path is invalid no translated path will be written to stdout and an error will be reported. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
realpath: Invalid path: /this/better/be/an/invalid/path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# $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 builtin realpath.out file can't include any $PWD | ||
# data since $PWD isn't under our control. | ||
set -l data_home_realpath (builtin realpath $XDG_DATA_HOME) | ||
|
||
# A bogus absolute path is handled correctly and sets a failure status. | ||
if not builtin realpath /this/better/be/an/invalid/path | ||
echo first invalid path handled okay | ||
end | ||
|
||
# A non-existent file relative to $PWD succeeds. | ||
set -l real_path (builtin realpath nonexistent-file) | ||
if test "$real_path" = "$PWD/nonexistent-file" | ||
echo nonexistent-file in PWD correctly converted | ||
end | ||
|
||
# The simplest absolute path should undergo no transformation. | ||
builtin realpath / | ||
|
||
# The second simplest absolute path should undergo no transformation. | ||
builtin realpath /this-better-not-exist | ||
|
||
# Check that a pathological case is handled correctly (i.e., there is only one | ||
# leading slash). | ||
builtin realpath /../../x | ||
|
||
# Another pathological corner case. GNU realpath first strips trailing slashes | ||
# so that "/a//" is converted to "/a" before performing the real path | ||
# conversion. So, despite appearances, it considers "a" to be the last | ||
# component in that case. | ||
builtin realpath /abc/ | ||
builtin realpath /def/// | ||
|
||
# A single symlink to a directory is correctly resolved. | ||
ln -s fish $XDG_DATA_HOME/fish-symlink | ||
set -l real_path (builtin realpath $XDG_DATA_HOME/fish-symlink) | ||
set -l expected_real_path "$data_home_realpath/fish" | ||
if test "$real_path" = "$expected_real_path" | ||
echo "fish-symlink handled correctly" | ||
else | ||
echo "fish-symlink not handled correctly: $real_path != $expected_real_path" >&2 | ||
end | ||
|
||
# A nonexistent file relative to a valid symlink to a directory gets converted. | ||
# This depends on the symlink created by the previous test. | ||
set -l real_path (builtin realpath $XDG_DATA_HOME/fish-symlink/nonexistent-file-relative-to-a-symlink) | ||
set -l expected_real_path "$data_home_realpath/fish/nonexistent-file-relative-to-a-symlink" | ||
if test "$real_path" = "$expected_real_path" | ||
echo "fish-symlink/nonexistent-file-relative-to-a-symlink correctly converted" | ||
else | ||
echo "failure nonexistent-file-relative-to-a-symlink: $real_path != $expected_real_path" >&2 | ||
end | ||
|
||
# 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 (builtin realpath $XDG_DATA_HOME/fish-symlink/symlink_file) | ||
set -l expected_real_path "$data_home_realpath/fish/real_file" | ||
if test "$real_path" = "$expected_real_path" | ||
echo "fish-symlink/symlink_file handled correctly" | ||
else | ||
echo "fish-symlink/symlink_file not handled correctly: $real_path != expected_real_path" >&2 | ||
end | ||
|
||
# The $PWD should undergo no further transformations because it should already | ||
# be a "realpath". | ||
set -l real_path (builtin realpath $PWD) | ||
if test "$real_path" = "$PWD" | ||
echo "pwd-resolved-to-itself" | ||
else | ||
echo "unexpected pwd-resolved-to-itself failure: $real_path != $PWD" >&2 | ||
end | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
first invalid path handled okay | ||
nonexistent-file in PWD correctly converted | ||
/ | ||
/this-better-not-exist | ||
/x | ||
/abc | ||
/def | ||
fish-symlink handled correctly | ||
fish-symlink/nonexistent-file-relative-to-a-symlink correctly converted | ||
fish-symlink/symlink_file handled correctly | ||
pwd-resolved-to-itself |