patch 9.1.1507: symlinks are resolved on :cd commands

Problem:  File paths change from symlink to target path after :cd command
          when editing files through symbolic links
Solution: Add "~" flag to 'cpoptions' to control symlink resolution.
          When not included (default), symlinks are resolved maintaining
          backward compatibility. When included, symlinks are preserved
          providing the improved behavior. (glepnir)

related: neovim/neovim#15695
closes: #17628

Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/testdir/test_cd.vim b/src/testdir/test_cd.vim
index 13a3eba..85494d3 100644
--- a/src/testdir/test_cd.vim
+++ b/src/testdir/test_cd.vim
@@ -252,4 +252,67 @@
   call chdir(startdir)
 endfunc
 
+func Test_cd_preserve_symlinks()
+  " Test new behavior: preserve symlinks when cpo-=~
+  set cpoptions+=~
+
+  let savedir = getcwd()
+  call mkdir('Xsource', 'R')
+  call writefile(['abc'], 'Xsource/foo.txt', 'D')
+
+  if has("win32")
+    silent !mklink /D Xdest Xsource
+  else
+    silent !ln -s Xsource Xdest
+  endif
+  if v:shell_error
+    call delete('Xsource', 'rf')
+    throw 'Skipped: cannot create symlinks'
+  endif
+
+  edit Xdest/foo.txt
+  let path_before = expand('%')
+  call assert_match('Xdest[/\\]foo\.txt$', path_before)
+
+  cd .
+  let path_after = expand('%')
+  call assert_equal(path_before, path_after)
+  call assert_match('Xdest[/\\]foo\.txt$', path_after)
+
+  bwipe!
+  set cpoptions&
+  call delete('Xdest', 'rf')
+  call delete('Xsource', 'rf')
+  call chdir(savedir)
+endfunc
+
+func Test_cd_symlinks()
+  CheckNotMSWindows
+
+  let savedir = getcwd()
+  call mkdir('Xsource', 'R')
+  call writefile(['abc'], 'Xsource/foo.txt', 'D')
+
+  silent !ln -s Xsource Xdest
+  if v:shell_error
+    call delete('Xsource', 'rf')
+    throw 'Skipped: cannot create symlinks'
+  endif
+
+  edit Xdest/foo.txt
+  let path_before = expand('%')
+  call assert_match('Xdest[/\\]foo\.txt$', path_before)
+
+  cd .
+  let path_after = expand('%')
+  call assert_match('Xsource[/\\]foo\.txt$', path_after)
+  call assert_notequal(path_before, path_after)
+
+  bwipe!
+  set cpoptions&
+  call delete('Xdest', 'rf')
+  call delete('Xsource', 'rf')
+  call chdir(savedir)
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testdir/test_options.vim b/src/testdir/test_options.vim
index cb5636e..21e4a03 100644
--- a/src/testdir/test_options.vim
+++ b/src/testdir/test_options.vim
@@ -2273,7 +2273,7 @@
     qall
   [CODE]
   if RunVim([], after, '')
-    call assert_equal(['aAbBcCdDeEfFgHiIjJkKlLmMnoOpPqrRsStuvwWxXyZz$!%*-+<>#{|&/\.;',
+    call assert_equal(['aAbBcCdDeEfFgHiIjJkKlLmMnoOpPqrRsStuvwWxXyZz$!%*-+<>#{|&/\.;~',
           \            'AS'], readfile('X_VIM_POSIX'))
   endif
 
@@ -2528,7 +2528,7 @@
         \ ['completeopt', 'popup', 'a123'],
         \ ['completepopup', 'width:20', 'border'],
         \ ['concealcursor', 'v', 'xyz'],
-        \ ['cpoptions', 'HJ', '~'],
+        \ ['cpoptions', 'HJ', 'Q'],
         \ ['cryptmethod', 'zip', 'a123'],
         \ ['cursorlineopt', 'screenline', 'a123'],
         \ ['debug', 'throw', 'a123'],