patch 9.0.0719: too many delete() calls in tests

Problem:    Too many delete() calls in tests.
Solution:   Use deferred delete where possible.
diff --git a/src/testdir/test_startup.vim b/src/testdir/test_startup.vim
index 7995cd2..6c2fbbd 100644
--- a/src/testdir/test_startup.vim
+++ b/src/testdir/test_startup.vim
@@ -43,14 +43,14 @@
     quit
   [CODE]
 
-  call mkdir('Xhere/plugin', 'p')
+  call mkdir('Xhere/plugin', 'pR')
   call writefile(['let g:sequence .= "here "'], 'Xhere/plugin/here.vim')
-  call mkdir('Xanother/plugin', 'p')
+  call mkdir('Xanother/plugin', 'pR')
   call writefile(['let g:sequence .= "another "'], 'Xanother/plugin/another.vim')
   call mkdir('Xhere/pack/foo/start/foobar/plugin', 'p')
   call writefile(['let g:sequence .= "pack "'], 'Xhere/pack/foo/start/foobar/plugin/foo.vim')
 
-  call mkdir('Xafter/plugin', 'p')
+  call mkdir('Xafter/plugin', 'pR')
   call writefile(['let g:sequence .= "after "'], 'Xafter/plugin/later.vim')
 
   if RunVim(before, after, '')
@@ -72,9 +72,6 @@
 
   call delete('Xtestout')
   call delete('Xsequence')
-  call delete('Xhere', 'rf')
-  call delete('Xanother', 'rf')
-  call delete('Xafter', 'rf')
 endfunc
 
 func Test_pack_in_rtp_when_plugins_run()
@@ -92,7 +89,7 @@
   let after = [
 	\ 'quit',
 	\ ]
-  call mkdir('Xhere/plugin', 'p')
+  call mkdir('Xhere/plugin', 'pR')
   call writefile(['redir! > Xtestout', 'silent set runtimepath?', 'silent! call foo#Trigger()', 'redir END'], 'Xhere/plugin/here.vim')
   call mkdir('Xhere/pack/foo/start/foobar/autoload', 'p')
   call writefile(['function! foo#Trigger()', 'echo "autoloaded foo"', 'endfunction'], 'Xhere/pack/foo/start/foobar/autoload/foo.vim')
@@ -105,7 +102,6 @@
   endif
 
   call delete('Xtestout')
-  call delete('Xhere', 'rf')
 endfunc
 
 func Test_help_arg()
@@ -228,7 +224,6 @@
     call assert_equal('foo', bn1)
     call assert_equal('bar', bn2)
   endif
-
   call delete('Xtestout')
 endfunc
 
@@ -303,7 +298,7 @@
       return 666
     }
   END
-  call writefile(lines, 'Xbadfile.c')
+  call writefile(lines, 'Xbadfile.c', 'D')
 
   let after =<< trim [CODE]
     call writefile([&errorfile, string(getpos("."))], "XtestoutQarg")
@@ -314,7 +309,7 @@
 
   " Test with default argument '-q'.
   call assert_equal('errors.err', &errorfile)
