runtime(termdebug): check for gdb file/dir before using as buffer name

Commit: 
https://github.com/vim/vim/commit/62ccaa60d5f7f9a13c758bd5e55b7ca6855a6de9
Author: Ubaldo Tiberi <ubaldo.tib...@volvo.com>
Date:   Tue May 21 23:33:03 2024 +0200

    runtime(termdebug): check for gdb file/dir before using as buffer name
    
    Add test so that this doesn't regress.
    
    fixes: #12718
    closes: #14792
    
    Signed-off-by: Ubaldo Tiberi <ubaldo.tib...@volvo.com>
    Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim 
b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim
index 50833f0df..8eb3fff69 100644
--- a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim
+++ b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim
@@ -415,7 +415,17 @@ func s:StartDebug_prompt(dict)
   let s:promptbuf = bufnr('')
   call prompt_setprompt(s:promptbuf, 'gdb> ')
   set buftype=prompt
-  file gdb
+
+  if empty(glob('gdb'))
+    file gdb
+  elseif empty(glob('Termdebug-gdb-console'))
+    file Termdebug-gdb-console
+  else
+    call s:Echoerr("You have a file/folder named 'gdb'
+          \ or 'Termdebug-gdb-console'.
+          \ Please exit and rename them because Termdebug may not work as 
expected.")
+  endif
+
   call prompt_setcallback(s:promptbuf, function('s:PromptCallback'))
   call prompt_setinterrupt(s:promptbuf, function('s:PromptInterrupt'))
 
@@ -1463,9 +1473,12 @@ func s:GotoAsmwinOrCreateIt()
 
     if s:asmbuf > 0 && bufexists(s:asmbuf)
       exe 'buffer' . s:asmbuf
-    else
+    elseif empty(glob('Termdebug-asm-listing'))
       silent file Termdebug-asm-listing
       let s:asmbuf = bufnr('Termdebug-asm-listing')
+    else
+      call s:Echoerr("You have a file/folder named 'Termdebug-asm-listing'.
+          \ Please exit and rename it because Termdebug may not work as 
expected.")
     endif
 
     if mdf != 'vert' && s:GetDisasmWindowHeight() > 0
@@ -1532,9 +1545,12 @@ func s:GotoVariableswinOrCreateIt()
 
     if s:varbuf > 0 && bufexists(s:varbuf)
       exe 'buffer' . s:varbuf
-    else
+    elseif empty(glob('Termdebug-variables-listing'))
       silent file Termdebug-variables-listing
       let s:varbuf = bufnr('Termdebug-variables-listing')
+    else
+      call s:Echoerr("You have a file/folder named 
'Termdebug-variables-listing'.
+          \ Please exit and rename it because Termdebug may not work as 
expected.")
     endif
 
     if mdf != 'vert' && s:GetVariablesWindowHeight() > 0
diff --git a/src/testdir/test_termdebug.vim b/src/testdir/test_termdebug.vim
index 14d443a98..62cafccb6 100644
--- a/src/testdir/test_termdebug.vim
+++ b/src/testdir/test_termdebug.vim
@@ -300,4 +300,43 @@ func Test_termdebug_mapping()
   %bw!
 endfunc
 
+func Test_termdebug_bufnames()
+  " Test if user has filename/folders named gdb, Termdebug-gdb-console,
+  " etc. in the current directory
+  let g:termdebug_config = {}
+  let g:termdebug_config['use_prompt'] = 1
+  let filename = 'gdb'
+  let replacement_filename = 'Termdebug-gdb-console'
+
+  call writefile(['This', 'is', 'a', 'test'], filename, 'D')
+  " Throw away the file once the test has done.
+  Termdebug
+  " Once termdebug has completed the startup you should have 3 windows on 
screen
+  call WaitForAssert({-> assert_equal(3, winnr('$'))})
+  " A file named filename already exists in the working directory,
+  " hence you must call the newly created buffer differently
+  call WaitForAssert({-> assert_false(bufexists(filename))})
+  call WaitForAssert({-> assert_true(bufexists(replacement_filename))})
+  quit!
+  call WaitForAssert({-> assert_equal(1, winnr('$'))})
+
+  " Check if error message is in :message
+  let g:termdebug_config['disasm_window'] = 1
+  let filename = 'Termdebug-asm-listing'
+  call writefile(['This', 'is', 'a', 'test'], filename, 'D')
+  " Check only the head of the error message
+  let error_message = "You have a file/folder named '" .. filename .. "'"
+  Termdebug
+  " Once termdebug has completed the startup you should have 4 windows on 
screen
+  call WaitForAssert({-> assert_equal(4, winnr('$'))})
+  call WaitForAssert({-> assert_notequal(-1, stridx(execute('messages'), 
error_message))})
+  quit!
+  wincmd b
+  wincmd q
+  call WaitForAssert({-> assert_equal(1, winnr('$'))})
+
+  unlet g:termdebug_config
+endfunc
+
+
 " vim: shiftwidth=2 sts=2 expandtab

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/E1s9XI7-00GONu-3n%40256bit.org.

Raspunde prin e-mail lui