pytest.vim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. " Vim compiler file
  2. " Compiler: Pytest (Python testing framework)
  3. " Maintainer: @Konfekt and @mgedmin
  4. " Last Change: 2024 Nov 28
  5. if exists("current_compiler") | finish | endif
  6. let current_compiler = "pytest"
  7. let s:cpo_save = &cpo
  8. set cpo&vim
  9. " CompilerSet makeprg=pytest
  10. if has('unix')
  11. execute $'CompilerSet makeprg=/usr/bin/env\ PYTHONWARNINGS=ignore\ pytest\ {escape(get(b:, 'pytest_makeprg_params', get(g:, 'pytest_makeprg_params', '--tb=short --quiet')), ' \|"')}'
  12. elseif has('win32')
  13. execute $'CompilerSet makeprg=set\ PYTHONWARNINGS=ignore\ &&\ pytest\ {escape(get(b:, 'pytest_makeprg_params', get(g:, 'pytest_makeprg_params', '--tb=short --quiet')), ' \|"')}'
  14. else
  15. CompilerSet makeprg=pytest\ --tb=short\ --quiet
  16. execute $'CompilerSet makeprg=pytest\ {escape(get(b:, 'pytest_makeprg_params', get(g:, 'pytest_makeprg_params', '--tb=short --quiet')), ' \|"')}'
  17. endif
  18. " Pytest syntax errors {{{2
  19. " Reset error format so that sourcing .vimrc again and again doesn't grow it
  20. " without bounds
  21. setlocal errorformat&
  22. " For the record, the default errorformat is this:
  23. "
  24. " %*[^"]"%f"%*\D%l: %m
  25. " "%f"%*\D%l: %m
  26. " %-G%f:%l: (Each undeclared identifier is reported only once
  27. " %-G%f:%l: for each function it appears in.)
  28. " %-GIn file included from %f:%l:%c:
  29. " %-GIn file included from %f:%l:%c\,
  30. " %-GIn file included from %f:%l:%c
  31. " %-GIn file included from %f:%l
  32. " %-G%*[ ]from %f:%l:%c
  33. " %-G%*[ ]from %f:%l:
  34. " %-G%*[ ]from %f:%l\,
  35. " %-G%*[ ]from %f:%l
  36. " %f:%l:%c:%m
  37. " %f(%l):%m
  38. " %f:%l:%m
  39. " "%f"\, line %l%*\D%c%*[^ ] %m
  40. " %D%*\a[%*\d]: Entering directory %*[`']%f'
  41. " %X%*\a[%*\d]: Leaving directory %*[`']%f'
  42. " %D%*\a: Entering directory %*[`']%f'
  43. " %X%*\a: Leaving directory %*[`']%f'
  44. " %DMaking %*\a in %f
  45. " %f|%l| %m
  46. "
  47. " and sometimes it misfires, so let's fix it up a bit
  48. " (TBH I don't even know what compiler produces filename(lineno) so why even
  49. " have it?)
  50. setlocal errorformat-=%f(%l):%m
  51. " Sometimes Vim gets confused about ISO-8601 timestamps and thinks they're
  52. " filenames; this is a big hammer that ignores anything filename-like on lines
  53. " that start with at least two spaces, possibly preceded by a number and
  54. " optional punctuation
  55. setlocal errorformat^=%+G%\\d%#%.%\\=\ \ %.%#
  56. " Similar, but when the entire line starts with a date
  57. setlocal errorformat^=%+G\\d\\d\\d\\d-\\d\\d-\\d\\d\ \\d\\d:\\d\\d%.%#
  58. " make: *** [Makefile:14: target] Error 1
  59. setlocal errorformat^=%+Gmake:\ ***\ %.%#
  60. " FAILED tests.py::test_with_params[YYYY-MM-DD:HH:MM:SS] - Exception: bla bla
  61. setlocal errorformat^=%+GFAILED\ %.%#
  62. " AssertionError: assert ...YYYY-MM-DD:HH:MM:SS...
  63. setlocal errorformat^=%+GAssertionError:\ %.%#
  64. " --- /path/to/file:before YYYY-MM-DD HH:MM:SS.ssssss
  65. setlocal errorformat^=---%f:%m
  66. " +++ /path/to/file:before YYYY-MM-DD HH:MM:SS.ssssss
  67. setlocal errorformat^=+++%f:%m
  68. " Sometimes pytest prepends an 'E' marker at the beginning of a traceback line
  69. setlocal errorformat+=E\ %#File\ \"%f\"\\,\ line\ %l%.%#
  70. " Python tracebacks (unittest + doctest output) {{{2
  71. " This collapses the entire traceback into just the last file+lineno,
  72. " which is convenient when you want to jump to the line that failed (and not
  73. " the top-level entry point), but it makes it impossible to see the full
  74. " traceback, which sucks.
  75. ""setlocal errorformat+=
  76. "" \File\ \"%f\"\\,\ line\ %l%.%#,
  77. "" \%C\ %.%#,
  78. "" \%-A\ \ File\ \"unittest%.py\"\\,\ line\ %.%#,
  79. "" \%-A\ \ File\ \"%f\"\\,\ line\ 0%.%#,
  80. "" \%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,
  81. "" \%Z%[%^\ ]%\\@=%m
  82. setlocal errorformat+=File\ \"%f\"\\,\ line\ %l\\,%#%m
  83. exe 'CompilerSet errorformat='..escape(&l:errorformat, ' \|"')
  84. let &cpo = s:cpo_save
  85. unlet s:cpo_save