summaryrefslogtreecommitdiff
path: root/spec/syntax_suggest/spec_helper.rb
blob: 89bc9f4ab1e638d319fdd8c51ed63547ce2a16ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

require "bundler/setup"
require "syntax_suggest/api"

require "benchmark"
require "tempfile"

RSpec.configure do |config|
  # Enable flags like --only-failures and --next-failure
  config.example_status_persistence_file_path = ".rspec_status"

  # Disable RSpec exposing methods globally on `Module` and `main`
  config.disable_monkey_patching!

  config.expect_with :rspec do |c|
    c.syntax = :expect
  end

  if config.color_mode == :automatic
    if config.color_enabled? && ((ENV["TERM"] == "dumb") || ENV["NO_COLOR"]&.slice(0))
      config.color_mode = :off
    end
  end
end

# Used for debugging modifications to
# display output
def debug_display(output)
  return unless ENV["DEBUG_DISPLAY"]
  puts
  puts output
  puts
end

def spec_dir
  Pathname(__dir__)
end

def lib_dir
  if ruby_core?
    root_dir.join("../lib")
  else
    root_dir.join("lib")
  end
end

def root_dir
  spec_dir.join("..")
end

def fixtures_dir
  spec_dir.join("fixtures")
end

def ruby_core?
  !root_dir.join("syntax_suggest.gemspec").exist?
end

def code_line_array(source)
  SyntaxSuggest::CleanDocument.new(source: source).call.lines
end

autoload :RubyProf, "ruby-prof"

def debug_perf
  raise "No block given" unless block_given?

  if ENV["DEBUG_PERF"]
    out = nil
    result = RubyProf.profile do
      out = yield
    end

    dir = SyntaxSuggest.record_dir("tmp")
    printer = RubyProf::MultiPrinter.new(result, [:flat, :graph, :graph_html, :tree, :call_tree, :stack, :dot])
    printer.print(path: dir, profile: "profile")

    out
  else
    yield
  end
end

def run!(cmd, raise_on_nonzero_exit: true)
  out = `#{cmd} 2>&1`
  raise "Command: #{cmd} failed: #{out}" if !$?.success? && raise_on_nonzero_exit
  out
end

# Allows us to write cleaner tests since <<~EOM block quotes
# strip off all leading indentation and we need it to be preserved
# sometimes.
class String
  def indent(number)
    lines.map do |line|
      if line.chomp.empty?
        line
      else
        " " * number + line
      end
    end.join
  end
end