/vendor/bundle/jruby/2.1/gems/rspec-core-2.14.8/lib/rspec/core/formatters/snippet_extractor.rb

https://github.com/delowong/logstash · Ruby · 92 lines · 53 code · 6 blank · 33 comment · 2 complexity · ea205b04af8cab8767bd58dfa709d35e MD5 · raw file

  1. module RSpec
  2. module Core
  3. module Formatters
  4. # @api private
  5. #
  6. # Extracts code snippets by looking at the backtrace of the passed error and applies synax highlighting and line numbers using html.
  7. class SnippetExtractor
  8. class NullConverter; def convert(code, pre); code; end; end
  9. begin
  10. require 'syntax/convertors/html'
  11. @@converter = Syntax::Convertors::HTML.for_syntax "ruby"
  12. rescue LoadError
  13. @@converter = NullConverter.new
  14. end
  15. # @api private
  16. #
  17. # Extract lines of code corresponding to a backtrace.
  18. #
  19. # @param [String] backtrace the backtrace from a test failure
  20. # @return [String] highlighted code snippet indicating where the test failure occured
  21. #
  22. # @see #post_process
  23. def snippet(backtrace)
  24. raw_code, line = snippet_for(backtrace[0])
  25. highlighted = @@converter.convert(raw_code, false)
  26. highlighted << "\n<span class=\"comment\"># gem install syntax to get syntax highlighting</span>" if @@converter.is_a?(NullConverter)
  27. post_process(highlighted, line)
  28. end
  29. # @api private
  30. #
  31. # Create a snippet from a line of code.
  32. #
  33. # @param [String] error_line file name with line number (i.e. 'foo_spec.rb:12')
  34. # @return [String] lines around the target line within the file
  35. #
  36. # @see #lines_around
  37. def snippet_for(error_line)
  38. if error_line =~ /(.*):(\d+)/
  39. file = $1
  40. line = $2.to_i
  41. [lines_around(file, line), line]
  42. else
  43. ["# Couldn't get snippet for #{error_line}", 1]
  44. end
  45. end
  46. # @api private
  47. #
  48. # Extract lines of code centered around a particular line within a source file.
  49. #
  50. # @param [String] file filename
  51. # @param [Fixnum] line line number
  52. # @return [String] lines around the target line within the file (2 above and 1 below).
  53. def lines_around(file, line)
  54. if File.file?(file)
  55. lines = File.read(file).split("\n")
  56. min = [0, line-3].max
  57. max = [line+1, lines.length-1].min
  58. selected_lines = []
  59. selected_lines.join("\n")
  60. lines[min..max].join("\n")
  61. else
  62. "# Couldn't get snippet for #{file}"
  63. end
  64. rescue SecurityError
  65. "# Couldn't get snippet for #{file}"
  66. end
  67. # @api private
  68. #
  69. # Adds line numbers to all lines and highlights the line where the failure occurred using html `span` tags.
  70. #
  71. # @param [String] highlighted syntax-highlighted snippet surrounding the offending line of code
  72. # @param [Fixnum] offending_line line where failure occured
  73. # @return [String] completed snippet
  74. def post_process(highlighted, offending_line)
  75. new_lines = []
  76. highlighted.split("\n").each_with_index do |line, i|
  77. new_line = "<span class=\"linenum\">#{offending_line+i-2}</span>#{line}"
  78. new_line = "<span class=\"offending\">#{new_line}</span>" if i == 2
  79. new_lines << new_line
  80. end
  81. new_lines.join("\n")
  82. end
  83. end
  84. end
  85. end
  86. end