test_system.vim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. " Tests for system() and systemlist()
  2. source shared.vim
  3. source check.vim
  4. func Test_System()
  5. if !executable('echo') || !executable('cat') || !executable('wc')
  6. return
  7. endif
  8. let out = 'echo 123'->system()
  9. call assert_equal("123\n", out)
  10. let out = 'echo 123'->systemlist()
  11. if &shell =~# 'cmd.exe$'
  12. call assert_equal(["123\r"], out)
  13. else
  14. call assert_equal(['123'], out)
  15. endif
  16. call assert_equal('123', system('cat', '123'))
  17. call assert_equal(['123'], systemlist('cat', '123'))
  18. call assert_equal(["as\<NL>df"], systemlist('cat', ["as\<NL>df"]))
  19. new Xdummy
  20. call setline(1, ['asdf', "pw\<NL>er", 'xxxx'])
  21. let out = system('wc -l', bufnr('%'))
  22. " On OS/X we get leading spaces
  23. let out = substitute(out, '^ *', '', '')
  24. call assert_equal("3\n", out)
  25. let out = systemlist('wc -l', bufnr('%'))
  26. " On Windows we may get a trailing CR.
  27. if out != ["3\r"]
  28. " On OS/X we get leading spaces
  29. if type(out) == v:t_list
  30. let out[0] = substitute(out[0], '^ *', '', '')
  31. endif
  32. call assert_equal(['3'], out)
  33. endif
  34. let out = systemlist('cat', bufnr('%'))
  35. " On Windows we may get a trailing CR.
  36. if out != ["asdf\r", "pw\<NL>er\r", "xxxx\r"]
  37. call assert_equal(['asdf', "pw\<NL>er", 'xxxx'], out)
  38. endif
  39. bwipe!
  40. call assert_fails('call system("wc -l", 99999)', 'E86:')
  41. endfunc
  42. func Test_system_exmode()
  43. if has('unix') " echo $? only works on Unix
  44. let cmd = ' -es -c "source Xscript" +q; echo "result=$?"'
  45. " Need to put this in a script, "catch" isn't found after an unknown
  46. " function.
  47. call writefile(['try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript', 'D')
  48. let a = system(GetVimCommand() . cmd)
  49. call assert_match('result=0', a)
  50. call assert_equal(0, v:shell_error)
  51. endif
  52. " Error before try does set error flag.
  53. call writefile(['call nosuchfunction()', 'try', 'call doesnotexist()', 'catch', 'endtry'], 'Xscript')
  54. if has('unix') " echo $? only works on Unix
  55. let a = system(GetVimCommand() . cmd)
  56. call assert_notequal('0', a[0])
  57. endif
  58. let cmd = ' -es -c "source Xscript" +q'
  59. let a = system(GetVimCommand() . cmd)
  60. call assert_notequal(0, v:shell_error)
  61. if has('unix') " echo $? only works on Unix
  62. let cmd = ' -es -c "call doesnotexist()" +q; echo $?'
  63. let a = system(GetVimCommand() . cmd)
  64. call assert_notequal(0, a[0])
  65. endif
  66. let cmd = ' -es -c "call doesnotexist()" +q'
  67. let a = system(GetVimCommand(). cmd)
  68. call assert_notequal(0, v:shell_error)
  69. if has('unix') " echo $? only works on Unix
  70. let cmd = ' -es -c "call doesnotexist()|let a=1" +q; echo $?'
  71. let a = system(GetVimCommand() . cmd)
  72. call assert_notequal(0, a[0])
  73. endif
  74. let cmd = ' -es -c "call doesnotexist()|let a=1" +q'
  75. let a = system(GetVimCommand() . cmd)
  76. call assert_notequal(0, v:shell_error)
  77. endfunc
  78. func Test_system_with_shell_quote()
  79. CheckMSWindows
  80. call mkdir('Xdir with spaces', 'p')
  81. call system('copy "%COMSPEC%" "Xdir with spaces\cmd.exe"')
  82. let shell_save = &shell
  83. let shellxquote_save = &shellxquote
  84. try
  85. " Set 'shell' always needs noshellslash.
  86. let shellslash_save = &shellslash
  87. set noshellslash
  88. let shell_tests = [
  89. \ expand('$COMSPEC'),
  90. \ '"' . fnamemodify('Xdir with spaces\cmd.exe', ':p') . '"',
  91. \]
  92. let &shellslash = shellslash_save
  93. let sxq_tests = ['', '(', '"']
  94. " Matrix tests: 'shell' * 'shellxquote'
  95. for shell in shell_tests
  96. let &shell = shell
  97. for sxq in sxq_tests
  98. let &shellxquote = sxq
  99. let msg = printf('shell=%s shellxquote=%s', &shell, &shellxquote)
  100. try
  101. let out = 'echo 123'->system()
  102. catch
  103. call assert_report(printf('%s: %s', msg, v:exception))
  104. continue
  105. endtry
  106. " On Windows we may get a trailing space and CR.
  107. if out != "123 \n"
  108. call assert_equal("123\n", out, msg)
  109. endif
  110. endfor
  111. endfor
  112. finally
  113. let &shell = shell_save
  114. let &shellxquote = shellxquote_save
  115. call delete('Xdir with spaces', 'rf')
  116. endtry
  117. endfunc
  118. " vim: shiftwidth=2 sts=2 expandtab