aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiPhish <hiphish@Aleksandars-iMac.local>2016-09-04 12:28:01 +0200
committerJustin M. Keyes <justinkz@gmail.com>2016-09-04 08:01:31 -0400
commitcd321b7d0fb161b77cd7b25da7f9a4721cfb0110 (patch)
tree921fd577a4c5738301d5a5c709c8d22b65fb8863
parent73b8424fad108d1ae5981f7fad32f12df35fef2e (diff)
downloadrneovim-cd321b7d0fb161b77cd7b25da7f9a4721cfb0110.tar.gz
rneovim-cd321b7d0fb161b77cd7b25da7f9a4721cfb0110.tar.bz2
rneovim-cd321b7d0fb161b77cd7b25da7f9a4721cfb0110.zip
getcwd(): Return empty string if CWD is invalid. #5292
Closes #5291 Restores behaviour identical to Vim. If the user calls the VimScript function 'getcwd()' and the working directory cannot be found (for example because the directory has been deleted since the last time it was used) an empty string needs to be returned instead of throwing an error.
-rw-r--r--src/nvim/eval.c20
-rw-r--r--test/functional/ex_cmds/cd_spec.lua20
2 files changed, 27 insertions, 13 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index bfe4707dab..211419f7a6 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -9776,26 +9776,20 @@ static void f_getcwd(typval_T *argvars, typval_T *rettv, FunPtr fptr)
if (from) {
break;
}
- case kCdScopeTab: // FALLTHROUGH
+ case kCdScopeTab: // FALLTHROUGH
assert(tp);
from = tp->localdir;
if (from) {
break;
}
- case kCdScopeGlobal: // FALLTHROUGH
- // The `globaldir` variable is not always set.
- if (globaldir) {
+ case kCdScopeGlobal: // FALLTHROUGH
+ if (globaldir) { // `globaldir` is not always set.
from = globaldir;
- } else {
- // We have to copy the OS path directly into output string.
- if (os_dirname(cwd, MAXPATHL) == FAIL) {
- EMSG(_("E41: Could not display path."));
- goto end;
- }
+ } else if (os_dirname(cwd, MAXPATHL) == FAIL) { // Get the OS CWD.
+ from = (char_u *)""; // Return empty string on failure.
}
break;
- case kCdScopeInvalid:
- // We should never get here
+ case kCdScopeInvalid: // We should never get here
assert(false);
}
@@ -9807,7 +9801,7 @@ static void f_getcwd(typval_T *argvars, typval_T *rettv, FunPtr fptr)
#ifdef BACKSLASH_IN_FILENAME
slash_adjust(rettv->vval.v_string);
#endif
-end:
+
xfree(cwd);
}
diff --git a/test/functional/ex_cmds/cd_spec.lua b/test/functional/ex_cmds/cd_spec.lua
index 74432dbe00..85e1a5c5d5 100644
--- a/test/functional/ex_cmds/cd_spec.lua
+++ b/test/functional/ex_cmds/cd_spec.lua
@@ -269,3 +269,23 @@ for _, cmd in ipairs {'getcwd', 'haslocaldir'} do
end)
end
+describe("getcwd()", function ()
+ local temp_dir = "Xtest-functional-ex_cmds-cd_spec.temp"
+ before_each(function()
+ clear()
+ lfs.mkdir(temp_dir)
+ end)
+
+ after_each(function()
+ helpers.rmdir(temp_dir)
+ end)
+
+ it("returns empty string if working directory does not exist", function()
+ execute("cd " .. temp_dir)
+ helpers.wait()
+ helpers.rmdir(temp_dir)
+ eq("", helpers.eval("getcwd()"))
+ end)
+end)
+
+