patch 9.1.1060: Vim always enables 'termguicolors' in a terminal

Problem:  Vim always enables 'termguicolors' in a terminal, even
          when not wanted (after v9.1.1054)
Solution: Respect `:set notermguicolors` in vimrc file

fixes: #16538
fixes: #16539
closes: #16540

Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/testdir/test_termcodes.vim b/src/testdir/test_termcodes.vim
index 098fdb3..db98a70 100644
--- a/src/testdir/test_termcodes.vim
+++ b/src/testdir/test_termcodes.vim
@@ -10,6 +10,24 @@
 source view_util.vim
 source term_util.vim
 
+func s:TermGuiColorsTest()
+  CheckNotMSWindows
+  if !CanRunVimInTerminal()
+    throw 'Skipped: cannot make screendumps'
+  endif
+  if !executable('tput')
+    throw "Skipped: tput not executable!"
+  endif
+  if has("gui_running")
+    throw "Skipped: does not work in GUI mode"
+  endif
+  call system('tput -Txterm-direct RGB 2>/dev/null')
+  if v:shell_error
+    throw "Skipped: xterm-direct $TERM has no RGB capability"
+  endif
+endfunc
+
+
 func Test_term_mouse_left_click()
   new
   let save_mouse = &mouse
@@ -2740,21 +2758,8 @@
 endfunc
 
 func Test_xterm_direct_enables_termguicolors()
+  call s:TermGuiColorsTest()
   " TERM=xterm-direct enables termguicolors
-  CheckNotMSWindows
-  if !CanRunVimInTerminal()
-    throw 'Skipped: cannot make screendumps'
-  endif
-  if !executable('tput')
-    throw "Skipped: tput not executable!"
-  endif
-  if has("gui_running")
-    throw "Skipped: does not work in GUI mode"
-  endif
-  call system('tput -Txterm-direct RGB 2>/dev/null')
-  if v:shell_error
-    throw "Skipped: xterm-direct $TERM has no RGB capability"
-  endif
   let colors  = systemlist('tput -Txterm-direct colors')[0]
   defer delete('XTerm-direct.txt')
 
@@ -2773,6 +2778,33 @@
   call assert_equal(['', 'TERM: xterm-direct', 'Termguicolors: 1'], result)
   " cleanup
   bw!
+  close
+endfunc
+
+func Test_xterm_direct_no_termguicolors()
+  " unfortunately doesn't work with libvterm
+  call s:TermGuiColorsTest()
+
+  let lines =<< trim END
+      set notermguicolors noswapfile
+      set t_Co=16777216
+  END
+  call writefile(lines, 'XtermDirect', 'D')
+  defer delete('XTerm-direct2.txt')
+
+  let buf = RunVimInTerminal('-S XtermDirect --clean XTerm-direct2.txt',
+        \  {'rows': 10, 'env': {'TERM': 'xterm-direct'}})
+  call TermWait(buf)
+  call term_sendkeys(buf, ":$put ='TERM: ' .. &term\<cr>")
+  call term_sendkeys(buf, ":$put ='Termguicolors: ' .. &tgc\<cr>")
+  call term_sendkeys(buf, ":wq\<cr>")
+  call TermWait(buf)
+
+  let result=readfile('XTerm-direct2.txt')
+  call assert_equal(['', 'TERM: xterm-direct', 'Termguicolors: 0'], result)
+  " cleanup
+  bw!
+  close
 endfunc
 
 " vim: shiftwidth=2 sts=2 expandtab