view src/testdir/test_rename.vim @ 34780:54890be01c00 v9.1.0265

patch 9.1.0265: console dialog cannot save unnamed buffers Commit: https://github.com/vim/vim/commit/df46115fc839c8912ed60646e86a412e5180ba1d Author: glepnir <glephunter@gmail.com> Date: Thu Apr 4 22:23:29 2024 +0200 patch 9.1.0265: console dialog cannot save unnamed buffers Problem: console dialog cannot save unnamed buffers Solution: set bufname before save (glepnir). Define dialog_con_gui to test for GUI+Console dialog support, use it to skip the test when the GUI feature has been defined. Note: The dialog_changed() function will also try to call the browse_save_fname() function, when FEAT_BROWSE is defined (which is only defined in a GUI build of Vim). This will eventually lead to a call of do_browse(), which causes an error message if a GUI is not currently running (see the TODO: in do_browse()) and will then lead to a failure in Test_goto_buf_with_onfirm(). Therefore, we must disable the Test_goto_buf_with_onfirm(), when the dialog_con_gui feature is enabled (which basically means dialog feature for GUI and Console builds, in contrast to the dialog_con and dialog_gui feature). (Previously this wasn't a problem, because the test aborted in the YES case for the :confirm :b XgotoConf case and did therefore not run into the browse function call) closes: #14398 Signed-off-by: glepnir <glephunter@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Thu, 04 Apr 2024 23:45:02 +0200
parents de319bf98a39
children
line wrap: on
line source

" Test rename()

source shared.vim

func Test_rename_file_to_file()
  call writefile(['foo'], 'Xrename1')

  call assert_equal(0, rename('Xrename1', 'Xrename2'))

  call assert_equal('', glob('Xrename1'))
  call assert_equal(['foo'], readfile('Xrename2'))

  " When the destination file already exists, it should be overwritten.
  call writefile(['foo'], 'Xrename1')
  call writefile(['bar'], 'Xrename2', 'D')

  call assert_equal(0, rename('Xrename1', 'Xrename2'))
  call assert_equal('', glob('Xrename1'))
  call assert_equal(['foo'], readfile('Xrename2'))

  call delete('Xrename2')
endfunc

func Test_rename_file_ignore_case()
  " With 'fileignorecase', renaming file will go through a temp file
  " when the source and destination file only differ by case.
  set fileignorecase
  call writefile(['foo'], 'Xrename')

  call assert_equal(0, 'Xrename'->rename('XRENAME'))

  call assert_equal(['foo'], readfile('XRENAME'))

  set fileignorecase&
  call delete('XRENAME')
endfunc

func Test_rename_same_file()
  call writefile(['foo'], 'Xrename', 'D')

  " When the source and destination are the same file, nothing
  " should be done. The source file should not be deleted.
  call assert_equal(0, rename('Xrename', 'Xrename'))
  call assert_equal(['foo'], readfile('Xrename'))

  call assert_equal(0, rename('./Xrename', 'Xrename'))
  call assert_equal(['foo'], readfile('Xrename'))
endfunc

func Test_rename_dir_to_dir()
  call mkdir('Xrenamedir1')
  call writefile(['foo'], 'Xrenamedir1/Xrenamefile')

  call assert_equal(0, rename('Xrenamedir1', 'Xrenamedir2'))

  call assert_equal('', glob('Xrenamedir1'))
  call assert_equal(['foo'], readfile('Xrenamedir2/Xrenamefile'))

  call delete('Xrenamedir2/Xrenamefile')
  call delete('Xrenamedir2', 'd')
endfunc

func Test_rename_same_dir()
  call mkdir('Xrenamedir', 'R')
  call writefile(['foo'], 'Xrenamedir/Xrenamefile')

  call assert_equal(0, rename('Xrenamedir', 'Xrenamedir'))

  call assert_equal(['foo'], readfile('Xrenamedir/Xrenamefile'))
endfunc

func Test_rename_copy()
  " Check that when original file can't be deleted, rename()
  " still succeeds but copies the file.
  call mkdir('Xrenamedir', 'R')
  call writefile(['foo'], 'Xrenamedir/Xrenamefile')
  call setfperm('Xrenamedir', 'r-xr-xr-x')

  call assert_equal(0, rename('Xrenamedir/Xrenamefile', 'Xrenamefile'))

  if !has('win32') && !IsRoot()
    " On Windows, the source file is removed despite
    " its directory being made not writable.
    call assert_equal(['foo'], readfile('Xrenamedir/Xrenamefile'))
  endif
  call assert_equal(['foo'], readfile('Xrenamefile'))

  call setfperm('Xrenamedir', 'rwxrwxrwx')
  call delete('Xrenamefile')
endfunc

func Test_rename_fails()
  call writefile(['foo'], 'Xrenamefile', 'D')

  " Can't rename into a non-existing directory.
  call assert_notequal(0, rename('Xrenamefile', 'Xdoesnotexist/Xrenamefile'))

  " Can't rename a non-existing file.
  call assert_notequal(0, rename('Xdoesnotexist', 'Xrenamefile2'))
  call assert_equal('', glob('Xrenamefile2'))

  " When rename() fails, the destination file should not be deleted.
  call assert_notequal(0, rename('Xdoesnotexist', 'Xrenamefile'))
  call assert_equal(['foo'], readfile('Xrenamefile'))

  " Can't rename to en empty file name.
  call assert_notequal(0, rename('Xrenamefile', ''))

  call assert_fails('call rename("Xrenamefile", [])', 'E730:')
  call assert_fails('call rename(0z, "Xrenamefile")', 'E976:')
endfunc

" vim: shiftwidth=2 sts=2 expandtab