-  call writefile(["Xbadfile.c:4:12: error: expected ';' before '}' token"], 'errors.err')
+  call writefile(["Xbadfile.c:4:12: error: expected ';' before '}' token"], 'errors.err', 'D')
   if RunVim([], after, '-q')
     let lines = readfile('XtestoutQarg')
     call assert_equal(['errors.err',
@@ -323,10 +318,9 @@
 	\             lines)
   endif
   call delete('XtestoutQarg')
-  call delete('errors.err')
 
   " Test with explicit argument '-q XerrorsQarg' (with space).
-  call writefile(["Xbadfile.c:4:12: error: expected ';' before '}' token"], 'XerrorsQarg')
+  call writefile(["Xbadfile.c:4:12: error: expected ';' before '}' token"], 'XerrorsQarg', 'D')
   if RunVim([], after, '-q XerrorsQarg')
     let lines = readfile('XtestoutQarg')
     call assert_equal(['XerrorsQarg',
@@ -349,9 +343,7 @@
   let out = system(GetVimCommand() .. ' -q xyz.err')
   call assert_equal(3, v:shell_error)
 
-  call delete('Xbadfile.c')
   call delete('XtestoutQarg')
-  call delete('XerrorsQarg')
 endfunc
 
 " Test the -V[N]{filename} argument to set the 'verbose' option to N
@@ -400,7 +392,7 @@
 " Test the -A, -F and -H arguments (Arabic, Farsi and Hebrew modes).
 func Test_A_F_H_arg()
   let after =<< trim [CODE]
-    call writefile([&rightleft, &arabic, &fkmap, &hkmap], "Xtestout")
+    call writefile([&rightleft, &arabic, &fkmap, &hkmap], "Xtestout", 'D')
     qall
   [CODE]
 
@@ -420,8 +412,6 @@
     let lines = readfile('Xtestout')
     call assert_equal(['1', '0', '0', '1'], lines)
   endif
-
-  call delete('Xtestout')
 endfunc
 
 " Test the --echo-wid argument (for GTK GUI only).
@@ -799,7 +789,7 @@
 
 func Test_zzz_startinsert()
   " Test :startinsert
-  call writefile(['123456'], 'Xtestout')
+  call writefile(['123456'], 'Xtestout', 'D')
   let after =<< trim [CODE]
     :startinsert
     call feedkeys("foobar\<c-o>:wq\<cr>","t")
@@ -820,7 +810,6 @@
     let lines = readfile('Xtestout')
     call assert_equal(['123456foobar'], lines)
   endif
-  call delete('Xtestout')
 endfunc
 
 func Test_issue_3969()
@@ -898,8 +887,8 @@
         \ "first\tXfile1\t/^    \\zsfirst$/",
         \ "second\tXfile1\t/^    \\zssecond$/",
         \ "third\tXfile1\t/^    \\zsthird$/"],
-        \ 'Xtags')
-  call writefile(['    first', '    second', '    third'], 'Xfile1')
+        \ 'Xtags', 'D')
+  call writefile(['    first', '    second', '    third'], 'Xfile1', 'D')
 
   for t_arg in ['-t second', '-tsecond']
     if RunVim(before, after, t_arg)
@@ -907,9 +896,6 @@
       call delete('Xtestout')
     endif
   endfor
-
-  call delete('Xtags')
-  call delete('Xfile1')
 endfunc
 
 " Test the '-T' argument which sets the 'term' option.
@@ -1006,7 +992,7 @@
     call assert_match('^E282:', v:errmsg)
     call writefile(v:errors, 'Xtestout')
   [CODE]
-  call writefile(after, 'Xafter')
+  call writefile(after, 'Xafter', 'D')
 
   let cmd = GetVimCommandCleanTerm() . ' -u Xvimrc_missing -S Xafter'
   let buf = term_start(cmd, {'term_rows' : 10})
@@ -1017,7 +1003,7 @@
   call WaitForAssert({-> assert_match(':', term_getline(buf, 10))})
   call StopVimInTerminal(buf)
   call assert_equal([], readfile('Xtestout'))
-  call delete('Xafter')
+
   call delete('Xtestout')
 endfunc
 
@@ -1029,13 +1015,13 @@
     call writefile(v:errors, 'Xtestout')
     qall
   [CODE]
-  call writefile(after, 'Xafter')
+  call writefile(after, 'Xafter', 'D')
   let cmd = GetVimProg() . ' --not-a-term -S Xafter --cmd "set enc=utf8"'
   call setenv('VIMINIT', 'let viminit_found="yes"')
   exe "silent !" . cmd
   call assert_equal([], readfile('Xtestout'))
+
   call delete('Xtestout')
-  call delete('Xafter')
 endfunc
 
 " Test for using the $EXINIT environment variable
@@ -1046,13 +1032,13 @@
     call writefile(v:errors, 'Xtestout')
     qall
   [CODE]
-  call writefile(after, 'Xafter')
+  call writefile(after, 'Xafter', 'D')
   let cmd = GetVimProg() . ' --not-a-term -S Xafter --cmd "set enc=utf8"'
   call setenv('EXINIT', 'let exinit_found="yes"')
   exe "silent !" . cmd
   call assert_equal([], readfile('Xtestout'))
+
   call delete('Xtestout')
-  call delete('Xafter')
 endfunc
 
 " Test for using the 'exrc' option
@@ -1064,13 +1050,12 @@
     call writefile(v:errors, 'Xtestout')
     qall
   [CODE]
-  call mkdir('Xrcdir')
+  call mkdir('Xrcdir', 'R')
   call writefile(['let exrc_found=37'], 'Xrcdir/.exrc')
   call writefile(after, 'Xrcdir/Xafter')
   let cmd = GetVimProg() . ' --not-a-term -S Xafter --cmd "cd Xrcdir" --cmd "set enc=utf8 exrc secure"'
   exe "silent !" . cmd
   call assert_equal([], readfile('Xrcdir/Xtestout'))
-  call delete('Xrcdir', 'rf')
 endfunc
 
 " Test for starting Vim with a non-terminal as input/output
@@ -1138,7 +1123,7 @@
   " Can't catch the output of gvim.
   CheckNotGui
 
-  call writefile(["iVim Editor\<Esc>:q!\<CR>"], 'Xscriptin', 'b')
+  call writefile(["iVim Editor\<Esc>:q!\<CR>"], 'Xscriptin', 'bD')
   if RunVim([], [], '-s Xscriptin -w Xscriptout')
     call assert_equal(["iVim Editor\e:q!\r"], readfile('Xscriptout'))
     call delete('Xscriptout')
@@ -1162,7 +1147,6 @@
       call delete('Xresult')
     endif
   endfor
-  call delete('Xscriptin')
 endfunc
 
 " Test for the "-s scriptin" argument
@@ -1174,10 +1158,9 @@
   let m = system(GetVimCommand() .. " -s abcxyz")
   call assert_equal("Cannot open for reading: \"abcxyz\"\n", m)
 
-  call writefile([], 'Xinput')
+  call writefile([], 'Xinput', 'D')
   let m = system(GetVimCommand() .. " -s Xinput -s Xinput")
   call assert_equal("Attempt to open script file again: \"-s Xinput\"\n", m)
-  call delete('Xinput')
 endfunc
 
 " Test for the "-n" (no swap file) argument
@@ -1252,7 +1235,7 @@
 func Test_progname()
   CheckUnix
 
-  call mkdir('Xprogname', 'p')
+  call mkdir('Xprogname', 'pD')
   call writefile(['silent !date',
   \               'call writefile([mode(1), '
   \               .. '&insertmode, &diff, &readonly, &updatecount, '
@@ -1324,12 +1307,11 @@
   endfor
 
   call delete('Xprogname_after')
-  call delete('Xprogname', 'd')
 endfunc
 
 " Test for doing a write from .vimrc
 func Test_write_in_vimrc()
-  call writefile(['silent! write'], 'Xvimrc')
+  call writefile(['silent! write'], 'Xvimrc', 'D')
   let after =<< trim [CODE]
     call assert_match('E32: ', v:errmsg)
     call writefile(v:errors, 'Xtestout')
@@ -1339,7 +1321,6 @@
     call assert_equal([], readfile('Xtestout'))
     call delete('Xtestout')
   endif
-  call delete('Xvimrc')
 endfunc
 
 func Test_echo_true_in_cmd()
@@ -1350,11 +1331,11 @@
       call writefile(['done'], 'Xresult')
       quit
   END
-  call writefile(lines, 'Xscript')
+  call writefile(lines, 'Xscript', 'D')
   if RunVim([], [], '--cmd "source Xscript"')
     call assert_equal(['done'], readfile('Xresult'))
   endif
-  call delete('Xscript')
+
   call delete('Xresult')
 endfunc
 
@@ -1365,11 +1346,11 @@
       call writefile(['done'], 'Xresult')
       qa!
   END
-  call writefile(lines, 'Xscript')
+  call writefile(lines, 'Xscript', 'D')
   if RunVim([], [], "--clean -e -s --cmd 'file x|new|file x' --cmd 'so Xscript'")
     call assert_equal(['done'], readfile('Xresult'))
   endif
-  call delete('Xscript')
+
   call delete('Xresult')
 endfunc